Most probably the error:
field or property "ListItemAllFields" does not exist
occurs since you are using CSOM SDK that is not compatible with SharePoint server version, in particular your are using version 15 or 16 of SDK against SharePoint 2010.
The point is that for each SharePoint version have been released a separate SDKs:
How to get List Item associated with Folder via CSOM in SharePoint 2010
So, if my assumption is correct then you first need to install SharePoint Foundation 2010 Client Object Model Redistributable.
Secondly, since Folder class does not expose ListItemAllFields
property in SharePoint 2010 CSOM API, you could utilize the following method for getting associated ListItem
with Folder
:
static class ListExtensions
{
/// <summary>
/// Load List Item by Url
/// </summary>
/// <param name="list"></param>
/// <param name="url"></param>
/// <returns></returns>
public static ListItem LoadItemByUrl(this List list, string url)
{
var context = list.Context;
var query = new CamlQuery
{
ViewXml = String.Format("<View><RowLimit>1</RowLimit><Query><Where><Eq><FieldRef Name='FileRef'/><Value Type='Url'>{0}</Value></Eq></Where></Query></View>", url),
};
var items = list.GetItems(query);
context.Load(items);
context.ExecuteQuery();
return items.Count > 0 ? items[0] : null;
}
}
Then you could set unique permissions for a Folder
as demonstrated below:
Principal user = ctx.Web.EnsureUser(accountName);
var list = ctx.Web.Lists.GetByTitle(listTitle);
var folderItem = list.LoadItemByUrl(folderUrl);
var roleDefinition = ctx.Site.RootWeb.RoleDefinitions.GetByType(RoleType.Reader); //get Reader role
var roleBindings = new RoleDefinitionBindingCollection(ctx) { roleDefinition };
folderItem.BreakRoleInheritance(true, false); //line 6
folderItem.RoleAssignments.Add(user, roleBindings);
ctx.ExecuteQuery();
How to get List Item associated with Folder via CSOM in SharePoint 2013
Since Folder.ListItemAllFields property is available in SharePoint 2013 CSOM, the following example demonstrates how to get List Item
associated with Folder
:
var folder = context.Web.GetFolderByServerRelativeUrl(folderUrl);
var folderItem = folder.ListItemAllFields;
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…