create a sharepoint online page based on custom template c#

nellie 126 Reputation points
2021-02-19T18:30:14.1+00:00

Hi I have created a page template on SharePoint using this article https://petri.com/sharepoint-page-templates-have-finally-arrived-heres-how-to-use-them

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

THANK YOU

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

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


                //page2.AddSection(CanvasSectionTemplate.TwoColumn, 10);
                page2.Save();
C#
C#
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
    2021-02-22T04:45:50.573+00:00

    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 = "user@Tenant.onmicrosoft.com";  
                string password = "**************";  
                using (ClientContext clientContext = new ClientContext("https://Tenant.sharepoint.com/sites/SiteName"))  
                {  
                    SecureString securePassword = new SecureString();  
                    foreach (char c in password.ToCharArray())  
                    {  
                        securePassword.AppendChar(c);  
                    }  
      
                    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  
                    page.Save("IT_Australia1.aspx");  
                }  
      
            }  
        }  
    }  
    

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

    70463-snipaste-2021-02-22-12-39-12.png

    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

    Thanks
    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