// you’re reading...

SharePoint

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.

You might be interested in:
  • SharePoint 2007 Test Data Population Tool
  • SHAREPOINT DESIGNER 2007 IS NOW FREE!
  • Programmatically Delete all items from a SharePoint List
  • SharePoint - Add or Delete Site Column reference from Content Types
  • Review of: SharePoint Content Deployment Wizard
  • Discussion

    2 comments for “Set the SharePoint Welcome Page through code”

    1. Hi Praveen,

      Thanks for your post. It helped me!

      Posted by Raghavan | April 8, 2009, 8:01 am
    2. Thanks Raghavan. I am gald it was of help to you.

      Posted by Praveen Modi | April 8, 2009, 8:11 am

    Post a comment

    Spam Protection by WP-SpamFree

      Locations of visitors to this page
    View Praveen Modi's profile on LinkedIn

    Recent Comments

    • D Baldassarri: Hello Praveen I apologize if this is a reduntant question. I have a List, when the list is created I...
    • Tanuj Kumar: Check this helpful link…. http://mindstick.com/Articles/ 19b8cf75-6d2e-4504-8840-718...
    • sanjeev: Hello i want to upadate all the items of a column.I need to apply one formula for upadating the...
    • pankaj: Thanks a lot.Though i m already done with it.will try this too.
    • Sandeep: Here’s correct way to do it http://snahta.blogspot.com/201 1/09/deleting-all-items-from-l ists.html