create a sharepoint online page based on custom template c#

nellie 126 Reputation points

Hi I have created a page template on SharePoint using this article

Now I want to use it my c# code but I don't know-how


using (var ctx = authMgr.GetSharePointOnlineAuthenticatedContextTenant(siteUrl, userName, password))
                Web web = ctx.Web;
                ctx.Load(web, w => w.SupportedUILanguageIds);

                //Create the Page
                var page2 = ctx.Web.AddClientSidePage("PageWithSections.aspx",true);                  <------ STUCK HERE
                page2.AddSection(CanvasSectionTemplate.TwoColumnRight, 5);

                //page2.AddSection(CanvasSectionTemplate.TwoColumn, 10);
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,535 questions
SharePoint Server Development
SharePoint Server Development
SharePoint Server: A family of Microsoft on-premises document management and storage systems.Development: The process of researching, productizing, and refining new or existing technologies.
1,591 questions
{count} votes

Accepted answer
  1. Jerryzy 10,566 Reputation points

    Hi @nellie ,

    Please refer the following code snippet to create site page from custom template:

    using System;  
    using System.Collections.Generic;  
    using System.Linq;  
    using System.Text;  
    using System.Threading.Tasks;  
    using Microsoft.SharePoint.Client;  
    using ClientSidePage = OfficeDevPnP.Core.Pages.ClientSidePage;  
    using System.Security;  
    namespace CreatePageFromTemplate  
        class Program  
            static void Main(string[] args)  
                string userName = "";  
                string password = "**************";  
                using (ClientContext clientContext = new ClientContext(""))  
                    SecureString securePassword = new SecureString();  
                    foreach (char c in password.ToCharArray())  
                    clientContext.AuthenticationMode = ClientAuthenticationMode.Default;  
                    clientContext.Credentials = new SharePointOnlineCredentials(userName, securePassword);  
                    // Load the template page  
                    ClientSidePage page = clientContext.Web.LoadClientSidePage("Templates/MyTemplate.aspx");  
                    // Create and Save the modern page based on the template  

    After saving a page as template, SharePoint Online will create a folder named "Templates" to store the page template like this:


    So get this template and then save into another page name will create the page based on this template.

    Here is a detailed code demo, I suggest you can refer:

    How to : Create modern page templates and provision modern pages using them via PnP PowerShell/Core

    Best Regards

    If an Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

0 additional answers

Sort by: Most helpful