views:

2070

answers:

4

Hi,

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

Thanks, MagicAndi.

A: 

Hi.

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"...>
Magnus Johansson
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
Ok, sorry. For some reason I seem to have missed that part in your question.
Magnus Johansson
Magnus, Apologies, forgot to say "Thanks!" for taking the time to post an answer.
MagicAndi
A: 

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 
   }
W0ut
Hi W0ut, thank you for your answer, but we are trying to avoid an event receiver for this particular case.
MagicAndi
+2  A: 

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.

MagicAndi
Thanks for the solution that works on WSS! It works.
Janis Veinbergs
A: 

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(); 
  }
}
Colin
Colin, I'm sorry, but the question is quite clear in asking for a programmatic solution, as opposed to a manual operation. -1
MagicAndi
Anything you cna do through a site settings page, you can do in code. The WelcomePageUrl property(see here: http://www.sharepointnutsandbolts.com/2007/08/creating-deploying-and-updating-custom.html) of the publishing feature can be used to set an SPWeb's homepage)..
Colin
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
Colin, Thanks. The -1 has been taken off.
MagicAndi