In this article we can able to see how to create a sub site in SharePoint using Client object model.
Create a custom page and add the below code in the Script Editor Web part.
<html>
<head>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.12.3.js"></script>
<script type="text/javascript" src="https://ajax.aspnetcdn.com/ajax/4.0/1/MicrosoftAjax.js"></script>
<script src="../_layouts/15/sp.runtime.js" type="text/javascript"> </script>
<script type="text/javascript">
$(function() {
bindButtonClick();
});
function bindButtonClick() {
$("#register").on("click", function() {
var name = $("#txtSiteTitle").val();
var id = $("#txtSiteDescription").val();
var URL = $("#txtSiteURL").val();
if (name == '' || id == '' || URL == '') {
alert("Please fill all fields...!!!!!!");
} else {
createSite();
}
});
}
function createSite() {
var siteTitle = $("#txtSiteTitle").val();
var siteDesc = $("#txtSiteDescription").val();
var siteUrl = $("#txtSiteURL").val();
var clientContext = new SP.ClientContext('https://rptj.sharepoint.com/Test/');
var collWeb = clientContext.get_web().get_webs();
var webCreationInfo = new SP.WebCreationInformation();
webCreationInfo.set_title(siteTitle);
webCreationInfo.set_description(siteDesc);
webCreationInfo.set_language(1033);
webCreationInfo.set_url(siteUrl);
webCreationInfo.set_useSamePermissionsAsParentSite(true);
webCreationInfo.set_webTemplate('STS#0');
var oNewWebsite = collWeb.add(webCreationInfo);
clientContext.executeQueryAsync(Function.createDelegate(this, onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
}
function onQuerySucceeded() {
alert('Site successfully Created!');
}
function onQueryFailed(sender, args) {
alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}
</script>
</head>
<body>
<div class="container">
<div class="main">
<form class="form" method="post" action="#">
<h2>Create Client sub Site</h2>
<label>Client Site Name :</label> <input type="text" name="dname" id="txtSiteTitle">
<label>Client SIte Description:</label> <input type="text" name="demail" id="txtSiteDescription">
<label>Client SIte URL:</label> <input type="text" name="durl" id="txtSiteURL">
<input type="button" name="register" id="register" value="Submit"> </form>
</div>
</body>
</html>
Create a custom page and add the below code in the Script Editor Web part.
<html>
<head>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.12.3.js"></script>
<script type="text/javascript" src="https://ajax.aspnetcdn.com/ajax/4.0/1/MicrosoftAjax.js"></script>
<script src="../_layouts/15/sp.runtime.js" type="text/javascript"> </script>
<script type="text/javascript">
$(function() {
bindButtonClick();
});
function bindButtonClick() {
$("#register").on("click", function() {
var name = $("#txtSiteTitle").val();
var id = $("#txtSiteDescription").val();
var URL = $("#txtSiteURL").val();
if (name == '' || id == '' || URL == '') {
alert("Please fill all fields...!!!!!!");
} else {
createSite();
}
});
}
function createSite() {
var siteTitle = $("#txtSiteTitle").val();
var siteDesc = $("#txtSiteDescription").val();
var siteUrl = $("#txtSiteURL").val();
var clientContext = new SP.ClientContext('https://rptj.sharepoint.com/Test/');
var collWeb = clientContext.get_web().get_webs();
var webCreationInfo = new SP.WebCreationInformation();
webCreationInfo.set_title(siteTitle);
webCreationInfo.set_description(siteDesc);
webCreationInfo.set_language(1033);
webCreationInfo.set_url(siteUrl);
webCreationInfo.set_useSamePermissionsAsParentSite(true);
webCreationInfo.set_webTemplate('STS#0');
var oNewWebsite = collWeb.add(webCreationInfo);
clientContext.executeQueryAsync(Function.createDelegate(this, onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
}
function onQuerySucceeded() {
alert('Site successfully Created!');
}
function onQueryFailed(sender, args) {
alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}
</script>
</head>
<body>
<div class="container">
<div class="main">
<form class="form" method="post" action="#">
<h2>Create Client sub Site</h2>
<label>Client Site Name :</label> <input type="text" name="dname" id="txtSiteTitle">
<label>Client SIte Description:</label> <input type="text" name="demail" id="txtSiteDescription">
<label>Client SIte URL:</label> <input type="text" name="durl" id="txtSiteURL">
<input type="button" name="register" id="register" value="Submit"> </form>
</div>
</body>
</html>