I have the following code to add a group with a permission level to a sharepoint list
SPGroup Contribute = web.SiteGroups["Staff"];
SPRoleDefinition ContributeDefinition = web.RoleDefinitions["Read"];
SPRoleAssignment ContributeRoleAssignment = new SPRoleAssignment(Contribute);
ContributeRoleAssignment.RoleDefinitionBindings.Add(ContributeDefinition);开发者_开发问答
list.RoleAssignments.Add(ContributeRoleAssignment);
which adds the group, but how would I do it if I want to revoke the permission to the list for this group?
thanks in advance
If you need to remove whole role assgnment you can do:
SPUser user = ...;
list.RoleAssignments.Remove(user);
If you need to revoke some definition binding you can do:
SPUser user = ...;
list.RoleAssignments.GetAssignmentByPrincipal(user).RoleDefinitionBindings.Remove(..);
If that is not you want, please describe in more details.
UPDATE: Here is the sample code which I've tested and it works. It will remove Contribute role and add Read role to target group. Please note, that I have non-english Sharepoint version, so ensure the names of roles.
static void Main(string[] args)
{
using (SPSite site = new SPSite("site_url"))
{
using (SPWeb web = site.OpenWeb())
{
SPList list = web.GetList(web.Url + "/" + "list_url");
SPGroup group = web.SiteGroups["target_group_name"];
SPRoleDefinition contributeDef = web.RoleDefinitions["Contribute"];
SPRoleDefinition readDef = web.RoleDefinitions["Read"];
SPRoleAssignment contributeRole = new SPRoleAssignment(group);
contributeRole.RoleDefinitionBindings.Add(contributeDef);
if (!list.HasUniqueRoleAssignments) // required to make role change
list.BreakRoleInheritance(true);
var assignmentForGroup = list.RoleAssignments.GetAssignmentByPrincipal(group);
assignmentForGroup.RoleDefinitionBindings.Remove(contributeDef);
assignmentForGroup.RoleDefinitionBindings.Add(readDef);
assignmentForGroup.Update();
}
}
}
精彩评论