我正在尝试使用SharePoint 2013中的ClientContext向用户授予权限。我已完成与Microsoft网站http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.client.roledefinitionbindingcollection.add.aspx
完全相同的操作但最后,这部分代码总是返回异常“无法添加具有空角色定义绑定集合的角色分配”:
RoleAssignment oRoleAssignment = w.RoleAssignments.Add(oUser, roleDefBinding);
clientContext.ExecuteQuery();
我搜索了网页,发现类似的问题给其他一些用户但没有回复。 有什么想法吗?
我的代码:
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
}
}
答案 0 :(得分:5)
解决! 删除了以下代码行:
clientContext.Load(w.RoleAssignments);
clientContext.ExecuteQuery();
看起来你不需要检索Assignements列表,然后添加你的。
答案 1 :(得分:0)
为我工作的代码:
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();