December 11, 2014

How to set FileLeafRef when creating list items using CSOM

Problem

When using CSOM via PowerShell, and adding folder type list items, I needed to set also the Name field, a.k.a FileLeafRef field value for the item in order to be able to easily add children to items.

Setting FileLeafRef during initial item creation never worked.

Solution

First create the item, and after that change FileLeafRef.

 $listItemCreationInformation = New-Object Microsoft.SharePoint.Client.ListItemCreationInformation
 $listItemCreationInformation.FolderUrl = "http://site.com/lists/mylist/folder"
 $newItem = $list.AddItem($listItemCreationInformation);
    $title = "Item title"

  $newItem["Title"] = $title
 $newItem.Update();
 $clientContext.ExecuteQuery()

 $newItem["FileLeafRef"] = $title
 $newItem.Update();
 $clientContext.ExecuteQuery()

No comments:

Post a Comment