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 (6)
  1. Rich says:

    Hi, sorry if I missed this, but where exactly do you place this code? Is this in the feature.xml file?


  2. Greg says:

    This is very helpful and exactly what I was looking but where do we put this code?

  3. Rodrigo Gama says:

    Isn't there a way to change this through configuration on the SharePoint site?

  4. Manoj Gangwar says:

    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)

  5. Readers don't know where to put the code. This isn't a good tutorial 🙁

Comments are closed.

Skip to main content