PowerShell: Create SharePoint List/Library
Use **Create-SharePointLibrary **function to create a new SharePoint List or Library.
Parameters
- $webUrl - Mandatory - SharePoint Web Url - e.g. http://server:port/
- $LibraryName - Mandatory - SharePoint Library Name
- $Description - Mandatory - SharePoint Library Description
- $LibraryTemplate - Mandatory - SharePoint Library Template
Code
function Create-SharePointLibrary {
[CmdletBinding()]
Param(
[Parameter(Mandatory=$true, Position=0, ValueFromPipeline=$true)]
[string]$webUrl,
[Parameter(Mandatory=$true, Position=1)]
[string]$LibraryName,
[Parameter(Mandatory=$true, Position=2)]
[string]$Description,
[Parameter(Mandatory=$false, Position=3)]
[string]$LibraryTemplate
)
Process
{
Start-SPAssignment -Global
$spWeb = Get-SPWeb -Identity $webUrl
$spListCollection = $spWeb.Lists
$spLibrary = $spListCollection.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, $Description, $LibraryTemplate)
$spLibrary = $spWeb.GetList($spWeb.ServerRelativeUrl+"/"+"$LibraryName")
Write-Host -f Green "...Success!"
}
Stop-SPAssignment -Global
}
}
Function Calling
$webUrl = "http://sever:port"
#-----------Document Library------------
$DocLibName = "DocLib1"
$LibraryTemplateDL = [Microsoft.SharePoint.SPListTemplateType]::DocumentLibrary
Create-SharePointLibrary $webUrl $DocLibName $DocLibName $LibraryTemplateDL
# -------Data Connection Library-----------
$DataConnLibName = "DataConnLib1"
$LibraryTemplateDCL = [Microsoft.SharePoint.SPListTemplateType]::DataConnectionLibrary
Create-SharePointLibrary $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
Other Languages
This article is also available in the following languages: