0

I have run the csom code for break inheritance and set the unique permission on list item (ex.contribute). when user modified the list item, i want to the remove that unique permission and set other permission (ex.read).

  if (listItem.HasUniqueRoleAssignments)
   {
     listItem.BreakRoleInheritance(false, false);
     //istItem.ResetRoleInheritance();    
     context.ExecuteQuery();
    }

 if (!listItem.HasUniqueRoleAssignments)
   {
    listItem.BreakRoleInheritance(false, false);
    context.ExecuteQuery();
    }
 Console.WriteLine("BreakRoleInheritance");

2 Answers 2

0

Try the below code to remove the unqiue permission, then set the unique permission on list item to read.

  ClientContext ctx = new ClientContext("https://yoursite/sites/dev");
  Web web = ctx.Web;
  List list = web.Lists.GetByTitle("test");
  ListItem listitem = list.GetItemById("1");
  listitem.ResetRoleInheritance();
  listitem.BreakRoleInheritance(false,false);
  User user = web.SiteUsers.GetByLoginName(@"contoso\michael");
  RoleDefinitionBindingCollection roleDefinitionBinding = new RoleDefinitionBindingCollection(ctx);
  roleDefinitionBinding.Add(web.RoleDefinitions.GetByType(RoleType.Reader));
  listitem.RoleAssignments.Add(user, roleDefinitionBinding);
  ctx.ExecuteQuery();
1
  • You saved my day. It is working fine. Thanks a lot
    – Liz
    Jan 29, 2020 at 5:51
0

I would suggest you to use Microsoft Power Automate which will be easy for breaking inheritance and setting unique permission on item.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.