gpt4 book ai didi

sharepoint - 如何以编程方式从 sharepoint 2010 的列表权限中添加和删除组?

转载 作者:行者123 更新时间:2023-12-03 17:46:32 25 4
gpt4 key购买 nike

我有以下代码将具有权限级别的组添加到共享点列表

SPGroup Contribute = web.SiteGroups["Staff"];
SPRoleDefinition ContributeDefinition = web.RoleDefinitions["Read"];
SPRoleAssignment ContributeRoleAssignment = new SPRoleAssignment(Contribute);
ContributeRoleAssignment.RoleDefinitionBindings.Add(ContributeDefinition);
list.RoleAssignments.Add(ContributeRoleAssignment);

这会添加该组,但是如果我想撤销该组的列表权限,我该怎么做?

提前致谢

最佳答案

如果您需要删除整个角色分配,您可以执行以下操作:

SPUser user = ...;
list.RoleAssignments.Remove(user);

如果您需要撤销某些定义绑定(bind),您可以执行以下操作:

SPUser user = ...;
list.RoleAssignments.GetAssignmentByPrincipal(user).RoleDefinitionBindings.Remove(..);

如果这不是您想要的,请更详细地描述。

更新:这是我已经测试过并且有效的示例代码。它将删除贡献角色并将读取角色添加到目标组。请注意,我有非英文 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();
}
}
}

关于sharepoint - 如何以编程方式从 sharepoint 2010 的列表权限中添加和删除组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4930260/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com