1

Following code adds user to SP Group. This code works perfectly and adding user to group. But permission is not going to set for the user from the group. When I go to site and "Check Permissions" user is not given permission from the added SP Group. Can anyone guess what goes wrong?

            ClientContext client = new ClientContext("SiteURL");
            client.ExecuteQuery();
            Web website = client.Web;
            client.Load(website, w => w.AllProperties, w => w.SiteGroups, w => w.SiteUserInfoList, w => w.Webs,w => w.Title);
            client.ExecuteQuery();
            GroupCollection groupCollection = website.SiteGroups;
            client.Load(groupCollection, groups => groups.Include(grps => grps.Users, grps => grps.Title));
            client.ExecuteQuery();
            User user;
            foreach (Group group in groupCollection)
            {
                if (group.Title.Equals("test Members"))
               {
                  UserCreationInformation userInfo = new UserCreationInformation();
                  userInfo.LoginName = @"mydomain\user";
                  user = group.Users.Add(userInfo);
                  group.Users.AddUser(user);
                  group.Update();
                  website.Update();
                  client.ExecuteQuery();
                }
            }
2
  • 1
    It sounds like the group does not have any permissions to the site. Can you confirm the group has at least Read access to the site?
    – Mihail
    Commented Jun 23, 2016 at 10:46
  • It has full permission. If I add user manually everything works fine. Through code is the problem. Commented Jun 23, 2016 at 11:09

2 Answers 2

3

If you only want to add a user to a group, then do this:

ClientContext client = new ClientContext("SiteURL");

Web web = client.Web;
Group group = web.SiteGroups.GetByName("test Members");
User user = web.EnsureUser("mydomain\\user"); // Note the double backslash.

// Add user to group.
group.Users.AddUser(user);
client.ExecuteQuery();

There is no need to call client.ExecuteQuery() everytime you get properties from an object. SharePoint CSOM supports batching, so you can basically lay out your business logic first and then execute it (at the end).

Also, make use of the built-in functions like web.SiteGroups.GetByName() instead of looping through all groups in the site collection.

1
  1. Go to Site Sittings page > Site Permissions page
  2. Find the SPGroup you have added there, Click on 'Edit User Permissions' in the ribbon
  3. You will be able to find the permission set for that group in the site

Important: If you were unable to find the SPGroup in your Site Permissions page, the you have just created the group and you have not set permissions for that group. In that case use 'Grant Permissions' button in the ribbon and grant premission to the required SPGroup.

If you have find any other scenario other than above, please post it as comment. We shall find what has gone wrong.

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.