Make sure to refer to the js files, given below, in Default.aspx page-
- <SharePoint:ScriptLink name="sp.js" runat="server" OnDemand="true" LoadAfterUI="true" Localizable="false" />
- <SharePoint:ScriptLink name="sp.runtime.js" runat="server" OnDemand="true" LoadAfterUI="true" Localizable="false" />
- <script type="text/javascript" src="../_layouts/15/sp.publishing.js"></script>
In app.js file, add the code, given below-
-
- var currentUser;
- var appCtxSite;
-
- var context = SP.ClientContext.get_current();
-
- $(document).ready(function () {
- console.log("page loading...");
-
- var hostWebUrl = decodeURIComponent(manageQueryStringParameter('SPHostUrl'));
- var appWebUrl = decodeURIComponent(manageQueryStringParameter('SPAppWebUrl'));
- var scriptBase = appWebUrl + "/_layouts/15/";
- $.getScript(scriptBase + "sp.publishing.js");
-
- appCtxSite = new SP.AppContextSite(context, hostWebUrl);
-
-
- $("#btnCreate").click(function () {
- CreatePublishingPage();
- });
- });
-
- function CreatePublishingPage() {
- var pageName = "customPage.aspx", pageTitle = "customPage";
-
- var web = appCtxSite.get_web();
-
- var rootFolder = web.get_rootFolder();
-
- var pubWeb =new SP.Publishing.PublishingWeb.getPublishingWeb(context, web);
-
- context.load(rootFolder);
- context.load(pubWeb);
-
- loadPageLayout(function (pageLayoutItem) {
- console.log("Page Creation is started");
- var pageInfo = new SP.Publishing.PublishingPageInformation();
- pageInfo.set_pageLayoutListItem(pageLayoutItem);
- pageInfo.set_name(pageName);
- var newPage = pubWeb.addPublishingPage(pageInfo);
- context.load(newPage);
- context.executeQueryAsync(function () {
- console.log("Page is created successfully");
- var listItem = newPage.get_listItem();
- context.load(listItem);
- context.executeQueryAsync(
-
- function () {
- listItem.set_item("Title", pageTitle);
- listItem.update();
- listItem.get_file().checkIn();
- listItem.get_file().publish("Publishing");
- var currentPath = "Pages/" + pageName;
-
- rootFolder.set_welcomePage(currentPath);
- web.update();
- rootFolder.update();
- context.executeQueryAsync(function () {
- console.log("successfully page is updated as welcome page..")
- },
- function (sender, args) {
- console.log('Create page failed. ' + args.get_message());
- });
- },
-
- function (sender, args) {
- console.log('Get page failed. ' + args.get_message());
-
- });
- },
- function (sender, args) {
- console.log('Add page failed. ' + args.get_message());
-
- });
- });
- }
-
-
- function onfail(sender, args) {
- alert('Failed to add field in list. Error:' + args.get_message());
- }
-
-
- function manageQueryStringParameter(paramToRetrieve) {
- 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] == paramToRetrieve) {
- return singleParam[1];
- }
- }
- }
Note- In appmanifest.xml file, add the permission for the site collection. Finally the page will be created, using an existing template on your host Website.
Summary
In this blog, we have explored, how to create a publishing page and set the page as a welcome page, using SharePoint hosted app. Happy coding!!