1
votes

I'm creating a new list in CSOM for SharePoint Online, but I'm very new to this so I'm stuck on changing the permissions. There are 3 user groups, owners, visitors and members. I'd like to give the visitors editing rights on this list.

I've managed to break inheritance and get all the visitors with the following code, but I'm stuck on how to give them new permissions

newList.BreakRoleInheritance(false, true);
var visitors = ctx.Web.SiteGroups;
ctx.Load(visitors, groupitems => groupitems.Include(groupitem => groupitem.Title,
    groupitem => groupitem.LoginName).Where(groupitem=> groupitem.Title == visitorsGroupName));
2

2 Answers

0
votes

You can do something similar to this one:

foreach(var grpUser in grpUsers)
{
    Principal user = ctx.Web.SiteUsers.GetByLoginName(grpUser);

    RoleDefinition writeDefinition = ctx.Web.RoleDefinitions.GetByName("Edit");
    RoleDefinitionBindingCollection roleDefCollection = new RoleDefinitionBindingCollection(ctx);
    roleDefCollection.Add(writeDefinition);
    RoleAssignment newRoleAssignment = ctx.Web.RoleAssignments.Add(grpUser, roleDefCollection);
}

ctx.ExecuteQuery();

This assumes that there is an "Edit" permission level which is available by default in SharePoint. Let me know if it works.

0
votes

You could try this

var EditRole= new RoleDefinitionBindingCollection(ctx);
EditRole.Add(ctx.Web.RoleDefinitions.GetByType(RoleType.Editor));
Microsoft.SharePoint.Client.Group visitors= ctx.Web.SiteGroups.GetByName("visitors");
ctx.Load(visitors);
newList.RoleAssignments.Add(visitors, EditRole);
ctx.ExecuteQuery()