1
function createWebsite(siteName, site) {

    this.clientContext = typeof site !== 'undefined' ?  new SP.ClientContext(site) : SP.ClientContext.get_current();
    this.oWebsite = clientContext.get_web();

    var webCreationInfo = new SP.WebCreationInformation();
    webCreationInfo.set_title(siteName);
    webCreationInfo.set_description(siteName);
    webCreationInfo.set_language(1033);
    webCreationInfo.set_url(siteName);
    webCreationInfo.set_useSamePermissionsAsParentSite(false);
    webCreationInfo.set_webTemplate('template here');

    oWebsite.get_webs().add(webCreationInfo);
    oWebsite.update();


    function onQuerySucceeded(sender, args){
        alert("Site made");
    }

    function onQueryFailed(sender, args){
        //alert(args.get_message());
    }

    clientContext.executeQueryAsync(Function.createDelegate(this ,onQuerySucceeded), Function.createDelegate(this, onQueryFailed));
}

This function creates a subsite to the specified site, I'm wondering how i enable the creation of default permission groups. That is when i create a subsite manually and decide not to inherit permissions from top site, SharePoint automatically creates three default groups for me (visitor, member, owner). How is this done in Javascript?

3 Answers 3

3

You should manually create those groups and assign permission as below

var groupCreationInfo = new SP.GroupCreationInformation();
groupCreationInfo.set_title(siteName + ' Members');
groupCreationInfo.set_description('Member Group');
this.oGroup = oWebsite.get_siteGroups().add(groupCreationInfo);
var collRoleDefinitionBinding = SP.RoleDefinitionBindingCollection.newObject(clientContext);
this.oRoleDefinition = oWebsite.get_roleDefinitions().getByType(SP.RoleType.contributor);
collRoleDefinitionBinding.add(oRoleDefinition);
var collRollAssignment = oWebsite.get_roleAssignments();
collRollAssignment.add(oGroup, collRoleDefinitionBinding);
clientContext.load(oGroup, 'Title');
clientContext.load(oRoleDefinition, 'Name');
clientContext.executeQueryAsync(
    Function.createDelegate(this, this.onCreateGroupQuerySucceeded), 
    Function.createDelegate(this, this.onQueryFailed));
3
  • What are you specifying in these lines? clientContext.load(oGroup, 'Title'); clientContext.load(oRoleDefinition, 'Name'); Commented Nov 9, 2014 at 18:01
  • @ThomasLindauer its same as 'Include(Title'). I got that script from msdn Commented Nov 9, 2014 at 18:06
  • This certainly creates a group, but how do i set this group to a site and how do i specify to put persons in it? Commented Nov 10, 2014 at 7:04
2

It seems the only missing part in your example is assigning permissions to the Web site. Use

Example

var createTeamSite = function(siteName,success,error) {

    var ctx = SP.ClientContext.get_current();
    var parentWeb = ctx.get_web();

    var info = new SP.WebCreationInformation();
    info.set_title(siteName);
    info.set_description(siteName);
    info.set_language(1033);
    info.set_url(siteName);
    info.set_useSamePermissionsAsParentSite(false);
    info.set_webTemplate('STS');

    var web = parentWeb.get_webs().add(info);
    parentWeb.update();
    ctx.executeQueryAsync(
        function(){
            success(web);
        }, error);
}





function logError(sender,args) {
    console.log(args.get_message()); 
}

//usage
createTeamSite('Team Site',
  function(web)
  {
     //Assign pemissions from root web 
     var ctx = web.get_context();
     var rootWeb = ctx.get_site().get_rootWeb(); 
     var ownerGroup = rootWeb.get_associatedOwnerGroup();
     var memberGroup = rootWeb.get_associatedMemberGroup();
     var visitorGroup = rootWeb.set_associatedVisitorGroup();

     web.set_associatedOwnerGroup(ownerGroup);
     web.set_associatedMemberGroup(memberGroup);
     web.set_associatedVisitorGroup(visitorGroup);
     web.update();
     ctx.executeQueryAsync(
        function(){
            console.log(String.format('{0} site has been created',web.get_title()));
        }, 
        logError);
  }
  ,logError);
2
  • Please read question again. >That is when i create a subsite manually and decide not to inherit permissions from top site, SharePoint automatically creates three default groups for me (visitor, member, owner). How is this done in Javascript? Commented Nov 9, 2014 at 17:14
  • I have to mention, the line parentWeb.update(); is not really needed for the website to be created, but then, associating groups would fail! Thanks!
    – JohnPan
    Commented Mar 17, 2015 at 9:02
2

The code below 1. Creates a website 2. Creates three NEW groups (visitors, members, onwers) if must not inherit permissions 3. Binds permissions for these groups 4. Associates the groups with web's properties

function createWeb(weburl, webname, webdesc, template, inheritBool) {

var latestNewWeb,
    latestNewGroup,
    clientContext = new SP.ClientContext.get_current(),
    defaultCallback = oncreateWebsiteSucceeded,
    parentWeb = clientContext.get_web(),
    collWeb = parentWeb.get_webs(),
    webCreationInfo = new SP.WebCreationInformation();

webCreationInfo.set_title(webname);
webCreationInfo.set_url(weburl);
webCreationInfo.set_description(webdesc);
webCreationInfo.set_webTemplate(template);
webCreationInfo.set_useSamePermissionsAsParentSite(inheritBool);
latestNewWeb = collWeb.add(webCreationInfo);
if (!inheritBool) {
    defaultCallback = createDefaultGroups;
}
parentWeb.update();
clientContext.executeQueryAsync(defaultCallback, oncreateWebsiteFailed);

function createDefaultGroups() {
    createVisitors();
    // all other group creations will occur as callbacks;
}

function createVisitors() {
    createGroup(
        "Visitors", "Visitors Group",
        SP.RoleType.reader, "set_associatedVisitorGroup",
        createMembers, oncreateWebsiteFailed
    );
}

function createMembers() {
    createGroup(
        "Members", "Members Group",
        SP.RoleType.contributor, "set_associatedMemberGroup",
        createOwners, oncreateWebsiteFailed
    );
}

function createOwners() {
    createGroup(
        "Owners", "Owners Group",
        SP.RoleType.administrator, "set_associatedOwnerGroup",
        oncreateWebsiteSucceeded, oncreateWebsiteFailed
    );
}

function createGroup(title, description, SPRoleType, assocFn, callbackOk, callbackHell) {

    var groupCreationInfo = new SP.GroupCreationInformation(),
        collRoleDefinitionBinding,
        oRoleDefinition,collRollAssignment;

    groupCreationInfo.set_title(webname + " " + title);
    groupCreationInfo.set_description(description);
    latestNewGroup = latestNewWeb.get_siteGroups().add(groupCreationInfo);
    collRoleDefinitionBinding = SP.RoleDefinitionBindingCollection.newObject(clientContext);
    oRoleDefinition = latestNewWeb.get_roleDefinitions().getByType(SPRoleType);
    collRoleDefinitionBinding.add(oRoleDefinition);
    collRollAssignment = latestNewWeb.get_roleAssignments();
    collRollAssignment.add(latestNewGroup, collRoleDefinitionBinding);
    clientContext.load(latestNewGroup);
    clientContext.load(oRoleDefinition, 'Name');

    clientContext.executeQueryAsync(
        function () {
            assocGroup(assocFn, callbackOk, callbackHell)
        },
        callbackHell
    );
}

function assocGroup(assocFn, callbackOk, callbackHell) {
    latestNewWeb[assocFn](latestNewGroup);
    latestNewWeb.update();
    clientContext.executeQueryAsync(callbackOk, callbackHell);
}

function oncreateWebsiteSucceeded() {
    say("Created Web site: " + weburl);
}

function oncreateWebsiteFailed(sender, args) {
    say('Fail. ' + weburl + " --- " + args.get_message() + '\n' + args.get_stackTrace());
}}
1
  • just a notice, someone could prefer using groupCreationInfo.set_title(weburl + " " + title); instead of groupCreationInfo.set_title(webname + " " + title); I know I would!
    – JohnPan
    Commented Apr 1, 2015 at 10:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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