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 Microsoft 365 Junction, Speaker, Author, Learner, Developer, Passionate Techie. Certified Professional Workshop Facilitator / Public Speaker. Believe in knowledge sharing. Around 20+ years of total IT experience and 17+ years of experience in SharePoint and Microsoft 365 services Please feel free me to contact for any SharePoint / Microsoft 365 queries. I am also very much interested in behavioral (life changing) sessions like motivational speeches, Success, Goal Setting, About Life, How to live Life etc. My book - Microsoft 365 Power Shell hand book for Administrators and Beginners and 100 Power Shell Interview Questions - https://www.amazon.in/Microsoft-Administrators-Beginners-Interview-Questions/dp/9394901639/ref=tmm_pap_swatch_0?_encoding=UTF8&qid=1679029081&sr=8-11

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 – […]

Leave a Reply

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

Discover more from Microsoft 365

Subscribe now to keep reading and get access to the full archive.

Continue reading