Share via


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.

Comments

  • Anonymous
    April 09, 2009
    PingBack from http://stevepietrek.com/2009/04/09/links-492009/

  • Anonymous
    May 03, 2010
    Hi, sorry if I missed this, but where exactly do you place this code? Is this in the feature.xml file? Thanks!

  • Anonymous
    July 23, 2010
    This is very helpful and exactly what I was looking but where do we put this code?

  • Anonymous
    August 09, 2010
    Isn't there a way to change this through configuration on the SharePoint site?

  • Anonymous
    December 15, 2011
    Rodrigo Gama :- yes you can change the page using Sharepoint site as well Greg: you can create a feature and put this code in feature receiver file (on feature activating)

  • Anonymous
    September 20, 2012
    Readers don't know where to put the code. This isn't a good tutorial :(