6

I need to assign unique permission to a SharePoint list. I have the SP group created in the site and the list permission inheritance is broken. Now I would like to assign permission to the group for this list by adding this group in the list permission. Below is the code I am trying but it says the specified name already exists. Kindly help.

public static void AddPermission(ClientContext cpx,List list, string groupName, string permissionName)

        {            
            Web web = cpx.Web;
            Site site = cpx.Site;
            RoleDefinitionBindingCollection collRoleDefinitionBinding = new RoleDefinitionBindingCollection(cpx);


            Principal userGroup = FindUSerorSiteGroup(groupName,cpx,site, groupName);

                RoleAssignment spRoleAssign = list.RoleAssignments.GetByPrincipal(userGroup);

              //  RoleDefinition role = web.RoleDefinitions.GetByName("Contribute");
            collRoleDefinitionBinding.Add(cpx.Web.RoleDefinitions.GetByType(RoleType.Reader));
            //   spRoleAssign.RoleDefinitionBindings.Add(role);
            list.RoleAssignments.Add(userGroup, collRoleDefinitionBinding);

                spRoleAssign.Update();

                list.Update();
          //  cpx.Load(list);
            cpx.ExecuteQuery();



        }
private static Principal FindUSerorSiteGroup(string groupname,ClientContext coj,Site site, string userOrGroup)
        {
            Principal myuser = null;
            Web web = coj.Web;

            if (userOrGroup != null)

                            {
                Microsoft.SharePoint.Client.GroupCollection gs = coj.Web.SiteGroups;
                coj.Load(gs, groupitems => groupitems.Include(groupitem => groupitem.Title, groupitem => groupitem.Description).Where(groupitem => groupitem.Title == groupname));
                coj.Load(gs);
                coj.Load(site.RootWeb.SiteGroups);
                coj.ExecuteQuery();
                //might be a group

                foreach (Microsoft.SharePoint.Client.Group g in site.RootWeb.SiteGroups)

                {

                    if (g.Title.ToUpper() == userOrGroup.ToUpper())

                    {                      
                        myuser = g;
                        Microsoft.SharePoint.Client.Group group = web.SiteGroups.GetByName(userOrGroup);
                        User owner = web.EnsureUser("Ankit Kumar");
                        GroupCreationInformation objCreateInfo = new GroupCreationInformation();
                        objCreateInfo.Title = userOrGroup;
                        web.SiteGroups.Add(objCreateInfo);
                        break;

                    }

                }

            }

            return myuser;
        }
6

Ok I figured out, I used the below code:

public static void AddPermission(ClientContext cpx, List list,
                                 string groupName, string permissionName)
{
    //  Web web = cpx.Web;
    //  Site site = cpx.Site;
    var GRoup1 = cpx.Web.SiteGroups.GetByName(groupName);
    var roletypes= cpx.Web.RoleDefinitions.GetByType(RoleType.Contributor);
    cpx.ExecuteQuery();
    list.BreakRoleInheritance(true, false);
    RoleDefinitionBindingCollection collRoleDefinitionBinding = 
         new RoleDefinitionBindingCollection(cpx);
    collRoleDefinitionBinding.Add(roletypes);
    list.RoleAssignments.Add(GRoup1, collRoleDefinitionBinding);
    cpx.ExecuteQuery();
}
| improve this answer | |
1

If the group is already present and having Full control or other higher level permissions than the permission we need to set then the above code is not useful as the permissions are added with previous permissions. Hence before adding the Role Assignments we need to check if group is already present then delete(remove) the group with previous permissions and then re-add the group.

list.RoleAssignments.Add(GRoup1, collRoleDefinitionBinding); list.RoleAssignments.Groups.Remove(GRoup1); list.RoleAssignments.Add(GRoup1, collRoleDefinitionBinding);

Same Can be implemented for single user rather than a group viz.
list.RoleAssignments.Add(user, collRoleDefinitionBinding); list.RoleAssignments.GetByPrincipal(user).DeleteObject(); list.RoleAssignments.Add(user, collRoleDefinitionBinding);

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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