0

I have written JavaScript to create sub site using custom template. Now it is needed to have unique permission for those new sub site created. Is it possible to do this using code at the time of creating subsite.

Below is my code:-

 var context = new SP.ClientContext.get_current();
    var web = context.get_web();
    context.load(web);
    var webTemplates = web.getAvailableWebTemplates(1033,false);
    context.load(webTemplates);
    context.executeQueryAsync(function(){
            var enumerator = webTemplates.getEnumerator();
            var customTemplate;
            while(enumerator.moveNext())
            {
                var webTemplate = enumerator.get_current();
                var webTitle = webTemplate.get_title();
                if(webTitle == templateTitle) // templateTitle = Custom Tempalte Title
                {
                    customTemplate = webTemplate.get_name();
                    break;
                }
            }                
            var webCreationInformation = new SP.WebCreationInformation();
            webCreationInformation.set_title(title);
            webCreationInformation.set_description(description);
            webCreationInformation.set_language(1033);
            webCreationInformation.set_url(webUrl);
            webCreationInformation.set_useSamePermissionsAsParentSite(true);
            webCreationInformation.set_webTemplate(customTemplate);
            web.get_webs().add(webCreationInformation);
            context.executeQueryAsync(function(){
                //alert('Website created successfully.');

            }, 

I have also tried with changing the value of webCreationInformation.set_useSamePermissionsAsParentSite(true); to false.

  • is it not working ? – Gautam Sheth Apr 18 '17 at 10:44
  • I think I also need to set groups after setting value false. – KumarV Apr 18 '17 at 10:46
1

After stop inheritance of a new sub-site, create a group in the same way that you create most other objects through the client object model, use the GroupCreationInformation object to define a new group, and then use the add(parameters) function to add the group to the collection of site groups in a site collection. And just as you assign users to a role, you assign a group to a particular role on a website by creating a RoleDefinitionBindingCollection object for the website context, adding a role definition to the binding collection object, and then using the add(principal, roleBindings) function to add the group together with the binding collection to the role assignments for the website. The following example creates a group and assigns it the Contribute role on the website.

var siteUrl = '/sites/MySiteCollection/MyWebSite';

function createGroupAddToRole() {

    var clientContext = new SP.ClientContext(siteUrl);
    this.oWebsite = clientContext.get_web();

    var groupCreationInfo = new SP.GroupCreationInformation();
    groupCreationInfo.set_title('My New GroupT');
    groupCreationInfo.set_description('Description of new 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.onQuerySucceeded), 
        Function.createDelegate(this, this.onQueryFailed));
}

function onQuerySucceeded() {

    var roleInfo = oGroup.get_title() + ' created and assigned to ' + oRoleDefinition.get_name();
    alert(roleInfo);
}

function onQueryFailed(sender, args) {

    alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}

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.