如何将特定SharePoint组添加到列表权限

时间:2012-08-01 14:24:44

标签: c# sharepoint sharepoint-2010 sharepoint-2007

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)上有错误,例如“无法使用空角色定义绑定集合添加角色分配”。我想停止继承权限并将特定组添加到列表权限。你能帮助我吗?请..

由于

2 个答案:

答案 0 :(得分:15)

这应该有用。

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();

答案 1 :(得分:4)

您需要为角色分配添加角色定义绑定,例如添加参与者角色定义

roleAssignment.RoleDefinitionBindings.Add(web.RoleDefinitions.GetByType(SPRoleType.Contributor));