通过roleDefinition从SpListItem中删除组

时间:2016-11-21 12:39:07

标签: c# sharepoint

我正在尝试删除所有具有特殊RoleDefinition的组。

SPGroupCollection s = item.RoleAssignments.Groups;

SPGroup group = properties.Web.SiteGroups[grp];
SPRoleDefinition definition = item.Web.RoleDefinitions[(int)SPRoleType.Reader];
foreach (SPGroup removeGroup in s)
{
    bool removethisGroup = false;
    SPRoleAssignment removerole = new SPRoleAssignment(removeGroup);
    if (removerole.Member is SPGroup)
    { 
        foreach (SPRoleDefinition rdb in removerole.RoleDefinitionBindings)
        {
            if (rdb == "Read")
            {
                removethisGroup = true;
            }
        }
    }
    if (removethisGroup)
    {
        item.RoleAssignments.RemoveById(removeGroup.ID);
    }
}
经过大量的尝试和搜索,我有点困惑。我需要通过roleDefinition“Read”

删除所有组

1 个答案:

答案 0 :(得分:0)

我找到了答案。这个解决方案对我来说是最好的。

SPGroup group = properties.Web.SiteGroups[grp];

SPRoleDefinition definition = item.Web.RoleDefinitions.GetByType(SPRoleType.Reader);

SPRoleAssignmentCollection roles = item.RoleAssignments;

for (int i = 0; i < roles.Count; i++)
{
    bool removeThisRole = false;
    SPRoleDefinitionBindingCollection rbc = roles[i].RoleDefinitionBindings;
    foreach (SPRoleDefinition rdef in rbc)
    {
        if (rdef.Type == SPRoleType.Reader)
            removeThisRole = true;
    }

    if (removeThisRole)
    {
        if (!item.HasUniqueRoleAssignments) // required to make role change
            item.BreakRoleInheritance(true);

        item.RoleAssignments.Remove(i);
        item.Update();
        removeThisRole = false;
    }
}