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