SharePoint Publishing feature is one of the features which extends SharePoint functionality as a content management system. Once the publishing feature is activated, we can create the publishing pages with the different page layouts and associate it to the custom master page. This creates a unique look and feel and helps to customize the way, content is published. Publishing feature can be activated at the site collection and the site level.
feature
Once, it is activated, Pages library will be available in the site contents.
Pages
We can create the pages with the different page layouts in this library.

 create pages

Let’s see, how we can do the same programmatically, using JavaScript Object Model.

Internal Implementation

Full Code

The full code to create the publishing page, using JavaScript object model is shown below:

  1. <script language="javascript" type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"></script>
  2. <script language="javascript" type="text/javascript">
  3. $(document).ready(function() {
  4. var scriptbase = _spPageContextInfo.webServerRelativeUrl + "/_layouts/15/";
  5. $.getScript(scriptbase + "SP.Runtime.js", function() {
  6. $.getScript(scriptbase + "SP.js", function() {
  7. $.getScript(scriptbase + "SP.Publishing.js", createPublishingPage);
  8. });
  9. });
  10. });
  11. var oWeb, clientContext, pageLayoutitem;
  12. function createPublishingPage() {
  13. //Get the client context,web and list object(Master Page Gallery)
  14. clientContext = new SP.ClientContext.get_current();
  15. oWeb = clientContext.get_web();
  16. var oList = oWeb.get_lists().getByTitle('Master Page Gallery');
  17. //Get the page layout by ID using which we will create a publishing page
  18. pageLayoutitem = oList.getItemById(2268);
  19. //Load the client context and execute the batch
  20. clientContext.load(oWeb);
  21. clientContext.load(pageLayoutitem);
  22. clientContext.executeQueryAsync(QuerySuccess, QueryFailure);
  23. }
  24. function QuerySuccess() {
  25. //Create Publishing Page using PublishingPageInformation object
  26. var newPublishingPage = SP.Publishing.PublishingWeb.getPublishingWeb(clientContext, oWeb);
  27. var pageInfo = new SP.Publishing.PublishingPageInformation();
  28. pageInfo.set_name("New Publishing Page.aspx");
  29. pageInfo.set_pageLayoutListItem(pageLayoutitem);
  30. newPage = newPublishingPage.addPublishingPage(pageInfo);
  31. //Load the new page object to the client context
  32. clientContext.load(newPage);
  33. clientContext.executeQueryAsync(SecondQuerySuccess, SecondQueryFailure);
  34. }
  35. function QueryFailure(sender, args) {
  36. console.log('Request failed' + args.get_message());
  37. }
  38. function SecondQuerySuccess(sender, args) {
  39. console.log("Publishing page created successfully.");
  40. }
  41. function SecondQueryFailure(sender, args) {
  42. console.log('Request failed' + args.get_message());
  43. }
  44. </script>
Let’s see, how we can implement it in SharePoint. Save the scripts, mentioned above, onto a text file and upload it to Site Assets library.

SharePoint Implementation


Output: The new publishing page has been created in the pages library.

Output

Summary

Thus, we have seen, how we can create a publishing page in SharePoint. This has been tried and tested in SharePoint 2016, as well as Office 365.