Create Custom List with PowerShell in SharePoint 2010
Introduction
This article provides overview of how to create Custom List from powershell2.0 in SharePoint 2010.
I will explain in series of posts from small snippets to full pledged organizing of script that can be used for deploying in staging
environments
Task
Create a SharePoint Custom List EmpInfo with text columns
FirstName – mandatory
LastName – not mandatory
Create Sharepoint List code
Remove-PSSnapin Microsoft.SharePoint.PowerShell -erroraction SilentlyContinue Add-PSSnapin Microsoft.SharePoint.PowerShell -erroraction SilentlyContinue try { $TestSiteUrl = "http://mysitecollection/mysite" #provide site url in this variable $ListName = "EmpInfo" #listName $ListDescription = "Employee information list" #list description $myTestWeb = Get-SPWeb -identity $TestSiteUrl #Get web object $listTemplate = [Microsoft.SharePoint.SPListTemplateType]::GenericList #GenericList template write-host "Adding list" $ListName #column1 schema xml $firstNameColXml = "<Field Type='Text' DisplayName='FirstName' Required='TRUE' EnforceUniqueValues='FALSE' MaxLength='255′ StaticName='FirstName' Name='FirstName' />" #column2 schema xml $lastNameColXml = "<Field Type='Text' DisplayName='LastName' Required='FALSE' EnforceUniqueValues='FALSE' MaxLength='255′ StaticName='LastName' Name='LastName' />" #build the list url $listUrl = $myTestWeb.ServerRelativeUrl + "/lists/" + $ListName; #we can't use getlist here as the method raises filenotfoundexception if the list url is not there $myCustomList = $myTestWeb.Lists[$ListName] if($myCustomList -eq $null) { $lstId = $myTestWeb.Lists.Add($ListName,$ListDescription,$listTemplate) $myCustomList = $myTestWeb.GetList($listUrl) # use getlist here as the list already exists #Add columns $myCustomList.Fields.AddFieldAsXml($firstNameColXml,$true, [Microsoft.SharePoint.SPAddFieldOptions]::AddFieldToDefaultView) $myCustomList.Fields.AddFieldAsXml($lastNameColXml,$true, [Microsoft.SharePoint.SPAddFieldOptions]::AddFieldToDefaultView) $myCustomList.Update() write-host "list created successfully" $ListName } else { write-host "List already exists" $ListName } } catch { write-host "Error" $_.exception $errorlabel = $true } finally { if($myTestWeb -ne $null) {$myTestWeb.Dispose()} if($errorlabel -eq $true){exit 1} else {exit 0} }exit 0
In the above code
Remove-PSSnapin Microsoft.SharePoint.PowerShell -erroraction SilentlyContinue Add-PSSnapin Microsoft.SharePoint.PowerShell -erroraction SilentlyContinue
These commands will make the powershell to refer sharepoint dlls and identity the commands we have written. Sometimes if the dll is already referred; you may not get error. But, remember this is the best way to add so that you will never encounter error.
$myCustomList = $myTestWeb.Lists[$ListName]
Better coding practice says that not to use web.Lists[listname]. But, here we are using this to check if the list exists or not. So, if we use GetList method to get the list object; FileNotFoundException exception will raise if the list is not there.
This is documented in msdn also SPWeb.GetList Method
We have to use GetList method only when we are sure that List is available
firstNameColXml & lastNameColXml are the schema xmls as per our scenario. We can add respective schema xml for the new columns
to create
Conclusion
Hope this post is useful for you covering some basic tasks. I will continue with series of scripts that cover common scenarios and with better coding standards. Happy coding and keep following the blog
March 26, 2012
·
Adi ·
No Comments
Tags: Create Custom List with PowerShell in SharePoint 2010, GetList, Lists.Add · Posted in: Packaging and Deployment, Powershell, Sharepoint 2010, Sharepoint Deployment
Leave a Reply