0

I want to add a user group to sharepoint subsite using javascript. I have used follwing code and it added a user group to subsite(ABS). and it also added user group to the parent site(OMSTest) which is a root sitecollection. why is that?

function CreateUserGroups(url)  
{  

    var siteUrl = 'https://xxxx.sharepoint.com/sites/OMSTest/ABS';
    var context = new SP.ClientContext(siteUrl);
    var currentWEB = context.get_web();     
    var membersGRP = new SP.GroupCreationInformation();  

    var membershipArray = []; 

    var collContribute = SP.RoleDefinitionBindingCollection.newObject(context); 
    var rdContribute = currentWEB.get_roleDefinitions().getByName("Contribute");  
    collContribute.add(rdContribute);   

    membersGRP.set_title("GROUPP1");  
    membersGRP.set_description("GROUPP1");  
    var oMembersGRP = currentWEB.get_siteGroups().add(membersGRP);

    oMembersGRP.set_allowMembersEditMembership(false);
    oMembersGRP.set_onlyAllowMembersViewMembership(false);  

    var assignments = currentWEB.get_roleAssignments();
    var roleAssignment = assignments.add(oMembersGRP, collContribute);



    oMembersGRP.update(); 

    membershipArray.push(oMembersGRP);          
    context.load(membershipArray[membershipArray.length - 1]);  
    //clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded2), Function.createDelegate(this, this.onQueryFailed2));

    context.executeQueryAsync(  
        function () {       

            alert("Group Created Successfully");  
        },  
        function (sender, args) {             

            alert("Failed to create groups " + args.get_message());              
        });
}
1

This is SharePoint's default behavior that group created at the subsite level also reflect on the root site. The reason for this is it that you can always reuse groups at different levels like sites, lists and libraries.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.