M365 – SharePoint Online – Resolving error – You can only promote home pages as site home page – error while calling ClientSidePage.PromoteAsHomePage() in CSOM

M365 - SharePoint Online - Error - You can only promote home pages as site home page - error while calling ClientSidePage.PromoteAsHomePage() in CSOM
M365 - SharePoint Online - Error - You can only promote home pages as site home page - error while calling ClientSidePage.PromoteAsHomePage() in CSOM

Hi All,

LIFE IS BEAUTIFUL 🙂 I hope we all are safe 🙂 STAY SAFE, STAY HEALTHY 🙂

Today new issue 🙂 and solution 🙂

It is very small issue but I was not aware, took time to solve so this article 🙂 Sharing is Caring 🙂

Background / Use Case / Scenario :

  • We have .NET Core console application – basically we were writing job which provisions few client side pages in “Site Pages” library in modern Communication site
  • Also, we need to promote one client side page as home page
  • We were using following code – Code to provision new client side page through CSOM
List pagesLibrary = getSitePagesLibrary(siteUrl, cc);
cc.Load(pagesLibrary, pagesLibrary => pagesLibrary.RootFolder.Files);
cc.ExecuteQuery();

ListItem newPage = pagesLibrary.RootFolder.Files.AddTemplateFile
                  (cc.Web.ServerRelativeUrl + "/SitePages/Home1.aspx",  
                   TemplateFileType.ClientSidePage).ListItemAllFields;
                   
 newPage.Update();

newPage["Title"] = "Home";
newPage["ClientSideApplicationId"] = "<client side application GUID>";
newPage["PageLayoutType"] = "Article";
newPage["PromotedState"] = "0";
newPage.Update();
cc.Load(newPage);
cc.ExecuteQuery();
  • Code for method – getSitePagesLibrary() – Getting Pages Library using CSOM
private static List getSitePagesLibrary(string siteUrl, ClientContext contentContext)
{
   List sitePagesLibrary = null;

   try
   {

       Web web = contentContext.Web;
       contentContext.Load(web, w => w.ServerRelativeUrl, w => w.Lists);
       contentContext.ExecuteQuery();
       string listURL = web.ServerRelativeUrl + "/SitePages/";
       sitePagesLibrary = web.GetList(listURL);
       contentContext.Load(sitePagesLibrary);
       contentContext.ExecuteQuery();

    }
    catch (Exception ex)
    {
        //ToDo: Exception handling
    }
    return sitePagesLibrary;
  }//getSitePagesLibrary
  • Then we are loading this page and promoting it as home page
  • Following is the code
ClientSidePage clientSidePage = null;
 try
 {
     clientSidePage = ClientSidePage.Load(contentContext, "Home1.aspx");
     clientSidePage.PromoteAsHomePage();
 }
 catch (Exception ex)
 {
    //ToDo: Exception handling
 }
  • On the code line – clientSidePage.PromoteAsHomePage(); we were getting an exception.

Error / Issue :

Message : You can only promote home pages as site home page

StackTrace: at PnP.Framework.Pages.ClientSidePage.PromoteAsHomePage()

 M365 - SharePoint Online - Error-You can only promote home pages as site home page - error while calling ClientSidePage.PromoteAsHomePage() in CSOM
Fig – M365 – SharePoint Online – Error – You can only promote home pages as site home page – error while calling ClientSidePage.PromoteAsHomePage() in CSOM

Solution:

  • Again Google my friend 🙂 after bit searching and looking various git code for ClientSidePage, I realized that page layout type set for this client side page is wrong.
  • If you notice the code for creating client side page and property setting page layout type – newPage[“PageLayoutType”] = “Article”;
  • So correct page layout type for promoting client side page is “Home
  • I updated the code and it worked like charm 🙂 – newPage[“PageLayoutType”] = “Home”;
  • Following is the corrected code
List pagesLibrary = getSitePagesLibrary(siteUrl, cc);
cc.Load(pagesLibrary, pagesLibrary => pagesLibrary.RootFolder.Files);
cc.ExecuteQuery();

ListItem newPage = pagesLibrary.RootFolder.Files.AddTemplateFile
                  (cc.Web.ServerRelativeUrl + "/SitePages/Home1.aspx",  
                   TemplateFileType.ClientSidePage).ListItemAllFields;
                   
 newPage.Update();

newPage["Title"] = "Home";
newPage["ClientSideApplicationId"] = "<client side application GUID>";
newPage["PageLayoutType"] = "Home";
newPage["PromotedState"] = "0";
newPage.Update();
cc.Load(newPage);
cc.ExecuteQuery()

Conclusion / Takeaway : If we need promote any client side page as a home page, page layout type [“PageLayoutType”] must be “Home” while setting through the CSOM

Thanks for reading ðŸ™‚ Feel free to discuss / comments / questions 🙂 SHARING IS CARING 🙂

Share In Teams:

Enjoy the beautiful life 🙂 Have a FUN 🙂 HAVE A SAFE LIFE 🙂 TAKE CARE 🙂

Prasham Sabadra

LIFE IS VERY BEAUTIFUL :) ENJOY THE WHOLE JOURNEY :) Founder of Knowledge Junction and live-beautiful-life.com, Author, Learner, Passionate Techie, avid reader. Certified Professional Workshop Facilitator / Public Speaker. Scrum Foundation Professional certificated. Motivational, Behavioral , Technical speaker. Speaks in various events including SharePoint Saturdays, Boot camps, Collages / Schools, local chapter. Can reach me for Microsoft 365, Azure, DevOps, SharePoint, Teams, Power Platform, JavaScript.

You may also like...

2 Responses

  1. February 5, 2021

    […] M365 – SharePoint Online – Resolving error – You can only promote home pages as site home page… […]

  2. February 15, 2021

    […] This article is actually continuation of my previous following three articles – […]

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: