Add Fields To List with PowerShell in SharePoint 2010 – Part2

Add Fields To List with PowerShell in SharePoint 2010 – Part2
3 votes, 5.00 avg. rating (97% score)


This is continuation to my earlier post Adding Fields To SharePoint List.
This post includes how to create lookup fields, checkbox field, person or group field and hyperlink field.
With this coverage the most common and frequent tasks related to SharePoint 2010 list with powershell will be now easier.

I will start the code again with creating list(of course the same piece of code in earlier posts) just not to get lost by just seeing field xml

Adding lookup field

 $TestSiteUrl = "http://mysitecollection/mysite"
 $NewWebobj = Get-SPWeb -identity $TestSiteUrl
 $newListName = "EmpList"
 $newListDescription = "Employee Information"
 $listTemplate = [Microsoft.SharePoint.SPListTemplateType]::GenericList
 $lstId = $NewWebobj.Lists.Add($ListName,$ListDescription,$ListTemplateType)
 write-host "list " $ListName " created successfully" $ListName -ForegroundColor Green
 $listUrl = $NewWebobj.ServerRelativeUrl + "/lists/" + $newListName;
 $myCustomList = $NewWebobj.GetList($listUrl)#get the lookup list ID
 $lookupListName = "MyLookupList"
 $LookulistUrl = $NewWebobj.ServerRelativeUrl + "/lists/" + $lookupListName;
 $lookupListObj = $NewWebobj.GetList($LookulistUrl)
 $lookupListId = $lookupListObj.ID#adding column of type 'lookup'
$fieldXml = "<Field Type='Lookup' DisplayName='LookupCol1' List='{" + $lookupListId + "}' ShowField='Title' 
RelationshipDeleteBehavior='None' Name='LookupCol1'/>"
$fieldXml2 = "<Field Type='Lookup' DisplayName='LookupCol2' List='{" + $lookupListId + "}' ShowField='Title'
               Indexed='True' RelationshipDeleteBehavior='Restrict' Name='LookupCol2'/>"
$fieldXml3 = "<Field Type='Lookup' DisplayName='LookupCol3' List='{" + $lookupListId + "}' ShowField='Title' 
Indexed='True' RelationshipDeleteBehavior='Cascade' Name='LookupCol3'/>"
write-host "Error" $_.exception
 $errorlabel = $true
 if($NewWebobj -ne $null) {$myTestWeb.Dispose()}
if($errorlabel -eq $true){exit 1}
 else {exit 0}
exit 0

Above code creates a custom list with three types of lookup columns.
Considering only the snippet from the above code

$fieldXml = "<Field Type='Lookup' DisplayName='LookupCol1' List='{" + $lookupListId + "}' ShowField='Title' 
RelationshipDeleteBehavior='None' Name='LookupCol1'/>"


$lookupListId is the ID of the list to which the lookup column belongs to

RelationshipDeleteBehavior=’None’ states that if the column in the actual list is deleted; then all other lists which are referring that column will be orphaned. So,the data will be there in other lists that referred this column, but no more relation.

In other lookup fieldxmls, we have used RelationshipDeleteBehavior with ‘Restrict‘ and ‘Cascade
RelationshipDeleteBehavior=’Restrict’ states that if we try to delete the column in the actual list; then SharePoint will restrict to delete unless all the data with lookup references are removed first.

RelationshipDeleteBehavior=’Cascade’ states that if we try to delete the column in the actual list; then SharePoint will delete all the referenced data where ever this column is used as lookup column.
It will give us warning if we want to continue the delete the operation or not

We have another way of creating lookup column. Infact I prefer creating columns with xml string, because it gives overview of complete properties what the list has.

#set all other properties like we do in c#

Adding field of type ‘Checkbox’

Creating field of type ‘Checkbox’

$fldXml = "<Field Type='Boolean' DisplayName='chkBoxCol1' Name='chkBoxCol1'>

The above code will create checkbox field in the list with default values as checked.
1 this makes checkbox checked by default
0 this makes checkbox unchecked by default

Adding field of type ‘People or Group column’

$fldXml = "<Field Type='User' DisplayName='PersonCol1' List='UserInfo' ShowField='ImnName' 
UserSelectionMode='PeopleOnly' UserSelectionScope='0' Name='PersonCol1'/>"

The above code will create people and group column ‘PersonCol1’ which allows only user(people) values but not user group names.
Setting UserSelectionMode=’PeopleAndGroups’ will allow user group values.
There are other possible values which can be set for ShowField attribute –
Title, NameWithPicture, NameWithPictureAndDetails, ImnName (this is NameWithPresence)

Adding field of type ‘Hyperlink’

$fldXml = "<Field Type='URL' DisplayName='HyperlinkCol1' Format='Hyperlink' Name='HyperlinkCol1'/>"

The above code will create Hyperlink column ‘HperlinkCol1’.
The property Format can have other value ‘Image


Hope you have good time while working with PowerShell and SharePoint 2010.I will continue to blog other important tasks we will encounter with SharePoint 2010 and also PowerShell.
Happy coding :)

March 29, 2012 · Adi · 4 Comments
Tags: , , ,  · Posted in: Packaging and Deployment, Powershell, Sharepoint 2010, Uncategorized

4 Responses

  1. Sergio - November 22, 2015

    Thank you for both Part1 and Part2 posts. They’re terrific ! :)

    The last 3 depicted types have a typo: on [Microsoft.SharePoint.SPAddFieldOptions]::, you have forgottehn the opening “[“

  2. Adi - November 23, 2015

    Thanks Sergio. Some wordpress plugins copy paste has issues, will monitor next time.
    I really like whoever gives improvement tips and any issues in the posts :)
    Happy coding

  3. Kevin L - March 17, 2016

    thanks for a great list, newer to SP and this saved me a ton of time building out my first PowerShell scripts. again thanks

  4. Adi - May 4, 2016

    You are welcome Kevin :)

Leave a Reply

What is 3 + 3 ?
Please leave these two fields as-is:
IMPORTANT! To be able to proceed, you need to solve the following simple math (so we know that you are a human) :-)