I want to delete all users in a site having full control permissions and assign them to contribute permissions.
I can able to add contribute permission to them. But while deleting the existing full contribute permission I am getting an error :
"Collection was modified; enumeration operation may not execute."
SPRoleDefinitionCollection WebRoles = web.RoleDefinitions;
SPRoleDefinition roledef = web.RoleDefinitions["Full Control"];
SPRoleAssignmentCollection assignColl = web.RoleAssignments;
foreach (SPRoleAssignment assignmentgn in assignColl)
{
if (assignmentgn.RoleDefinitionBindings.Contains(roledef))
{
if (assignmentgn.Member is SPGroup)
{
SPGroup group = (SPGroup)assignmentgn.Member;
foreach (SPUser user in group.Users)
{
if (user.IsSiteAdmin == false)
{
web.AllowUnsafeUpdates = true;
//web.RoleAssignments.Remove(user);
SPRoleAssignment newroleassignweb = new SPRoleAssignment(user);
newroleassignweb.RoleDefinitionBindings.Add(WebRoles["Contribute"]);
web.RoleAssignments.Add(newroleassignweb);
web.Update();
web.AllowUnsafeUpdates = false;
}
}
}
}
}