Search This Blog

Thursday, March 26, 2015

Add Fields To SharePoint List with PowerShell in SharePoint 2010 (Part2)



This blog 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.
 Adding lookup field
$errorlabel=$falsetry
 {
 $TestSiteUrl = "http://XXXXXXXX:XXXX/sites/XXXX"
 $NewWebobj = Get-SPWeb -identity $TestSiteUrl
 $newListName = "ProductList"
 $newListDescription = "Product Information"
 $listTemplate = [Microsoft.SharePoint.SPListTemplateType]::GenericList
 $lstId = $NewWebobj.Lists.Add($ListName,$ListDescription,$ListTemplateType)
 write-host "list " $newListName " created successfully" $newListName -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'/>"
$myCustomList.Fields.AddFieldAsXml($fieldXml,$true,
[Microsoft.SharePoint.SPAddFieldOptions]::AddFieldToDefaultView)

$fieldXml2 = "<Field Type='Lookup' DisplayName='LookupCol2' List='{" + $lookupListId + "}' ShowField='Title'
               Indexed='True' RelationshipDeleteBehavior='Restrict' Name='LookupCol2'/>"
$myCustomList.Fields.AddFieldAsXml($fieldXml2,$true,
[Microsoft.SharePoint.SPAddFieldOptions]::AddFieldToDefaultView)

$fieldXml3 = "<Field Type='Lookup' DisplayName='LookupCol3' List='{" + $lookupListId + "}' ShowField='Title'
Indexed='True' RelationshipDeleteBehavior='Cascade' Name='LookupCol3'/>"
$myCustomList.Fields.AddFieldAsXml($fieldXml3,$true,
[Microsoft.SharePoint.SPAddFieldOptions]::AddFieldToDefaultView)

$myCustomList.Update()
 }
 catch
 {

write-host "Error" $_.exception
 $errorlabel = $true
 }
 finally
 {
 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.
Need to Considering only the snippet from the above code
$fieldXml = "<Field Type='Lookup' DisplayName='LookupCol1' List='{" + $lookupListId + "}' ShowField='Title'
RelationshipDeleteBehavior='None' Name='LookupCol1'/>"
$myCustomList.Fields.AddFieldAsXml($fieldXml,$true,
[Microsoft.SharePoint.SPAddFieldOptions]::AddFieldToDefaultView)

$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.
$myCustomList.Fields.AddLookup("Title",$lookupListId,$false)
#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'>
           <Default>1</Default>
           </Field>"
$myCustomList.Fields.AddFieldAsXml($fldXml,$true,Microsoft.SharePoint.SPAddFieldOptions]::AddFieldToDefaultView)
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'/>"
$myCustomList.Fields.AddFieldAsXml($fldXml,$true,Microsoft.SharePoint.SPAddFieldOptions]::AddFieldToDefaultView)

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'/>"
$myCustomList.Fields.AddFieldAsXml($fldXml,$true,Microsoft.SharePoint.SPAddFieldOptions]::AddFieldToDefaultView)

The above code will create Hyperlink column ‘HperlinkCol1′.
The property Format can have other value ‘Image
Reference:
http://adicodes.com/adding-fields-to-list-with-powershell-in-sharepoint-2010-part2/

No comments:

Post a Comment