简体   繁体   English

无法使用空的角色定义绑定集合添加角色分配

[英]Cannot add a role assignment with empty role definition binding collection

I am trying to give a permission to user using ClientContext in SharePoint 2013. I have done all exactly the same as in the Microsoft web site http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.client.roledefinitionbindingcollection.add.aspx 我试图授予在SharePoint 2013中使用ClientContext的用户权限。我所做的所有操作与Microsoft网站http://msdn.microsoft.com/zh-cn/library/microsoft.sharepoint.client中的操作完全相同。 roledefinitionbindingcollection.add.aspx

But at the end, this part of code always returns exception "Cannot add a role assignment with empty role definition binding collection": 但是最后,这部分代码始终返回异常“无法添加具有空角色定义绑定集合的角色分配”:

RoleAssignment oRoleAssignment = w.RoleAssignments.Add(oUser, roleDefBinding);                            
clientContext.ExecuteQuery();

I haves searched web, found similar problems to some other users but without responses. 我搜索了网络,发现与其他一些用户类似的问题,但没有任何答复。 Any ideas? 有任何想法吗?

My Code: 我的代码:

clientContext.Load(w.RoleDefinitions);
clientContext.ExecuteQuery();
var role = w.RoleDefinitions.Where(r => r.Name == roleName);
if (role.Count() > 0)
{
       RoleDefinition roleMSP = role.First();
       clientContext.Load(w.SiteUsers);
       clientContext.ExecuteQuery();
       var user = w.SiteUsers.Where(u=> u.LoginName == "c:0+.w|s-1-5-21-3493872076-3631449775-1555872641-1347");
       if (user.Count() > 0)
       {
             // Create a new RoleDefinitionBindingCollection object.
             RoleDefinitionBindingCollection roleDefBinding = new RoleDefinitionBindingCollection(clientContext);
             roleDefBinding.Add(roleMSP);
             User oUser = user.First() as User;

             clientContext.Load(w.RoleAssignments);
             clientContext.ExecuteQuery();

             RoleAssignment oRoleAssignment = w.RoleAssignments.Add(oUser, roleDefBinding);                            
             clientContext.ExecuteQuery();//Here I get an exception
        }
}

Resolved! 解决! Removed the following lines of code: 删除了以下代码行:

clientContext.Load(w.RoleAssignments);
clientContext.ExecuteQuery();

Looks like you don`t need to retrieve the list of Assignements and after add your one. 看起来您不需要检索分配列表,然后添加您的分配列表。

Working code for me: 我的工作代码:

 RoleDefinitionCollection roleDefs = web.RoleDefinitions;
 var query = projectCtx.LoadQuery(roleDefs.Where(p => p.RoleTypeKind == RoleType.Contributor));
 projectCtx.ExecuteQuery();
 RoleDefinition roledefObj = query.FirstOrDefault();

 RoleDefinitionBindingCollection collRoleDefinitionBinding = new RoleDefinitionBindingCollection(projectCtx) { roledefObj };
 var roleAssignments = web.RoleAssignments;
 roleAssignments.Add(principalTest, collRoleDefinitionBinding);

 projectCtx.ExecuteQuery();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM