Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I am writing a feature receiver. At first I create some pages in "Site Pages" Library on feature activation and further I try to set one of the recently created page as Welcome page. But on deployment of the feature all works as intended except the welcome page. It remains the default "Home.aspx" under "Site Pages". The site where I deploy my feature is not a publishing site. Any specific reason?

Here is my sample code

 public void SetHomePage(SPWeb web, string path)
{
  try
  {

    if (PublishingWeb.IsPublishingWeb(web))
    {
      PublishingWeb publish = PublishingWeb.GetPublishingWeb(web);
      SPFile file = web.GetFile(path);
      publish.DefaultPage = file;
      publish.Update();
      web.Update();
    }
    else
    {
      web.RootFolder.WelcomePage = path;
      web.RootFolder.Update();
    }

  }
share|improve this question
    
What code did you use to try to set teh welcome page? –  Russell Aug 16 '12 at 7:44
    
I have edited my question for sample code.Please have a look. –  V.B Aug 16 '12 at 7:54
1  
Try removing "web.Update();" below the "pub.Update();". Perhaps the web.Update() is replacing teh DefaultPage property... –  Russell Aug 16 '12 at 7:57
1  
Does it work for non-publishing pages? Which were you trying? –  Russell Aug 16 '12 at 7:58
    
Mine is not a publishing site. –  V.B Aug 16 '12 at 8:01

1 Answer 1

up vote 4 down vote accepted

Check the site you are using is a publishing site. Also remember if a site has already been used, users may have added bookmarks which will point directly to the old page. Make sure you consider the change management component if applicable.

Gary Lapointe has an article on how to set the default "welcome" page for publishing sites:

http://blog.falchionconsulting.com/index.php/2008/01/set-the-welcome-page-for-a-web/

To summarise the code:

SPWeb web; // assuming you have an SPWeb object already.
SPFile file; // assuming you have an instance of SPFile for the page to set to be default.
PublishingWeb currentPublishingWeb = pubWeb;
currentPublishingWeb.DefaultPage = file;
currentPublishingWeb.Update();

If the site is not a publishing site, this MSDN article explains the SPFolder.WelcomePage property: http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spfolder.welcomepage.aspx

Here's the example:

using (SPSite oSiteCollection = new SPSite("http://MyServer/sites/MyWikiSite"))
{
    SPWeb oWebsite = oSiteCollection.OpenWeb();
    SPFolder oFolder = oWebsite.RootFolder; 
    oFolder.WelcomePage = "My Wiki Library/MyWelcome.aspx";
    oFolder.Update();
    oWebsite.Dispose();
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.