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! :)

Monday, October 17, 2016

Updating SharePoint Multi Select Lookup field and Multi Select Choice filed in SharePoint 2013 REST api

What is Multi Select Lookup field ?


A Multi select lookup column is a field in the SharePoint list whose values are retrieved from another List. Using a multi select lookup column, you can display a list of choices in a combo box or list box. Since the lookup column is multi select, you can choose more than one item in the list.

What is Multi Select Choice filed ?


A Multi select choice field in the SharePoint list is column type that is possible to have more than one choice at once.

Work around


In normal scenario when a Lookup field is updated, as we know item id of the lookup list should be saved against the lookup column, but when it comes to the Multi select lookup column it is different.

When the normal Choice filed is updated string choice should be saved against the choice column, but if it is a Multi select choice field, it is different.

By using blow shown rest endpoint, all the LookupMulti type columns in a particular SharePoint list can be retrieved.  

{site Url}/_api/web/lists/getbytitle('List Name')/fields?$filter=TypeAsString eq 'LookupMulti'

Please consider the result

1 Results for REST call

Bellow shown image shows the type of the field which is in JSON object.

2. Type of the column

Blow shown image shows a item which is retrieved using mentioned rest api call.

{site Url}/_api/web/lists/getbytitle('List Name')/items

As it shown in the bellow image, Multi select lookup field is contained a object. That object has two attributes metadata and results. metadata contained another object and result contained int array.

3. Retrieved result
Now we will see how we should update Multi select lookup field and Multi select choice field using REST api.

If the Multi select lookup field needs to be updated, below shown object should be saved against the multi select lookup column.

"result" should be a int array, which contains the item ids of the lookup list.


var lookUpMultiItemObj = {
                                                   __metadata: { "type": "Collection(Edm.Int32)" },
                                                   results: [1,3]
                                             };
listItem["Column Name"] = lookUpMultiItemObj
;

If the Multi select choice field needs to be updated, below shown object should be saved against the multi select choice column.

"result" should be a string array, which contains the choices of the field.


var choiceMultiItemObj = {
                                                   __metadata: { "type": "Collection(Edm.String)" },
                                                   results: ["Choice 2","Choice 4"]
    };
listItem["Column Name"] = choiceMultiItemObj;


4. Real code sample


After completing aforementioned steps, by using a normal ajax POST request the relevant items can be updated.


Keep sharing the knowledge! :)