Change the welcome page of your SharePoint 2007 site programmatically

Today I was trying to figure out how to change the welcome page of a SharePoint 2007 site programmatically. I couldn’t really find useful links on Google, so I figured I’d go and play around a bit with the object model.

Apparently the PublishingWeb object has a property called DefaultPage, which takes an SPFile object as a value. I ended up with the following code to change the welcome page:

using(SPSite siteCollection = new SPSite("http://yourserver"))
{
SPWeb targetWeb = siteCollection.AllWebs["YourWebName"];
SPFile newWelcomePage = null; // Change the code here to set the SPFile object to your new welcome page

if(PublishingWeb.IsPublishingWeb(targetWeb))
{
PublishingWeb publishingWeb = PublishingWeb.GetPublishingWeb(targetWeb);
publishingWeb.DefaultPage = newWelcomePage; // this sets the new welcome page
publishingWeb.Update();
}
}

That should do the trick :). I hope this post is useful for anyone who’s also trying to achieve the same thing!

1 Response to “Change the welcome page of your SharePoint 2007 site programmatically”


  1. 1 Wilson Wampers

    Thx mate, you just saved me some trouble :)

Leave a Reply