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 have been asked to programmatically do the following in a SharePoint 2007 team site collection:

  • Create a website using the OOTB blank site template. (Please note, for project specific reasons, we are not using customized site templates.)
  • Activate the Publishing feature on the created website
  • Activate a number of other custom features (for lists, etc)
  • Create a page using a specified page layout (provisioned with webparts) and use this as the default page.

I have completed the first three steps successfully, but I am stuck on the fourth step. I can easily create a new publishing page via the SharePoint API, but how do I overwrite the default page with this new page? Or can I simply modify the web so that the new page becomes the default page for that website? Any advice would be appreciated.

Update

The code has to run on a button click, not in a Feature Event Reciever

share|improve this question
    
Could you please add information that you have decided that don't want to use site templates, thanks. –  Magnus Oct 30 '09 at 16:11
    
For what it's worth, we've historically run into issues with the proposed approach. Specifically, .aspx files that were not in the Pages list did not always behave as expected. Good luck! –  Jason Weber Nov 2 '09 at 14:11
add comment

migrated from stackoverflow.com Nov 9 '11 at 14:56

This question came from our site for professional and enthusiast programmers.

4 Answers

up vote 11 down vote accepted

I came across this link by Josh Gaffey that describes how to set the welcome page for a publishing site.

The code I used is:

... // Create publishing page

if (bPageCreated)
{
    using (SPSite site = new SPSite(p_sSubSiteUrl))
    {
        using (SPWeb web = site.OpenWeb())
        {
            PublishingWeb publishingWeb = null;

            if (PublishingWeb.IsPublishingWeb(web))
            {
                publishingWeb = PublishingWeb.GetPublishingWeb(web);
                SPFile homePageFile = web.GetFile("Pages/" + sPageName + ".aspx");
                publishingWeb.DefaultPage = homePageFile;
                homePageFile.Publish("Page set as default welcome page by " + SPContext.Current.Web.CurrentUser.LoginName);
                publishingWeb.Update();
                web.Update();
            }
        }
    }
}

All credit to Josh for the solution. I have tested the above code and it works.

Additionally, there is a slightly different solution for changing the default page for a blank site:

using (SPSite site = new SPSite(p_sSubSiteUrl))
{
    using (SPWeb web = site.RootWeb())
    {
        SPFolder rootFolder = web.RootFolder;
        rootFolder.WelcomePage = "login.aspx";
        rootFolder.Update();
    }
}

This came from an article by Peter Tane, and I have not tested it personally.

share|improve this answer
    
Thanks for the solution that works on WSS! It works. –  Janis Veinbergs Mar 15 '10 at 7:41
add comment

Add a page to the Pages library of your site, name it HomePage.aspx. Add whatever webpart etc. you need to it. Then, in the site settings of the site (_Layouts/AreaWelcomePage.aspx) set the url to point to your custom homepage. Now when someone enters the url of your site (ie. http://mycoolportal.company.local) IIS / SharePoint will point the browser to the new custom homepage.

To be complete, add a Content Editor Web part to the original Default.aspx and add some Javascript to redirect the user to the new custom page. (this is for user who for instance have http://mycoolportal.company.local/default.aspx in their favourites or who enter that complete url in there browser manually).

Edit:

from here

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(); 
  }
}
share|improve this answer
    
Colin, I'm sorry, but the question is quite clear in asking for a programmatic solution, as opposed to a manual operation. -1 –  MagicAndi Nov 3 '09 at 9:49
    
Anything you cna do through a site settings page, you can do in code. The WelcomePageUrl property(see here: sharepointnutsandbolts.com/2007/08/…) of the publishing feature can be used to set an SPWeb's homepage).. –  Colin Nov 3 '09 at 12:56
    
Colin, if you were to update your answer to include a link to an article showing the relevant code, or to include the code in the answer, I would be more than happy to remove the downvote. Until then, your answer isn't relevant to the question asked. –  MagicAndi Nov 3 '09 at 13:07
    
Colin, Thanks. The -1 has been taken off. –  MagicAndi Nov 4 '09 at 10:23
add comment

Create a feature with your Site Definition. Include the custom default.aspx

You can add extra web parts on the page in your Site definition. Make sure that the features containing the web parts are enabled on the site. I've done this a bunch of times without any problems.

If you don't like doing it in the site definition. You can also handle the feature's events with a 'SPFeatureReceiver'.

This FeatureActivated event is triggered after the feature is activated. There you can put your C# magic.

public override void FeatureActivated(SPFeatureReceiverProperties properties)
   { 
   //DO YOUR THING 
   }
share|improve this answer
    
Hi W0ut, thank you for your answer, but we are trying to avoid an event receiver for this particular case. –  MagicAndi Oct 30 '09 at 16:54
    
Thanks wout, for your answer. I had also same type of problem that has been solve now by referring your code. –  user9197 Jun 29 '12 at 10:52
add comment

You can create a custom site template, and create a custom default page in your custom onet.xml file.

<Modules>
<Module...>
<File Url="default.aspx"...>
share|improve this answer
    
Magnus, No!! We have been down that route already, and while we were able to create the new site and the default page using the specified page layout, it lead to other issues. So we are going down the above route of provisioning the websites programmatically. –  MagicAndi Oct 30 '09 at 15:02
    
Ok, sorry. For some reason I seem to have missed that part in your question. –  Magnus Oct 30 '09 at 16:10
    
Magnus, Apologies, forgot to say "Thanks!" for taking the time to post an answer. –  MagicAndi Oct 30 '09 at 16:54
add comment

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.