Search This Blog

Tuesday, March 24, 2015

Create Custom List from powershell2.0 in SharePoint 2010

Introduction

In this article explains how to create Custom List from powershell2.0 in SharePoint 2010.

Task

Create a SharePoint Custom List EmpInfo with text columns
FirstName – mandatory
LastName – not mandatory
Remove-PSSnapin Microsoft.SharePoint.PowerShell -erroraction SilentlyContinue

Add-PSSnapin Microsoft.SharePoint.PowerShell -erroraction SilentlyContinue

try

{

$TestSiteUrl = "http://xxxxxx:xxxx/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

1. Remove-PSSnapin Microsoft.SharePoint.PowerShell -erroraction SilentlyContinue
2. 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.


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.

1. $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

Reference:

http://adicodes.com/create-custom-list-with-powershell-in-sharepoint-2010/

No comments:

Post a Comment