Lorenzo
Lorenzo

Reputation: 29427

SharePoint 2010 change list permission

I have a list contained in an SPWeb and when the web is provisioned an event receiver change the list permissions by using this code:

[...]
SPList theList = web.Lists[listName];
theList.BreakRoleInheritance(false);
SPGroup group = web.Groups["MyGroup"];
SPRoleAssignment ra = new SPRoleAssignment(group);
SPRoleDefinition roleDef = web.RoleDefinitions.GetByType(SPRoleType.Contributor);
ra.RoleDefinitionBindings.Add(roleDef);
list.RoleAssignments.Add(ra);
list.Update();

Later in the workflow I need to change the permission for the group to be Reader instead of Contributor. I have used the following code

SPList theList = web.Lists[listName];
foreach (SPRoleAssignment assignment in theList.RoleAssignments) {
    if (assignment.Member.Name == "MyGroup") {
        assignment.RoleDefinitionBindings.RemoveAll();
        SPRoleDefinition rda = web.RoleDefinitions.GetByType(SPRoleType.Reader);
        assignment.RoleDefinitionBindings.Add(rda);
    }
}

However this code is not working and does not change the permission on the list.

Could anybody help on understanding which is the right method to change permission for an existing principal?

Thanks

Upvotes: 1

Views: 122

Answers (1)

AdamBT
AdamBT

Reputation: 1926

You need to do a theList.Update() after your foreach statement.

Upvotes: 1

Related Questions