如何使用C#更改sharepoint 2013中的组权限

时间:2015-02-20 20:19:58

标签: c# sharepoint permissions roles

我有一个具有Contribute权限的“访问者”组,我想以编程方式更改Read的权限。我没有向组添加新权限的问题,但我不知道如何删除旧权限。

如何更改此代码以删除权限Contribute并添加权限Read?

SPWeb root = site.RootWeb;
SPGroup group = root.SiteGroups["Visitors"];  
SPRoleDefinition roleDefinition = root.RoleDefinitions.GetByType(SPRoleType.Reader);
SPRoleAssignment roleAssignment = new SPRoleAssignment(group);
roleAssignment.RoleDefinitionBindings.Add(roleDefinition);
root.RoleAssignments.Add(roleAssignment);
root.Update();

1 个答案:

答案 0 :(得分:2)

以下示例演示如何:

  • 向群组添加Reader权限级别
  • 从群组中删除Contribute权限级别

示例:

SPWeb root = site.RootWeb;
SPGroup group = root.SiteGroups[groupName];

SPRoleDefinition contributeRoleDef = root.RoleDefinitions.GetByType(SPRoleType.Contributor);
SPRoleDefinition readerRoleDef = root.RoleDefinitions.GetByType(SPRoleType.Reader);
SPRoleAssignment groupRoleAssignments = root.RoleAssignments.GetAssignmentByPrincipal(group);
groupRoleAssignments.RoleDefinitionBindings.Remove(contributeRoleDef);
groupRoleAssignments.RoleDefinitionBindings.Add(readerRoleDef);
groupRoleAssignments.Update();