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!

Be Sociable, Share!

5 thoughts on “Change the welcome page of your SharePoint 2007 site programmatically

  1. I’d suggest running this from a console application on a SharePoint server. Be sure to include references to Microsoft.SharePoint.dll and Microsoft.SharePoint.Publishing.dll, both to be found in [12]\ISAPI.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">