3

I need to create site in SharePoint Online from SharePoint hosted app. I used code like this:

var webCreationInfo = new SP.WebCreationInformation();
        webCreationInfo.set_webTemplate('STS#0');

But now I have a problem because I need use my own template. How can I get code for own template in SharePoint Online?

  • 1
    Did you try joranmarkx.wordpress.com/2013/07/18/… – Aanchal Sep 29 '14 at 9:16
  • @Aanchal Thank you. I think it would be working, but i don't know how to get feature id? Could you tell it to me ? – OleksiiYatsenko Sep 29 '14 at 10:00
  • How are you deploying your custom template? – Aanchal Sep 29 '14 at 10:19
  • @Aanchal i created site and than saved it like template. After it i choose this template when create new site. – OleksiiYatsenko Sep 29 '14 at 10:38
3

If you like to use the title of your template, you could get it with an extra call to the the getAvailableWebTemplates on the web. Something like this:

var createSubSite = function(templateName) {
        var webCreationInfo = new SP.WebCreationInformation();
         webCreationInfo.set_title(dataForSiteCreate[i].clientInfo.ClientName);
        webCreationInfo.set_description(dataForSiteCreate[i].clientInfo.ClientDescription);
        webCreationInfo.set_language(1033);            
        webCreationInfo.set_url(hostname);
        webCreationInfo.set_useSamePermissionsAsParentSite(true);
        webCreationInfo.set_webTemplate(templateName);

        var clientContext = SP.ClientContext.get_current();

        var newWeb = clientContext.get_web().get_webs().add(webCreationInfo);

        clientContext.load(newWeb);
        clientContext.executeQueryAsync(function() {
            console.log("Done");
            }, function(sender,args) { 
            console.log("failed. Message:" + args.get_message());
        });     
}

var getTemplateName = function(templateName) {
    var dfd = new $.Deferred(); 
    var clientContext = SP.ClientContext.get_current();
    var templates = clientContext.get_web().getAvailableWebTemplates(1033, false);
    clientContext.load(templates);

    clientContext.executeQueryAsync(function() {
        var templateGuidName;

        for (var template in templates.get_data()) {
                if (templates.itemAt(template).get_title() === templateName) {
                    templateGuidName = templates.itemAt(template).get_name();
                    break;
                }
            }   
        dfd.resolve(templateGuidName);

     }, function() { dfd.reject(); });

    return dfd.promise();

}

SP.SOD.executeFunc("sp.js", "SP.ClientContext", function () {
    getTemplateName("TitleOfYourTemplateHere").done(function(template) {
        createSubSite(template);    
    }); 
});

Some error handling etc is needed, but it should work. I have used jquery deferred in the example, so that's needed.

1

Using Custom Template:

function CreateWebsite(title, description, webUrl, templateTitle)
{    
    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.');
            }, 
            function(sender, args){ 
                alert(args.get_message());
                });
        }, 
        function(sender, args){ 
            alert(args.get_message())
        }
    );
}

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.