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

后端 未结 1 869
醉酒成梦
醉酒成梦 2020-12-18 16:33

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

相关标签:
1条回答
  • 2020-12-18 16:50

    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;
    
    0 讨论(0)
提交回复
热议问题