none
Invite/Share external user to Group using CSOM? RRS feed

  • Question

  • Hi all.

    Using Sharepoint Online w/Norwegian language.

    I have been trying to add a external (none Live) user to a site-collection in a specific group. 

    I am able to invite/share with the site with the user, but have a problem defining the group the user should be invited in.

    I have the following code:

     public static string AddUserToSite(string liveId, string siteId, int groupId)
            {
                using (var clientContext = GetSharepointClientUrl(_login, _password, $"{_siteUrl}{siteId}"))
                {
                    var web = clientContext.Web;
                    clientContext.Load(web, x => x.SiteGroups);
                    clientContext.ExecuteQuery();
    
                    var users = new List<UserRoleAssignment>();
                    var userAssigment = new UserRoleAssignment { Role = Role.LimitedEdit, UserId = liveId };
                    users.Add(userAssigment);
    
                    WebSharingManager.UpdateWebSharingInformation(clientContext, clientContext.Web, users, true, null, true, true);
                    clientContext.ExecuteQuery();
                }
                return null;
            }

    This invites the user, but the user is not in any group, is seems like it's in the "root" folder with the access/permission being = "Role.LimitedEdit" (not any friendly default name).

    In the "Pending Invitation" list (before user has accepeted), under the "Permission" column is says "Choose group or permission-level" (or what ever the English equivalent is) 

    Have also tried to invite the user with Role.None, then afterwards adding user to the specific group, but this does not work with "none-live" email, also no-one gets invitation mails. 

    Any tips?


    Thursday, January 5, 2017 1:06 PM

All replies

  • Hi,

    As your method, try to invite the user with Role.None, then afterwards adding user to the specific group. And then add a workflow to send mail to the user.

    Or please check the thread below:

    Adding external users to SharePoint Group

    https://social.technet.microsoft.com/Forums/office/en-US/9480545a-783d-421f-99c4-6062c071353c/adding-external-users-to-sharepoint-group?forum=sharepointgeneral

    Best Regards,

    Dennis


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    Friday, January 6, 2017 8:55 AM
    Moderator
  • Hi, thx for the answer.

    This does indeed work, but only for "Ensured"/liveenabledusers. If the user does not have a registered "microsoft" account, I get user not found in the EnsureUser method.

    IE:

     using (var clientContext = GetSharepointClientUrl(_login, _password, $"{_siteUrl}{siteId}"))
                {
                    var web = clientContext.Web;
                    clientContext.Load(web, x => x.SiteGroups);
                    clientContext.ExecuteQuery();
    
                    var users = new List<UserRoleAssignment>();
                    var userAssigment = new UserRoleAssignment { Role = Role.None, UserId = liveId };
                    users.Add(userAssigment);
    
                    WebSharingManager.UpdateWebSharingInformation(clientContext, clientContext.Web, users, true, null, true, true);
                    clientContext.ExecuteQuery();
    
                    var user = clientContext.Web.EnsureUser(liveId);
    
                    clientContext.Load(user);
                    clientContext.ExecuteQuery();
    
                    var spGroup = web.SiteGroups.GetById(groupId);
                    clientContext.Load(spGroup);
    
                    var spUserToAdd = spGroup.Users.AddUser(user);
                    clientContext.Load(spUserToAdd);
                    clientContext.ExecuteQuery();
    
    
                }
                return null;

    Or can you do this without the Web.EnsureUser method?

    What bugs me is that you can do this no problem in the SharePoint web-GUI. You can even update the pending invitation to attach it to a group.

    Ok, is there anyway to alter a pending invitation then? Because then I can invite the user (as is), the invitation is created, then update it pending invitation to regard the specific group? 

    Friday, January 6, 2017 4:23 PM
  • One last *bump*
    Monday, January 16, 2017 1:17 PM