none
Loop all list items and add user to groups in all site collections RRS feed

  • Question

  • Hi all,

    I have user the below code to add user to one site coll.

     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());
    }

    I have a master list and all the sitecoll urls stotred in the list i want to get all sitecollurls from master list and loop the items and passing the sitecoll url and add users to group in all sitecollections. please help me.

    Thanks in advance.Iam using jsom and working on 2010.

    Regards,

    Praveen

    Friday, December 14, 2018 12:58 PM

Answers

  • Hi Praveen,

    In my test, my list name is "MyTestList" and field name is "siteCollUrl".

    The following code for your reference.

    <script type="text/javascript">
    SP.SOD.executeFunc('sp.js', 'SP.ClientContext', retrieveListItems);
    function retrieveListItems(){
    	var clientContext = new SP.ClientContext.get_current();
    	var oList = clientContext.get_web().get_lists().getByTitle('MyTestList');
    	var camlQuery = new SP.CamlQuery();	
    	this.collListItem = oList.getItems(camlQuery);
    	clientContext.load(collListItem);
    	clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded),Function.createDelegate(this, this.onQueryFailed));
    }
    function onQuerySucceeded(sender, args){	
    	var listItemEnumerator = collListItem.getEnumerator();
    	while (listItemEnumerator.moveNext()) {
    		var oListItem = listItemEnumerator.get_current();
    		var siteCollUrl=oListItem.get_item('siteCollUrl');
    		addUserToSharePointGroup(siteCollUrl);
    	}
    }            
    function addUserToSharePointGroup(siteUrl){
        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.onAddSucceeded), Function.createDelegate(this, this.onQueryFailed));
    
    }
    function onAddSucceeded() {
        console.log(this.oUser.get_title() + " added.");
    }
    function onQueryFailed(sender, args) {
        alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
    }
    </script>

    Best Regards,

    Dennis


    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.

    • Marked as answer by Praveenrangu Thursday, December 20, 2018 8:58 AM
    Monday, December 17, 2018 8:47 AM
    Moderator

All replies

  • Hi

    https://social.technet.microsoft.com/Forums/en-US/a1853212-62ac-4a15-a47f-e27bc1c35187/hot-to-get-list-which-is-located-in-different-site-collection-using-ecmascript

    I am afraid you cannot do this using JSOM.


    Justin Liu Office Servers and Services MVP, MCSE
    Senior Software Engineer
    Please Vote and Mark as Answer if it helps you.

    Monday, December 17, 2018 2:47 AM
  • Hi Praveen,

    In my test, my list name is "MyTestList" and field name is "siteCollUrl".

    The following code for your reference.

    <script type="text/javascript">
    SP.SOD.executeFunc('sp.js', 'SP.ClientContext', retrieveListItems);
    function retrieveListItems(){
    	var clientContext = new SP.ClientContext.get_current();
    	var oList = clientContext.get_web().get_lists().getByTitle('MyTestList');
    	var camlQuery = new SP.CamlQuery();	
    	this.collListItem = oList.getItems(camlQuery);
    	clientContext.load(collListItem);
    	clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded),Function.createDelegate(this, this.onQueryFailed));
    }
    function onQuerySucceeded(sender, args){	
    	var listItemEnumerator = collListItem.getEnumerator();
    	while (listItemEnumerator.moveNext()) {
    		var oListItem = listItemEnumerator.get_current();
    		var siteCollUrl=oListItem.get_item('siteCollUrl');
    		addUserToSharePointGroup(siteCollUrl);
    	}
    }            
    function addUserToSharePointGroup(siteUrl){
        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.onAddSucceeded), Function.createDelegate(this, this.onQueryFailed));
    
    }
    function onAddSucceeded() {
        console.log(this.oUser.get_title() + " added.");
    }
    function onQueryFailed(sender, args) {
        alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
    }
    </script>

    Best Regards,

    Dennis


    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.

    • Marked as answer by Praveenrangu Thursday, December 20, 2018 8:58 AM
    Monday, December 17, 2018 8:47 AM
    Moderator
  • thanks for the rply.The error iam getting request failed security validation for this page is in valid and click back in the web browser refresh page try operation again.Undefined.below is my code.

    <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
     <script>

    <html>
    <input type='text' id='name'/>
    < input type='button' id='btnadd' onclick="test"/>Click me</button>
    </html>
     var siteUrl='';
    var deferred;
    function test() {
     
        ExecuteOrDelayUntilScriptLoaded(getvalues, "sp.js");
     
    });


     function getvalues() {  
            retrieveListItems().done(function()  
    {  
            console.log('Execute  second after the retrieve list items');   
    }).fail(function()  
    {  
     console.log('Execute  second after the retrieve list items  failed');   
    });             
        }  



     
    function retrieveListItems() {
        deferred=$.Deferred();
     
        var clientContext = new SP.ClientContext();
     
        var oList = clientContext.get_web().get_lists().getByTitle('sitecoll');
     
        var camlQuery = new SP.CamlQuery();
     
        this.collListItem = oList.getItems(camlQuery);
     
        clientContext.load(collListItem);
     
        clientContext.executeQueryAsync(
     
        Function.createDelegate(this, this.onQuerySucceeded),
     
        Function.createDelegate(this, this.onQueryFailed)
     
        );
    return deferred.promise(); 
     
    }
     
    function onQuerySucceeded(sender, args) {
     
        var listItemInfo = '';
     
        var listItemEnumerator = collListItem.getEnumerator();
       
     
        while (listItemEnumerator.moveNext()) {
     
            var oListItem = listItemEnumerator.get_current();
              siteUrl= oListItem.get_item('Url');
                AddUserToSharePointGroup();
               deferred.resolve(oListItem);
     
        }
     
        
     
    }
     
    function onQueryFailed(sender, args) {
     
        alert('Request failed. ' + args.get_message() +
     
        '\n' + args.get_stackTrace());
     
    }

    function AddUserToSharePointGroup() {  

            var clientContext = new SP.ClientContext.get_current(siteUrl);  
            var siteGroups = clientContext.get_web().get_siteGroups();  
            var group=siteGroups.getByid(3);
            var usercreationinfo=new SP.UserCreationInformation();
            usercreationinfo.setLoginName(document.getElemntById('name'))
            this.oUser=group.get_users.add(usercreationinfo)
            clientContextload(oUser);  
     

            clientContext.executeQueryAsync(onQuerySucceeded, onQueryFailed);  
        }  
      
        function onQuerySucceeded() {  
            alert('success');  
        }  
      
        function onQueryFailed() {  
            alert('Request failed.');  
        }   
    </script>

    thanks in advance.

    Monday, December 17, 2018 9:33 AM
  • Hi,

    Modify the code as below.

    <input type='button' id='btnadd' onclick="test()"/>Click me</button>

    Best Regards,

    Dennis


    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 9:48 AM
    Moderator
  • thanks for the reply dennis.Changed no luck.please help.
    Monday, December 17, 2018 10:05 AM
  • thanks for your help dennis.anyway i resolved the validation issue by changing the property in central admin as described below  url.

    http://sharepoint-works.blogspot.com/2012/04/security-validation-for-this-page-is.html.

    Monday, December 17, 2018 2:54 PM
  • Hi,

    Glad to hear that you solve this issue, thanks for your sharing.

    You can mark the reply as answer, it will make others who stuck with the similar issue easier to search for valid solutions in this forum.

    Best Regards,

    Dennis


    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.

    Tuesday, December 18, 2018 1:18 AM
    Moderator