字段或属性\" ListItemAllFields\"不存在异常 [英] field or property \"ListItemAllFields\" does not exist exception

查看:369
本文介绍了字段或属性\" ListItemAllFields\"不存在异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的代码归功于瓦迪姆Gremyachev。我的目标是通过使用 CSOM 授予用户访问权限对某些的SharePoint 文件夹。我想达到的目标是访问一个名为 JZhu ,并在 JZhu 库库,我有两个文件夹文件夹1 文件夹2 。我想授予阅读权限文件夹1 。到目前为止,代码是不工作,因为我会在第6行话说异常:




字段或属性\ListItemAllFields\不存在




  ClientContext上下文=新ClientContext(HTTP:// myRealUrl); 
主要用户= context.Web.EnsureUser(@myLoginAccout);
变种文件夹= context.Web.GetFolderByServerRelativeUrl(JZhu /文件夹1);
VAR roleDefinition = context.Site.RootWeb.RoleDefinitions.GetByType(RoleType.Reader); //获取读者角色
变种roleBindings =新RoleDefinitionBindingCollection(上下文){} roleDefinition;
folder.ListItemAllFields.BreakRoleInheritance(真,假); // 6号线
folder.ListItemAllFields.RoleAssignments.Add(用户,roleBindings);
context.ExecuteQuery();


解决方案

最有可能的错误:




字段或属性\ListItemAllFields\不存在




由于出现您所使用CSOM SDK是的不兼容的与SharePoint Server的版本,尤其是你正在使用的SDK版本15或16对SharePoint 2010的



的一点是,对于每个SharePoint版本已经发布了一个单独的SDK:





如何获得通过CSOM与文件夹关联列表项在SharePoint 2010



所以,如果我的假设是正确的,那么你首先需要安装< A HREF =https://www.microsoft.com/en-us/download/details.aspx?id=21786相对=nofollow> SharePoint Foundation 2010的客户端对象模型可再发行。



其次,由于文件夹类做的不可以公开 ListItemAllFields 属性在SharePoint 2010 CSOM API,你可以利用下面的方法获取相关列表项文件夹

 静态类ListExtensions 
{
///<总结>
///根据URL
///<加载列表项; /总结>
///< PARAM NAME =清单>< /参数>
///< PARAM NAME =URL>< /参数>
///<&回报GT;< /回报>
公共静态列表项LoadItemByUrl(名单列表中,字符串URL)
{
VAR背景= list.Context;
VAR的查询=新CamlQuery
{
ViewXml =的String.Format(<视图>< RowLimit> 1< / RowLimit><查询><其中><公式> < FieldRef名称='FileRef/><值类型='URL'> {0}< /值>< / EQ>< /在哪里>< /查询>< /视图> URL),
};
VAR项目= list.GetItems(查询);
context.Load(项目);
context.ExecuteQuery();
返回items.Count> 0?项目[0]:空;
}
}



然后,你可以设置唯一权限为文件夹,如下所示:

 主要用户= ctx.Web.EnsureUser(帐户名) ; 
无功名单= ctx.Web.Lists.GetByTitle(LISTTITLE);
VAR folderItem = list.LoadItemByUrl(folderUrl);
VAR roleDefinition = ctx.Site.RootWeb.RoleDefinitions.GetByType(RoleType.Reader); //获取读者角色
变种roleBindings =新RoleDefinitionBindingCollection(CTX){} roleDefinition;
folderItem.BreakRoleInheritance(真,假); // 6号线
folderItem.RoleAssignments.Add(用户,roleBindings);
ctx.ExecuteQuery();



如何获得与文件夹通过CSOM在SharePoint 2013相关的项目



由于 Folder.ListItemAllFields物业是SharePoint提供的 2013 CSOM,下面的例子演示了如何获得项目关联的文件夹

  VAR文件夹= context.Web.GetFolderByServerRelativeUrl(folderUrl); 
VAR folderItem = folder.ListItemAllFields;


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();

解决方案

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;

这篇关于字段或属性\&QUOT; ListItemAllFields\&QUOT;不存在异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆