如何以编程方式更新sharepoint 2010中组的角色定义?

时间:2011-10-20 09:06:01

标签: sharepoint

我以编程方式创建了一个具有读取和贡献角色的SharePoint组 现在,我正在尝试使用其他角色定义更新该组的角色。像完全控制等。但我无法从SharePoint组中删除现有角色。

1 个答案:

答案 0 :(得分:0)

尝试以下代码。首先需要确定roledefinition(假设您使用的是现有的)。之后,将组添加到角色,然后将角色添加到Web:

public enum RoleTypeEnum { fullcontrol, design, contribute, read };

 //declare roledefinition
 SPRoleDefinition roleDefinition;

 //switch to determine role
 switch (roleType)
 {
      case RoleTypeEnum.contribute:
          roleDefinition = spWeb.RoleDefinitions[Constants.SiteCollection.SecurityTypeNames.contribute];
          break;
      case RoleTypeEnum.design:
          roleDefinition = spWeb.RoleDefinitions[Constants.SiteCollection.SecurityTypeNames.design];
          break;
      case RoleTypeEnum.fullcontrol:
          roleDefinition = spWeb.RoleDefinitions[Constants.SiteCollection.SecurityTypeNames.fullcontrol];
          break;
      case RoleTypeEnum.read:
          roleDefinition = spWeb.RoleDefinitions[Constants.SiteCollection.SecurityTypeNames.read];
          break;
      default:
          return null;
}

//add group to role
SPGroup group = spWeb.SiteGroups[groupName];
SPRoleAssignment roleAssignment = new SPRoleAssignment(group);
roleAssignment.RoleDefinitionBindings.Add(roleDefinition);

//add role to web
spWeb.RoleAssignments.Add(roleAssignment);
spWeb.Update();

我在交换机中使用的常量是荷兰语,但这里是:

            public static partial class SecurityTypeNames
        {
            public const string fullcontrol = "Volledig beheer";

            public const string design = "Ontwerpen";

            public const string contribute = "Bijdragen";

            public const string read = "Lezen";

        }