我有以下代码将具有权限级别的组添加到共享点列表
SPGroup Contribute = web.SiteGroups["Staff"];
SPRoleDefinition ContributeDefinition = web.RoleDefinitions["Read"];
SPRoleAssignment ContributeRoleAssignment = new SPRoleAssignment(Contribute);
ContributeRoleAssignment.RoleDefinitionBindings.Add(ContributeDefinition);
list.RoleAssignments.Add(ContributeRoleAssignment);
添加了该组,但是如果我想撤销该组列表的权限,我该怎么办呢?
提前致谢
答案 0 :(得分:6)
如果你需要删除整个角色分配,你可以这样做:
SPUser user = ...;
list.RoleAssignments.Remove(user);
如果你需要撤销一些定义绑定,你可以这样做:
SPUser user = ...;
list.RoleAssignments.GetAssignmentByPrincipal(user).RoleDefinitionBindings.Remove(..);
如果您不想,请详细说明。
更新: 这是我测试过的示例代码,它的工作原理。它将删除Contribute角色并将Read角色添加到目标组。请注意,我有非英文版的Sharepoint版本,因此请确保角色名称。
static void Main(string[] args)
{
using (SPSite site = new SPSite("site_url"))
{
using (SPWeb web = site.OpenWeb())
{
SPList list = web.GetList(web.Url + "/" + "list_url");
SPGroup group = web.SiteGroups["target_group_name"];
SPRoleDefinition contributeDef = web.RoleDefinitions["Contribute"];
SPRoleDefinition readDef = web.RoleDefinitions["Read"];
SPRoleAssignment contributeRole = new SPRoleAssignment(group);
contributeRole.RoleDefinitionBindings.Add(contributeDef);
if (!list.HasUniqueRoleAssignments) // required to make role change
list.BreakRoleInheritance(true);
var assignmentForGroup = list.RoleAssignments.GetAssignmentByPrincipal(group);
assignmentForGroup.RoleDefinitionBindings.Remove(contributeDef);
assignmentForGroup.RoleDefinitionBindings.Add(readDef);
assignmentForGroup.Update();
}
}
}