How to create webpart page with specific layout template?

Yuvraj Patil 361 Reputation points
2022-02-04T09:14:37.273+00:00

I have a requirement where I need to take backup of webpart page and restore it later if page got deleted. I am able to take the backup of webpart page and webparts of it using LimitedWebPartManager.

While restoring, when I create webpart page using below code, it have layout template similar to FullPage but its zone name is main, only single zone.

Code:
List targetList = clientcontext.Web.Lists.GetByTitle("Site Pages");
var pageTitle = "newPage0101.aspx";
var file = targetList.RootFolder.Files.AddTemplateFile(targetList.RootFolder.ServerRelativeUrl + "/" + pageTitle, TemplateFileType.StandardPage);
clientcontext.Load(file);
clientcontext.ExecuteQuery();

However backed up page have "Header, Footer, 2 Columns, 4 Rows", and would like to create webpart page with same layout template, so could anyone please help here?

I am using CSOM (C#).

Available templates:

Header, Footer, 3 Columns
Full Page, Vertical
Header, Left Column, Body
Header, Right Column, Body
Header, Footer, 2 Columns, 4 Rows
Header, Footer, 4 Columns, Top Row
Left Column, Header, Footer, Top Row, 3 Columns
Right Column, Header, Footer, Top Row, 3 Columns

SharePoint
SharePoint
A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.
9,965 questions
SharePoint Development
SharePoint Development
SharePoint: A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.Development: The process of researching, productizing, and refining new or existing technologies.
2,748 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. RaytheonXie_MSFT 32,476 Reputation points Microsoft Vendor
    2022-02-07T08:56:35.767+00:00

    Hi @Yuvraj Patil ,
    You can refer to the following rest api to create webpart page

    function createWikiPage(webUrl,listTitle,fileName,success, failure)  
    {  
      
      getListUrl(webUrl,listTitle,  
        function(listUrl){    
      
         var fileUrl = listUrl + '/' + fileName  
         var url = webUrl + "/_api/web/GetFolderByServerRelativeUrl('" + listUrl + "')/Files" +  
                   "/AddTemplateFile(urlOfFile='" + fileUrl + "',templateFileType=1)";  
         $.ajax({  
            url: url,  
            method: "POST",  
            headers: {  
                   "accept": "application/json;odata=verbose",  
                   "content-type": "application/json;odata=verbose",  
                   "X-RequestDigest" : $("#__REQUESTDIGEST").val()  
            },  
            success: function (data) {  
                success(data.d);  
            },  
            error: function (data) {  
                failure(data);  
            }  
         });  
      
        },  
        failure  
      );  
      
      
    }  
      
      
    function getListUrl(webUrl,listTitle,success, failure)  
    {  
        var url = webUrl + "/_api/web/lists/GetByTitle('" + listTitle +  "')/RootFolder";   
        $.ajax({  
            url: url,  
            method: "GET",  
            headers: {  
                   "accept": "application/json;odata=verbose",  
                   "content-type": "application/json;odata=verbose"  
            },  
            success: function (data) {  
                success(data.d.ServerRelativeUrl);  
            },  
            error: function (data) {  
                failure(data);  
            }  
        });  
    }  
      
      
    //Usage  
    createWikiPage(_spPageContextInfo.webAbsoluteUrl,'Pages','WikiTestPage.aspx',  
      function(page){    
      },  
      function(error){  
      }  
    );  
    

    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    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.