4
using (SPSite site = (SPSite)properties.Feature.Parent)
{
   using (SPWeb web = site.OpenWeb())
   {
        if (web != null)
        {
              web.AllowUnsafeUpdates = true;
              SPList list = web.Lists["Alert Status v1.0"];

              //Creates a new role assignment for a group
              SPGroup myGroup = web.SiteGroups["IKM Manager"];
              SPRoleAssignmentCollection roleAssignments = web.RoleAssignments;

              // SPRoleAssignment accepts a SPPrincipal which can be a SPUser or SPGroup
              SPRoleAssignment roleAssignment = new SPRoleAssignment(myGroup);

              //Add the new role assignment to the collection of role assignments for the site.
              roleAssignments.Add(roleAssignment);

              // Stop inheriting permissions
              list.BreakRoleInheritance(true);
              list.RoleAssignments.Add(roleAssignment);
              list.Update();

我在 RoleAssignments.Add(roleAssignment) 上有错误,例如“无法添加具有空角色定义绑定集合的角色分配”。我想停止继承权限并将特定组添加到列表权限。你能帮助我吗?请..

谢谢

4

2 回答 2

15

This should work.

list.BreakRoleInheritance(true);
SPGroup groupAdmin = web.SiteGroups["IKM Manager"];
SPRoleAssignment roleAssignmentAdmin = new SPRoleAssignment((SPPrincipal)groupAdmin);
SPRoleDefinition roleAdmin = web.RoleDefinitions.GetByType(SPRoleType.Administrator);
roleAssignmentAdmin.RoleDefinitionBindings.Add(roleAdmin);
list.RoleAssignments.Add(roleAssignmentAdmin);
list.Update();
于 2012-08-03T05:05:05.140 回答
4

您需要将角色定义绑定添加到角色分配,例如添加贡献者角色定义

roleAssignment.RoleDefinitionBindings.Add(web.RoleDefinitions.GetByType(SPRoleType.Contributor));
于 2012-08-01T14:35:45.223 回答