SharePoint Library is used to store the documents or files and metadata associated to those files. Some of the special type of libraries is used to store pages and components that can be utilized by SharePoint to display the data in those pages. There are 3 types of basic templates available in SharePoint to create the pages.

Page Template Types:

Template File Type Template File Type ID
Standard Page (Web Part Page) 0
Wiki Page 1
Form Page 2

In this post,

we will see how to create a page in SharePoint library based on the page template using PnP JavaScript Component.

PnP-JS-Core library contains the number of extensible methods and properties. By using that we can achieve the various actions in a simple code. To know more about this library component, visit the below links,

Simplified JavaScript Library for SharePoint

PnP-JS-Core Library


$pnp.sp.web.getFolderByServerRelativeUrl(‘/folderurl’).files.addTemplateFile(‘/folderurl/filename.aspx’,TemplateFileTypeID).then(function(data){ console.log(data); });


The below steps and code snippet used to all create a web part page in Site Pages library using PnP JavaScript Component,

  1. Download Required files to use PnP-JS-Core library from the below links and upload that to Site Asstes or Style Library
    • Download pnp.js  PnP JS file
    • Download fetch.js Used by PnP js file to handle web requests and responses (Required in IE)
    • Download promise.js Used by PnP js file to handle web requests and responses (Required in IE)
  2. Create new web part page and insert Content Editor web part
  3. Create a sample.html file in Site Assets or Style library and insert the below code snippet
    <script type="text/javascript" src="/siteassets/scripts/fetch.js"></script> 
    <script type="text/javascript" src="/siteassets/scripts/es6-promise.js"></script>
    <script type="text/javascript" src="/siteassets/scripts/pnp.min.js"></script>
    <div id="sample"></div>
    <script type="text/javascript">
    //getFolderByServerRelativeUrl method returns the folder based on the folder's relative url
    //files property returns all files from the folder
    //addTemplateFile with two parameters 'New File Relative URL' and 'Template File Type' used to create a new page
    .addTemplateFile("/SitePages/webpartpage.aspx",0).then(function(result) {
        var pagename =;  //New Page Name (ex., webppartpage.aspx)
        var strmessage = "";
        if (pagename != "") {
            strmessage += "Page '" + pagename + "' created successfully!<br/>";
            strmessage += "Click <a href='" + + '">here</a> to open that page.";
            document.getElementById("sample").innerHTML = strmessage;
    }).catch(function(err) {
    //To create Wiki Page, use addTemplateFile("/SitePages/wikipage.aspx",1)
    //To create Form page, use addTemplateFile("/SitePages/formpage.aspx",2)
  4. Add the URL of sample.html file to the content editor web part.
  5. Click OK to apply the changes to the web part and save the page.
  6. After the successful creation of the page based on the template, the current page displays the success message and a link to that page.


Pascal · October 12, 2017 at 4:55 PM

Hi Shantha,

thanks very much for your blog. It helped me a lot, as I am trying to create pages using SPFx and PNP JS. Just to let you know, I had to to add the serverRelativeUrl of the Web before the both “/SitePages” to make it work properly.

Within SPFx I added “this.context.pageContext.web.serverRelativeUrl” like:
pnp.sp.web.getFolderByServerRelativeUrl(this.context.pageContext.web.serverRelativeUrl + “/SitePages”).files
.addTemplateFile(this.context.pageContext.web.serverRelativeUrl + “/SitePages/” + dialog.roomName + “.aspx”,0).then….

I am trying to add modern pages within a modern Team site. In the moment it always creates a classic page experience. I tried the TemplateFileTypes 0,1 and 2. Do you have an idea how to create a modern page?

Thanks for your help!

    mohan · December 17, 2019 at 4:20 PM

    Nice dear …. For given solution

Pappu Kumar Singh · December 15, 2017 at 2:27 PM

I too did not succeed in creating modern pages using pnp js. Looks like it is not possible at the moment. Let me know your thoughts

Shyam Sundar · June 2, 2018 at 1:41 PM

Hi Shantha,

I need to get files from all folders and subfolders in a document library. How do it recursively retrieve files and folder from all the folders and sub folders. I managed to get from one specific folder/relative path specified but not from all folders.

Do you have any idea how we retrieve files from all folders in a document library?


Leave a Reply

Your email address will not be published. Required fields are marked *