4

Not a programmer, but need to get a job done.

Using Powershell and CSOM I've found examples and been able to import CSV into a list as new items. That is working. Here is a code snippet I'm using that comes after I already got the list:

[Microsoft.SharePoint.Client.Web]$web = $context.Web
[Microsoft.SharePoint.Client.List]$list = $web.Lists.GetByTitle($listName)
foreach ($row in $csv) {
[Microsoft.SharePoint.Client.ListItemCreationInformation]$itemCreateInfo = New-Object Microsoft.SharePoint.Client.ListItemCreationInformation;
[Microsoft.SharePoint.Client.ListItem]$item = $list.AddItem($itemCreateInfo);
$item["Title"] = $row.Title; ..... etc.

This works great to add new items to the list. However, now I need to update existing items in the list and can't seem to find any examples specific to what I'm using. Seems like it might be the Microsoft.Sharepoint.Client.ChangeItem, but I don't know how to use it.

Examples from MSDN, ChangeItem class etc show C# and VB. Thanks if anyone has an answer.

7

As an introductory please follow How to: Complete basic operations using SharePoint 2013 client library code article to get acquainted with how to update a SharePoint list item via CSOM.

How to update list items via CSOM in PowerShell

Assume the following data is contained in CSV file

ID,FullName,Company,Title
1,Pilar Ackerman,Contoso Ltd,Manager
2,Jonathan Haas,Contoso Ltd,Finance Specialist

Then, the following script could be used to update list item(s) in Contacts list

Function Update-ListItem([Microsoft.SharePoint.Client.List]$List,$ID, $Properties)
{
    [Microsoft.SharePoint.Client.ClientContext]$context = $List.Context
    [Microsoft.SharePoint.Client.ListItem]$listItem = $List.GetItemById($ID) 
    foreach($p in $Properties.psobject.Properties) {
       if($p.Name -eq "ID")  {
           continue
       }     
       $listItem[$p.Name] = $p.Value  
    }
    $listItem.Update()
    $context.ExecuteQuery()
}

[Microsoft.SharePoint.Client.List]$list = $context.Web.Lists.GetByTitle($ListTitle)

$data = Import-Csv "c:\upload\Contacts.txt"
foreach ($row in $data) {
   Update-ListItem -List $list -ID $row.ID -Properties $row 
}

Key points:

  • ID field is used for getting an existing list item
  • 2
    Super answer Vadim! This is a very good example of how to answer a SP.SE question! Thanx for all the great work you put in here! – Benny Skogberg Aug 20 '14 at 11:08
  • 2
    Thank you Benny for the kind words and being a great contributor and moderator here in SP.SE! – Vadim Gremyachev Aug 20 '14 at 11:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.