Vince Orio
Vince Orio

Reputation: 85

Why are items with unique permissions being affected when I delete Users/Groups in the list permission

I'm new to SharePoint and csom. I'm trying to figure out how to change existing group/user permissions in the list without inadvertently changing the item permission inside that said list. Here's the code:

myList.BreakRoleInheritance(true, true);

var roletypes = ctx.Web.RoleDefinitions.GetByType(RoleType.Reader);
ctx.ExecuteQuery();
RoleDefinitionBindingCollection colRoleDefinitionBinding = new RoleDefinitionBindingCollection(ctx);
colRoleDefinitionBinding.Add(roletypes);

// if user, add him with read access
if (!item.ContainsKey("group"))
{
     Principal entity = ctx.Web.EnsureUser(item["user"]);
     myList.RoleAssignments.GetByPrincipal(entity).DeleteObject();
     myList.RoleAssignments.Add(entity, colRoleDefinitionBinding);
}
else
{
     Group entity = ctx.Web.SiteGroups.GetById(Int32.Parse(item["group"]));
     myList.RoleAssignments.GetByPrincipal(entity).DeleteObject();
     myList.RoleAssignments.Add(entity, colRoleDefinitionBinding);
}

ctx.ExecuteQuery();

I'm essentially just removing the user or group and adding them again with their new permissions. When deleting the user/group, if that user or group exist in the item permission then it is also removed. What I dont understand is why the item permission is being tampered when it has unique permissions? Is there another way to change the list permissions without affecting the item permission?

Upvotes: 1

Views: 627

Answers (1)

Vince Orio
Vince Orio

Reputation: 85

Figured it out. Just use ImportRoleDefinitionBindings to change the permission:

Principal entity = ctx.Web.EnsureUser(item["user"]);
roleAssignment = myList.RoleAssignments.GetByPrincipal(entity);
roleAssignment.ImportRoleDefinitionBindings(colRoleDefinitionBinding);
roleAssignment.Update();
ctx.ExecuteQuery();

Upvotes: 2

Related Questions