本文介绍了在项目添加的事件接收器中找不到ID为237的委托人的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
am试图删除现有权限并为splist项添加新的权限,在itemadded事件接收器内部:am出现错误w
am trying to remove existing permissions and add new permsision for an splist item , inside itemadded event receiver:am getting error with getting SPPrincipals,.
这是独家新闻:我需要授予对SPItem的权限.我得到了SPUser:
Here is the scoop: I need to grant permissions to SPItem. I get SPUser:
SPUser user = web.AllUsers.GetByID // {i:0#.w|domain\ad_name}
检查用户当前的权限:
Check what current permissions for the user:
SPRoleAssignment currentUserRole = item.RoleAssignments.GetAssignmentByPrincipal(user);
收到异常:
Getting exception:
如何解决此问题?
how to resolve this ?
推荐答案
我认为您的用户属于一个组,因此您可以不能获取基于用户的SPRoleAssignment.
例如:
Userb属于站点组开发人员成员 .
我拥有父项的listitem继承权限,因此userb拥有列表项的编辑权限.
我们可以根据小组负责人获得SPRoleAssignment无法基于userb获得SPRoleAssignment.
//fine
SPGroup group = web.SiteGroups["Developer Members"];
SPRoleAssignment rag = item.RoleAssignments.GetAssignmentByPrincipal(group);
//error, except you break permission for the list item once
SPUser userb = web.EnsureUser("contoso\\userb");
SPRoleAssignment rag1 = item.RoleAssignments.GetAssignmentByPrincipal(userb);
如果您想直接授予用户权限,请尝试使用以下代码:
SPList list = web.Lists.TryGetList("MyList");
SPListItem item = list.Items[0];
//SPGroup group = web.SiteGroups["Developer Members"];
//SPRoleAssignment rag= item.RoleAssignments.GetAssignmentByPrincipal(group);
//SPUser usera = web.EnsureUser("contoso\\usera");
//SPRoleAssignment raga = item.RoleAssignments.GetAssignmentByPrincipal(usera);
SPUser userb = web.EnsureUser("contoso\\userb");
//SPRoleAssignment rag1 = item.RoleAssignments.GetAssignmentByPrincipal(userb);
SPRoleAssignment roleAssignment = new SPRoleAssignment(userb);
SPRoleDefinition roleDefinition = web.RoleDefinitions.GetByType(SPRoleType.Contributor);
roleAssignment.RoleDefinitionBindings.Add(roleDefinition);
if (!item.HasUniqueRoleAssignments)
{
item.BreakRoleInheritance(true); // Ensure we don't inherit permissions from parent
SPRoleAssignmentCollection SPRoleAssColn = item.RoleAssignments;
for (int i = SPRoleAssColn.Count - 1; i >= 0; i--)
{
SPRoleAssColn.Remove(i);
}
Console.WriteLine("All Permissions Removed");
}
item.RoleAssignments.Add(roleAssignment);
item.Update();
}
Console.WriteLine("done");
Console.ReadKey();
最好的问候,跨度>
Lee
这篇关于在项目添加的事件接收器中找不到ID为237的委托人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!