字段或属性\" ListItemAllFields\"不存在异常 [英] field or property \"ListItemAllFields\" does not exist exception
问题描述
下面的代码归功于瓦迪姆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:
- 的 SharePoint Foundation 2010的客户端对象模型可再发行
目标的SharePoint Foundation 2010中/ Server(版本14)
- SharePoint服务器2013客户端组件SDK 目标的SharePoint
2013 Jun 2013(第15版)
- 的 SharePoint Online的客户端组件SDK 目标的SharePoint
在线(版本16)
如何获得通过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 theCSOM
. The goal I am trying to achieve is to access the library calledJZhu
, and insideJZhu
library, I have two foldersfolder1
andfolder2
. I am trying to grantReader
permission tofolder1
. 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:
- 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 associatedListItem
withFolder
: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 withFolder
:var folder = context.Web.GetFolderByServerRelativeUrl(folderUrl); var folderItem = folder.ListItemAllFields;
这篇关于字段或属性\" ListItemAllFields\"不存在异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!