none
Create a SP Group in a site using Client Object Model (CSOM) or Rest Service

    Question

  • Hi,

    Want to create a sp group using either client object model CSOM or REST Service.

    I found a link from MSDN which says we can create a sp group using CSOM- http://msdn.microsoft.com/en-us/library/jj246523.aspx  , but it does not have any example (code).

    Please help.


    Please remember to click 'Mark as Answer' on the post that helps you or "Vote As Helpful"

    Friday, May 10, 2013 5:16 PM

Answers

  • function createSPGroups() {
        //Load new Site
        var currentCTX = new SP.ClientContext();
        var currentWEB = currentCTX.get_web();
    
        //Get all groups in site
        var groupCollection = currentWEB.get_siteGroups();
    
        // Create Group information for a Group
        var membersGRP = new SP.GroupCreationInformation();
        membersGRP.set_title('My group');
        membersGRP.set_description('Use this group to grant people contribute permissions to the SharePoint site: ');
    
        //add  group
        oMembersGRP = currentWEB.get_siteGroups().add(membersGRP);
    
        //Get Role Definition by name (http://msdn.microsoft.com/en-us/library/jj246687.aspx)
        //return SP.RoleDefinition object
        var rdContribute = currentWEB.get_roleDefinitions().getByName('Contribute');
    
        // Create a new RoleDefinitionBindingCollection.
        var collContribute = SP.RoleDefinitionBindingCollection.newObject(currentCTX);
    
        // Add the role to the collection.
        collContribute.add(rdContribute);
    
        // Get the RoleAssignmentCollection for the target web.
        var assignments = currentWEB.get_roleAssignments();
    
        // assign the group to the new RoleDefinitionBindingCollection.
        var roleAssignmentContribute = assignments.add(oMembersGRP, collContribute);
    
        currentCTX.load(oMembersGRP);
        currentCTX.executeQueryAsync(Function.createDelegate(this, AddUsers), Function.createDelegate(this, this.onQueryFailed));
    }

    Also see this post:

    http://yeshagrawal.blogspot.in/2013/05/create-sp-group-and-assign-permission.html


    Please remember to click 'Mark as Answer' on the post that helps you or "Vote As Helpful"


    • Marked as answer by Yesh Agrawal Wednesday, May 22, 2013 12:20 PM
    • Edited by Yesh Agrawal Friday, June 14, 2013 4:07 PM
    Wednesday, May 22, 2013 12:19 PM

All replies

  • There's a CSOM sample here in the 2010 Dev forum:  http://social.msdn.microsoft.com/Forums/en-US/sharepointdevelopmentprevious/thread/3362074c-38d4-400a-a388-0cd0de0c8ae4/

    As you can see it only displays the essential group creation code. You need to add a ref to the ClientContext and add the call  ExecuteQueryAsync  yourself.


    Kind Regards

    Bjoern
    http://www.sharepointviking.com
    Twitter: Follow @bjoern_rapp

    Friday, May 10, 2013 5:55 PM
  • Thanks for reply,

    How about adding a permission level to SP Group. (i want to assign Contribute permission)

    That is not defined in the code or whether it is possible using CSOM or not?


    Please remember to click 'Mark as Answer' on the post that helps you or "Vote As Helpful"

    Friday, May 10, 2013 6:58 PM
  • function createSPGroups() {
        //Load new Site
        var currentCTX = new SP.ClientContext();
        var currentWEB = currentCTX.get_web();
    
        //Get all groups in site
        var groupCollection = currentWEB.get_siteGroups();
    
        // Create Group information for a Group
        var membersGRP = new SP.GroupCreationInformation();
        membersGRP.set_title('My group');
        membersGRP.set_description('Use this group to grant people contribute permissions to the SharePoint site: ');
    
        //add  group
        oMembersGRP = currentWEB.get_siteGroups().add(membersGRP);
    
        //Get Role Definition by name (http://msdn.microsoft.com/en-us/library/jj246687.aspx)
        //return SP.RoleDefinition object
        var rdContribute = currentWEB.get_roleDefinitions().getByName('Contribute');
    
        // Create a new RoleDefinitionBindingCollection.
        var collContribute = SP.RoleDefinitionBindingCollection.newObject(currentCTX);
    
        // Add the role to the collection.
        collContribute.add(rdContribute);
    
        // Get the RoleAssignmentCollection for the target web.
        var assignments = currentWEB.get_roleAssignments();
    
        // assign the group to the new RoleDefinitionBindingCollection.
        var roleAssignmentContribute = assignments.add(oMembersGRP, collContribute);
    
        currentCTX.load(oMembersGRP);
        currentCTX.executeQueryAsync(Function.createDelegate(this, AddUsers), Function.createDelegate(this, this.onQueryFailed));
    }

    Also see this post:

    http://yeshagrawal.blogspot.in/2013/05/create-sp-group-and-assign-permission.html


    Please remember to click 'Mark as Answer' on the post that helps you or "Vote As Helpful"


    • Marked as answer by Yesh Agrawal Wednesday, May 22, 2013 12:20 PM
    • Edited by Yesh Agrawal Friday, June 14, 2013 4:07 PM
    Wednesday, May 22, 2013 12:19 PM