field or property \“ListItemAllFields\” does not exist exception

牧云@^-^@ 提交于 2019-11-28 06:16:26

问题


Below code credit goes to Vadim Gremyachev. My goal is to grant user access permissions to certain SharePoint folders by using the CSOM. The goal I am trying to achieve is to access the library called JZhu, and inside JZhu library, I have two folders folder1 and folder2. I am trying to grant Reader permission to folder1. So far the code is not working because I get exception at line 6 saying:

field or property \"ListItemAllFields\" does not exist

 ClientContext context = new ClientContext("http://myRealUrl");
 Principal user = context.Web.EnsureUser(@"myLoginAccout");
 var folder = context.Web.GetFolderByServerRelativeUrl("JZhu/folder1");
 var roleDefinition = context.Site.RootWeb.RoleDefinitions.GetByType(RoleType.Reader);  //get Reader role
 var roleBindings = new RoleDefinitionBindingCollection(context) { roleDefinition };
 folder.ListItemAllFields.BreakRoleInheritance(true, false);  //line 6
 folder.ListItemAllFields.RoleAssignments.Add(user, roleBindings);
 context.ExecuteQuery();

回答1:


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:

  • SharePoint Foundation 2010 Client Object Model Redistributable targets SharePoint 2010 Foundation/Server (version 14)
  • SharePoint Server 2013 Client Components SDK targets SharePoint 2013 (version 15)
  • SharePoint Online Client Components SDK targets SharePoint Online (version 16)

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;


来源:https://stackoverflow.com/questions/29797298/field-or-property-listitemallfields-does-not-exist-exception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!