none
Add users to group RRS feed

  • Question

  • Hi all,

    I have used below code to add users to group in sharepoint successfully.

    <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>

    But in my environment my login name is like i:0#.w|test|kkumar.with out passing  domain name how to add users.Please help.

    regards,
    Praveen
    Thursday, December 20, 2018 9:55 AM

All replies

  • Hi,

    you cannot hard code the domain name in code and pass only username?


    Thursday, December 20, 2018 10:59 AM
  • if iam passing only username example in my case kkumar but its not adding to the group.
    Thursday, December 20, 2018 11:13 AM
  • You can append the domain in your code right? like below

    function doSomething(userName)

    {

    var validName="domainName\"+userName;

    }



    Thursday, December 20, 2018 11:17 AM
  • iam getting security validation error with full control permissions as well as iam following instructions in the below url

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

    but its not a good practice.please suggest any other solution.thaks in advance.

    Thursday, December 20, 2018 11:38 AM
  • iam facing security validation for this page is invalid error.please suggest if any solution.Thanks in advance.
    Thursday, December 20, 2018 12:49 PM
  • Hi Praveenrangu,

    SPSecurity.RunWithElevatedPrivileges is used in Server Object Model, as your code is JavaScript Client Object Model, this is not the right way to fix this error.

    And let's back to the original question, as Ulas's suggestion, you could use "Domain\"+username for the login name.

    If this is helpful to the question, I would suggest to Mark as answer so that it could help others who stuck with the same question in the forum.

    About the new error occurred, I would suggest you could create a new thread which will focus on the new question.

    Thanks

    Best Regards


    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.

    Friday, December 21, 2018 8:27 AM
  • Hi,

     

    I am checking to see how things are going there on this issue. Please let us know if you would like further assistance.

     

    You can mark the helpful post as answer to help other community members find the helpful information quickly.

     

    Thanks

     

    Best Regards


    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 31, 2018 8:47 AM