none
Add users to groups in all site collections using JSOM RRS feed

  • Question

  • Hi all,

    I have a requirement like i have webapplication with multiple sitecollections and each site collection  some sub sites as well.

    I wanted to add users to all site collections by looping all site collections in a webapplication and wanted to add users based on the groups selection.For single site collection i can add by following this article https://www.c-sharpcorner.com/blogs/how-to-add-users-sharepoint-group-using-jsom.I wanted to loop all  site collections and subsites and get gropus and based on the selection i need to add the users in respective groups.I have to use JSOM for this.Please help me how to achieve this.Thanks in advance.

    Regards,

    Praveen


    • Edited by Praveenrangu Thursday, December 13, 2018 5:16 AM
    Thursday, December 13, 2018 5:15 AM

Answers

All replies

  • Hi, you can try below code:

    var siteUrl = '/sites/MySiteCollection ';
    
    function addUserToSharePointGroup() {
    
        var clientContext = new SP.ClientContext(siteUrl);
        var collGroup = clientContext.get_web().get_siteGroups();
        var oGroup = collGroup.getById(7);
        var userCreationInfo = new SP.UserCreationInformation();
        userCreationInfo.set_email('alias@somewhere.com');
        userCreationInfo.set_loginName('DOMAIN\alias');
        userCreationInfo.set_title('John');
        this.oUser = oGroup.get_users().add(userCreationInfo);
        
        clientContext.load(oUser);
        clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
    
    }
    
    function onQuerySucceeded() {
            
        alert(this.oUser.get_title() + " added.");
    }
    
    function onQueryFailed(sender, args) {
    
        alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
    }
    https://docs.microsoft.com/en-us/previous-versions/office/developer/sharepoint-2010/hh185012(v=office.14)#adding-a-user-to-a-sharepoint-group-using-javascript


    Please remember to mark the replies as answers if they helped.
    Linyu - SharePoint Engineer
    QQ Group: 72637444

    Thursday, December 13, 2018 7:01 AM
  • Also, you can try it by REST API:

    executor.executeAsync({
      url: "<app web url>/_api/SP.AppContextSite(@target)/web
        /sitegroups(7)/users
        ?@target='<host web url>'",
      method: "POST",
      body: "{ '__metadata': { 'type': 'SP.User' }, 'LoginName':'i:0#.w|domain\\user' }",
      headers: {
        "accept": "application/json; odata=verbose",
        "content-type": "application/json; odata=verbose"
      },
      success: successHandler,
      error: errorHandler
    });
    https://msdn.microsoft.com/en-us/library/office/dn531432.aspx?f=255&MSPPError=-2147217396#bk_UserCollection


    Please remember to mark the replies as answers if they helped.
    Linyu - SharePoint Engineer
    QQ Group: 72637444

    Thursday, December 13, 2018 7:02 AM
  • Hi,

    We can’t access cross site collection in SharePoint 2010(I did test in my local).

    Here is one thread for your reference.

    https://sharepoint.stackexchange.com/questions/93653/can-we-access-list-from-other-site-collection-sites-using-javascript-object-mode

    For such administration job, I would suggest you use PowerShell instead.

    Set-SPUser -Identity $User -Web $siteCollUrl -Group $group

    Here are some links for your reference.

    https://blogs.technet.microsoft.com/sp/?p=83

    https://manasbhardwaj.net/add-users-sharepoint-group-using-powershell/

    Best Regards,

    Lee


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


    Click here to learn more. Visit the dedicated forum to share, explore and talk to experts about Microsoft Teams.

    Thursday, December 13, 2018 7:31 AM
  • thanks linyus for your reply.I already tried this but this works for only one site collection i want to do this for all site collectios i need to retrieve all site collections in a web application  usng jsom.Please help.
    Thursday, December 13, 2018 8:07 AM
  • Hi,

    The solution shared by Linyus can be used for SharePoint 2013, while for SharePoint 2010, we can’t access cross site collection by client side JS.

    So I would suggest you use PowerShell instead.

    If you can do this from client side only, you could create a service for this requirement.

    Here is one thread for your reference.

    https://www.c-sharpcorner.com/uploadfile/gaikwaduc/hosting-wcf-in-sharepoint-2010/

    Best Regards,

    Lee


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


    Click here to learn more. Visit the dedicated forum to share, explore and talk to experts about Microsoft Teams.

    Monday, December 17, 2018 2:38 AM