Quantcast
Viewing all articles
Browse latest Browse all 41

SharePoint 2010 Powershell read lookup or person field & add value

Powershell is great, powershell can do really great things if you know how to. So i would like to share some experiences with you.

Basic: Get the list and the item of your list:


$Web = Get-SPWeb "http://server/sites/sitecollection"
$List = $Web.Lists["Listname"]
//All items where ID is equal "1" you can set it as you like
$Item = $List.Items | ? {$_["ID"] -eq "1"}
$groupcol = $Item["YourField"]


Reading the values of a lookup


$ValueCollection = New-Object Microsoft.SharePoint.SPFieldUserValueCollection
foreach($group in $groupcol)
{
$group.lookupvalue
}


Adding value to this field


//This value collection will be saved afterwords into the field and it overrides the old values
$ValueCollection = New-Object Microsoft.SharePoint.SPFieldUserValueCollection
foreach($group in $groupcol)
{
//Add the old values to the new Value collection
$ValueCollection.Add($group)
$myGroup.id = "1"
$myGroup.Name = "Test"
$GroupValue = new-Object Microsoft.SharePoint.SPFieldUserValue($Web,
$myGroup.id, $myGroup.Name)
$ValueCollection.Add($GroupValue)
}
$Item["YourField"] = $ValueCollection
$Item.Update()


Hope this helps.

..:: I LIKE SHAREPOINT ::..


Viewing all articles
Browse latest Browse all 41

Trending Articles