Thursday, October 27, 2016

Updating a item in the SharePoint list using SharePoint management shell

In SharePoint there are number of field types are available. Those are Text, Choice, Number, Date, Yes/No, Hyperlink, Lookup and LookupMulti, Person or Group. According to the field type, the way of inserting will be different (different type of object should be assigned according to the field type).

As a first step the relevant item should be retrieved prior to the update.

$web = get-spweb "SiteCollection url"
$list = $web.lists | where {$_.title -eq "List Name"}
$item = $list.items | where {$_['Column Name'] -eq "Query Value"}

As the second step relevant type of objects should be set to the appropriate fields.Bellow shown SharePoint management shell script shows the way values are assigned to Text, Yes/No, Choice, Number, DateTime, Hyperlink, Person or Group, Lookup and LookupMulti type fields.

$item["Text"] = "Test Value"
$item["Yes/No"] = $true
$item["Choice"] = "MyChoice"
$item["Number"] = 10
$item["DateTime"] = Get-Date
$item["Hyperlink"] = "<a href="http://neranjanb1987.blogspot.com">My Blog</a>, Neranjan Bandara"

$spUser = get-spuser -Web "Url of the SiteCollection" -Identity "Login Name"
$item["PersonorGroup"] = $spUser

$lookup = New-Object Microsoft.Sharepoint.SPFieldLookupValue(1,"Service 1")
$item["Lookup"] = $lookup

$lookup1 = New-Object Microsoft.Sharepoint.SPFieldLookupValue(1,"Value 1")
$lookup2 = New-Object Microsoft.Sharepoint.SPFieldLookupValue(2,"Value 2")
$lookup3 = New-Object Microsoft.Sharepoint.SPFieldLookupValue(3,"Value 3")
$multiLookup = New-Object Microsoft.Sharepoint.SPFieldMultiChoiceValue($null)
$multiLookup.Add($lookup1)
$multiLookup.Add($lookup2)
$multiLookup.Add($lookup3)

$item["LookupMulti"] = $multiLookup

As the Final step item should be updated, after the item updating, the list also should be updated.

$item.update()
$list.update()

Now your item is updated properly, after the complection of the script check the item in the SharePoint list.


Keep sharing the knowledge! :)

No comments:

Post a Comment