Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
914 views
in Technique[技术] by (71.8m points)

c# - field or property "ListItemAllFields" does not exist exception

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();
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...