Please allow me to demonstrate how to create SharePoint site programmatically.
Important things to understand when working with apps for SharePoint are host webs and app webs:
- Host web is a SharePoint site where the app is installed
- App web is a special isolated site where the app for SharePoint internal components and content are deployed
var hostweburl = decodeURIComponent(getQueryStringParameter("SPHostUrl"));<br />
var appweburl = decodeURIComponent(getQueryStringParameter("SPAppWebUrl"));
The following method is used to create SharePoint site:
function createSite(siteUrl, fullSiteName){<br />
var clientContext =new SP.ClientContext(appweburl);<br />
var appContextSite =new SP.AppContextSite(clientContext, hostweburl);<br />
var webs = appContextSite.get_web().get_webs();
var webCreationInfo =new SP.WebCreationInformation();<br />
webCreationInfo.set_title(fullSiteName);<br />
webCreationInfo.set_description('Web site description...');<br />
webCreationInfo.set_language(1033);<br />
webCreationInfo.set_url(siteUrl);<br />
webCreationInfo.set_useSamePermissionsAsParentSite(false);<br />
webCreationInfo.set_webTemplate('{809C73E7-591B-41C2-8DCD-C4B18110BE1D}');<br />
var site = webs.add(webCreationInfo);
clientContext.load(site);<br />
clientContext.executeQueryAsync(successHandler, onFailed);
function successHandler(){<br />
setSiteNavigation(fullSiteName, siteUrl);<br />
createSPGroups(fullSiteName, siteiteUrl);<br />
}<br />
}
As you’ve noticed there are a couple of methods that are called in the code above. Let’s explain each of them:
function setSiteNavigation(siteTitle, siteUrl){<br />
var absoluteSiteUrl = hostweburl +"/"+ siteUrl;<br />
var context =new SP.ClientContext(appweburl);<br />
var appContextSite =new SP.AppContextSite(context, absoluteSiteUrl);<br />
var web = appContextSite.get_web();
var webNavSettings =new SP.Publishing.Navigation.WebNavigationSettings(context, web);
var navigation = webNavSettings.get_globalNavigation();
navigation.set_source(3);
webNavSettings.update();
context.executeQueryAsync(successHandler, onFailed);
function successHandler(){
}<br />
}
- The following method facilitates creation of a site global navigation.
- createSPGroup method explained in another blog article.
Happy coding!
The post Creating a Site in SharePoint Hosted Apps Programmatically appeared first on Merit Solutions.