7

My using following script to create Sub site.

<script 
    src="//ajax.aspnetcdn.com/ajax/4.0/1/MicrosoftAjax.js" <br/>
    type="text/javascript"><br/>
</script><br/>
<script<br/>
    type="text/javascript" <br/>
    src="/_layouts/15/sp.runtime.js"><br/>
</script><br/>
<script <br/>
    type="text/javascript" <br/>
    src="/_layouts/15/sp.js"><br/>
</script><br/>

<script type="text/javascript"><br/>
function myFunction()<br/>
{<br/>
alert('started');<br/>

var siteUrl = " ";<br/>

//get client context and root web<br/>
//var clientContext = new SP.ClientContext.get_current();<br/>
//var rootWeb = clientContext.site.rootWeb();<br/>
var clientContext = new SP.ClientContext(siteUrl);<br/>
var rootWeb = clientContext.site.rootWeb();<br/>
this.clientContext.load(rootWeb);<br/>
this.clientContext.executeQuery();<br/>

//set creation info<br/>
var WCI = new SP.WebCreationInformation();<br/>
WCI.set_webTemplate('sts#0');<br/>
WCI.set_description('Created Using CSOM');<br/>
WCI.set_title('Custom SubSite');<br/>
WCI.set_url(url);<br/>
WCI.set_useSamePermissionsAsParentSite(true);<br/>
WCI.set_language(1033);<br/>
this.rootWeb.get_webs().add(WCI);<br/>
this.rootWeb.update();<br/>

// save site and set callbacks<br/>
this.clientContext.load(this.rootWeb);<br/>
this.clientContext.executeQueryAsync(<br/>
Function.createDelegate(this, this.OnSiteCreationSuccess),<br/>
Function.createDelegate(this, this.Error));<br/>
}<br/>
</script><br/>

<p>Click the button to trigger a function.</p>

<button onclick="myFunction()">Click me</button>

</div>

My Facing following error.

  • What url is undefined [WCI.set_url(url);]
  • Script are not getting loaded MicrosoftAjax.js,sp.runtime.js and sp.js
  • This script i have added inside script viewer web part SharePoint 2013.

Kindly provide me proper approach to create subsite using javascript object module.

  • Are you looking for JSOM or CSOM? – Stephen Jan 22 '14 at 12:43
  • Unable to get property 'get_webs' of undefined or null reference" any idea – user42744 May 27 '15 at 12:23
13

For JSOM Something like this would work...however you need to request full trust for your app.

function createWebsite() {

var clientContext = new SP.ClientContext('your url');
this.oWebsite = clientContext.get_web();

var webCreationInfo = new SP.WebCreationInformation();
webCreationInfo.set_title('My New Web Site');
webCreationInfo.set_description('Description of new Web site...');
webCreationInfo.set_language(1033);
webCreationInfo.set_url('MyNewWebSite');
webCreationInfo.set_useSamePermissionsAsParentSite(true);
webCreationInfo.set_webTemplate('mall');

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

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

You can load the JS files in the default.aspx page like this in the PlaceholderMain.

<SharePoint:ScriptLink ID="ScriptLink5" name="sp.js" runat="server" LoadAfterUI="true" Localizable="false" />
<SharePoint:ScriptLink ID="ScriptLink6" name="sp.runtime.js" runat="server" LoadAfterUI="true" Localizable="false" />
<SharePoint:ScriptLink ID="ScriptLink7" name="sp.core.js" runat="server" LoadAfterUI="true" Localizable="false" />

and i tend to add the ajax in the head.

 <script type="text/javascript" src="https://ajax.aspnetcdn.com/ajax/4.0/1/MicrosoftAjax.js"></script>

S

  • what should i put in place of "MyNewWebSite" webCreationInfo.set_url('MyNewWebSite'); – Rahul Barua Jan 22 '14 at 13:14
  • whatever you want to call it...no spaces – Stephen Jan 22 '14 at 14:10
  • it worked like charm. – Rahul Barua Jan 22 '14 at 15:13
  • 1
    Worked for mee too ! I put the JS Files references in the master page because I am not able to modify the default.aspx. Is that normal ? But I have a problem with the site tempalte. How can I apply a customized site template i already created (Available as Solution) ? When I put its name in webCreationInfo.set_webTemplate('MyTemplate'); I got an error (Invalid arguments for the site template) ... – M3HD1 Dec 1 '14 at 10:31
  • 1
    @GennadyG, Please find my answer to create subsite using Custom tempate.. – SID Mar 31 '17 at 5:36
3

You can do it with REST API but be careful with user permission.

    $.ajax({
        url: "/_api/web/webinfos/add",
        type: "POST",
        headers: {
            "accept": "application/json;odata=verbose",
            "content-type": "application/json;odata=verbose",
            "X-RequestDigest": $("#__REQUESTDIGEST").val()
        },
        data: JSON.stringify({
            'parameters': {
                '__metadata': {
                    'type': 'SP.WebInfoCreationInformation'
                },
                'Url': <New SubSite URL>,
                'Title': <New SubSite Title>,
                'Description': 'Test',
                'Language': 1033,
                'WebTemplate': 'sts',
                'UseUniquePermissions': true
            }
        })           
    });
0

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())
        }
    );
}
-1
set_webTemplate("[guid-id feature]#MyTemplate")

ex.

set_webTemplate("{4a110786-9683-4734-97ca-f2eca95ca377}#MyTemplate");

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.