Musings on SharePoint, Adobe CQ, ECM, and more…

Set the SharePoint Welcome Page through code

You might end up with this requirement quite often to set your custom page as a welcome page for site/subsite instead of default.aspx provided by SharePoint.

Its quite a tedious job to set up default pages manually when you have more than 10 site/subsites. Its pretty easy to do it programmatically. Here is how:

public static void SetDefaultPage(SPWeb web, string pageName)
{
    if (PublishingWeb.IsPublishingWeb(web))
    {
    PublishingWeb publishingWeb = PublishingWeb.GetPublishingWeb(web); 

    //Get the file name
    SPFile welcomeFile = web.GetFile(pageName);
    //Assign the new filename to the DefaultPage property
    publishingWeb.DefaultPage = welcomeFile; 

    //Update the Publishing Web.
    publishingWeb.Update();
    }
} 

Remember to pass the full relative path to the pageName. For example, pageName should be "/Subsite/Pages/MyNewWelcome.aspx" if your welcome page resides in a Page library of the subsite.

Praveen Modi

Praveen works at Razorfish which is one of the largest digital advertising agency in the world. His mantra for life is "You are never too old to set another goal or to dream a new dream". He lives in sunny Austin, TX with his beautiful wife Nidhi and son Aariv.

2 Comments

  1. Raghavan

    Hi Praveen,

    Thanks for your post. It helped me!

  2. Praveen Modi

    Thanks Raghavan. I am gald it was of help to you.

Leave a Reply

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