0

I have a site column of type People/Group named "SecurityGroups" which contain security groups. now i want to get those security groups and add them to our Folder permission. tried the following code:-

var securitygroups = i5["SecurityGroups"] as FieldUserValue[];
listItem.BreakRoleInheritance(false, false); // false clears out all permissions on the item
context.ExecuteQuery();

    foreach (FieldUserValue userValue in securitygroups)
        {
            var userInfoList = context.Site.RootWeb.SiteUserInfoList;
            context.ExecuteQuery();
                try
                    {
                        var userInfo = userInfoList.GetItemById(userValue.LookupId);
                        context.Load(userInfo, i => i.ContentType);
                        context.ExecuteQuery();
                        var userType = userInfo.ContentType.Name;
                             if (userType == "Person")
                                    {
                                        User oUser = context.Web.EnsureUser(userValue.LookupValue);
                                        RoleDefinitionBindingCollection collRoleDefinitionBinding = new RoleDefinitionBindingCollection(context);
                                        //RoleType.Contributor your permission level
                                        collRoleDefinitionBinding.Add(context.Web.RoleDefinitions.GetByType(RoleType.Contributor));
                                        listItem2.RoleAssignments.Add(oUser, collRoleDefinitionBinding);
                                    }
                             else
                                    {
                                        Microsoft.SharePoint.Client.Group oGroup = context.Web.SiteGroups.GetByName(userValue.LookupValue);
                                        RoleDefinitionBindingCollection collRoleDefinitionBinding = new RoleDefinitionBindingCollection(context);
                                        //RoleType.Contributor your permission level
                                        collRoleDefinitionBinding.Add(context.Web.RoleDefinitions.GetByType(RoleType.Contributor));
                                        listItem2.RoleAssignments.Add(oGroup, collRoleDefinitionBinding);
                                    }
                    }
                catch (Exception e)
                    { }
        }

but on the first context.ExecuteQuery() after the first foreach iteration i will get "Group Not Found" error

1 Answer 1

1

Your else statement is assuming that if it is not a "User" then it is treating it as if it were a SharePoint Group -- SharePoint Groups are not the same as Azure Active Directory (AAD) groups. A Multi-user column in SharePoint will not hold references to SharePoint Groups, only to AAD Users or AAD groups.

With that said, you've actually made things more complicated than necessary. Any value that can be held in a FieldUserValue in a User column, can also be retrieved using EnsureUser, and that object passed as the principal to RoleAssignments.Add, so you actually don't need two separate branches for your logic. You also don't need to keep re-constructing your RoleDefinitionBindingCollection inside the loop, you can construct it first, then re-use it for each new RoleAssignment.

Try this:

var securitygroups = i5["SecurityGroups"] as FieldUserValue[];
listItem.BreakRoleInheritance(false, false); // false clears out all permissions on the item
context.ExecuteQuery();

RoleDefinitionBindingCollection collRoleDefinitionBinding = new RoleDefinitionBindingCollection(context);
collRoleDefinitionBinding.Add(context.Web.RoleDefinitions.GetByType(RoleType.Contributor));

foreach (FieldUserValue userValue in securitygroups)
{
        try
        {
            User oUser = context.Web.EnsureUser(userValue.LookupValue);
            context.Load(oUser);
            context.ExecuteQuery();
            listItem2.RoleAssignments.Add(oUser, collRoleDefinitionBinding);
        }
        catch (Exception e)
            { }
}
1
  • worked well thanks
    – John John
    Feb 19, 2020 at 14:58

Your Answer

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

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