M365 – SharePoint Online – Resolving 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()

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 🙂
2 Responses
[…] M365 – SharePoint Online – Resolving error – You can only promote home pages as site home page… […]
[…] This article is actually continuation of my previous following three articles – […]
You must log in to post a comment.