Friday, August 16, 2013

Create SharePoint List / Library Using PowerShell


Use CreateSharePointLibrary function to create a new SharePoint List or Library.

Parameters : 
  1. $webUrl - Mandatory - SharePoint Web Url - e.g. http://server:port/ 
  2. $LibraryName - Mandatory - SharePoint Library Name 
  3. $Discription - Mandatory - SharePoint Library Description 
  4. $LibraryTemplate - Mandatory - SharePoint Library Template 



function CreateSharePointLibrary
{  
    [CmdletBinding()]
    Param(
    [Parameter(Mandatory=$true,ValueFromPipeline=$true)]
    [string]$webUrl,
    [Parameter(Mandatory=$true)]
    [string]$LibraryName,
    [Parameter(Mandatory=$true)]
    [string]$Discription,
    [Parameter(Mandatory=$false)]
    [string]$LibraryTemplate
    )
   Start-SPAssignment -Global 

   $spWeb = Get-SPWeb -Identity $webUrl    
   $spListCollection = $spWeb.Lists  
   $spLibrary=$spWeb.Lists.TryGetList($LibraryName)
    if($spLibrary -ne $null)
    {
        write-host -f yellow "Library $LibraryName already exists in the site"
    }
    else
    {       
        Write-Host -NoNewLine -f yellow "Creating  Library - $LibraryName"
        $spListCollection.Add($LibraryName, $Discription, $LibraryTemplate)
        $spLibrary = $spWeb.GetList("$LibraryName")
        write-host -f Green "...Success!"
    }         
    Stop-SPAssignment -Global  
}


Function  Calling

$webUrl = "http://sever:port"

#-----------Document Library------------
$DocLibName = "DocLib1"
$LibraryTemplateDL = [Microsoft.SharePoint.SPListTemplateType]::DocumentLibrary
CreateSharePointLibrary $webUrl  $DocLibName  $DocLibName  $LibraryTemplateDL

# -------Data Connection Library-----------
$DataConnLibName = "DataConnLib1"
$LibraryTemplateDCL = [Microsoft.SharePoint.SPListTemplateType]::DataConnectionLibrary
CreateSharePointLibrary $webUrl  $DataConnLibName  $DataConnLibName $LibraryTemplateDCL


Similarly you can create any type of SharePoint List or Libraries , here are the details of List/Library Templates -
http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.splisttemplatetype.aspx

 

No comments: