In this post, we would see on how to create a SharePoint 2013 site using the SharePoint 2013 RESTful endpoints available in HostWeb. I would be using a SharePoint hosted app to demonstrate this scenario.
File –> New –> Office/SharePoint –> Apps and name it as ‘CreateSiteApp’.
In the SharePoint hosted app, I’d be making a call to the /_api/web/webinfos/add available in HostWeb to create a sub-site under HostWeb.
Open App.js file.
Copy and paste the following code. The following snippet leverages Cross Domain library of SharePoint 2013 to make
'use strict'; var context = SP.ClientContext.get_current(); var user = context.get_web().get_currentUser(); var HostWebUrl; var AppWebUrl; // This code runs when the DOM is ready and creates a context object which is needed to use the SharePoint object model $(document).ready(function () { HostWebUrl = decodeURIComponent( RetrieveQueryStringParameter("SPHostUrl") ); AppWebUrl = decodeURIComponent( RetrieveQueryStringParameter("SPAppWebUrl") ); var scriptbase = HostWebUrl + "/_layouts/15/"; $.getScript(scriptbase + "SP.RequestExecutor.js", execCrossDomainRequest); }); function RetrieveQueryStringParameter(ParamsforRetrieval) { var params = document.URL.split("?")[1].split("&"); var strParams = ""; for (var i = 0; i < params.length; i = i + 1) { var singleParam = params[i].split("="); if (singleParam[0] == ParamsforRetrieval) return singleParam[1]; } } function execCrossDomainRequest() { var executor = new SP.RequestExecutor(AppWebUrl); executor.executeAsync( { url: AppWebUrl +"/_api/web/webinfos/add", type: "POST", data: JSON.stringify( { 'parameters': { '__metadata': { 'type': 'SP.WebInfoCreationInformation' }, 'Url': 'SundarSite', 'Title': 'SundarSite', 'Description': 'Site created using REST', 'Language': 1033, 'WebTemplate': 'sts', 'UseUniquePermissions': false } } ), headers: { "accept": "application/json; odata=verbose", "content-type": "application/json;odata=verbose", "content-length": 1028, "X-RequestDigest": $("#__REQUESTDIGEST").val() }, success: successHandler, error: errorHandler } ); } function successHandler(data) { document.getElementById("message").innerText = "Site Created Successfully"; } function errorHandler(data, errorCode, errorMessage) { document.getElementById("message").innerText = "Could not complete cross-domain call " + errorMessage; }
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, “Courier New”, courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, “Courier New”, courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, “Courier New”, courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }