Changing the SharePoint Welcome Page through code

Sometimes you will have a feature that you activate on a sub site where you want to change the “Welcome Page” or landing page to be a custom one that you provision through your feature.  I was doing this the other day and realized that it isn’t blatantly obvious through Intellisense because it is called “DefaultPage”.  Here is a sample on how you would change default.aspx to be a file home.aspx that you have just created.


 PublishingWeb pubWeb = PublishingWeb.GetPublishingWeb(rootWeb);
SPFile homePageFile = rootWeb.GetFile("Pages/Home.aspx");

pubWeb.DefaultPage = homePageFile;

Hope that comes in handy at some point.