Sunday, May 24, 2015

Creating a SharePoint List with columns and views via powershell


-------------------------------------------------------------------------------


$spWeb = Get-SPWeb -Identity http://xyz:2234/
$spListCollection = $spWeb.Lists
$spTemplate = $spWeb.ListTemplates["Custom List"]

###########Create Category List  #################

$ListName="Category"
$spList = $spListCollection.TryGetList($ListName)
if($spList -eq $null)
{
    $spListCollection.Add($ListName,$ListName,$spTemplate)
    $path = $spWeb.url.trim()
    $spList = $spWeb.GetList("$path/Lists/" + $ListName)
}

$Views = $spList.Views["All Items"]

#Create Columns
$FieldName = "Category"
if($spList.Fields.ContainsField($FieldName) -ne $true)
{
  $spFieldType = [Microsoft.SharePoint.SPFieldType]::Text
  $spList.Fields.Add($FieldName,$spFieldType,$false)
  $Views.ViewFields.Add($FieldName)
}

$Views.Update()


#########################################

Download All Wsp solution from  Central Administration in SharePoint


 From the below script, You need to change the $dirName only .

 Run  The script in poweshell editor. or management shell.

$dirName = "<directory path>" 
Write-Host Exporting solutions to $dirName  
foreach ($solution in Get-SPSolution)  
{  
    $id = $Solution.SolutionID  
    $title = $Solution.Name  
    $filename = $Solution.SolutionFile.Name 
    Write-Host "Exporting ?$title? to ?\$filename" -nonewline  
    try {  
        $solution.SolutionFile.SaveAs("$dirName\$filename")  
        Write-Host " ? done" -foreground green  
    }  
    catch  
    {  
        Write-Host " ? error : $_" -foreground red  
    }  
}


----------------------------------------------------------------------