15

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

2
  • Could you please add information that you have decided that don't want to use site templates, thanks.
    – Magnus
    Oct 30, 2009 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, 2009 at 14:11

5 Answers 5

14

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.

0
7

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(); 
  }
}
3
  • Colin, I'm sorry, but the question is quite clear in asking for a programmatic solution, as opposed to a manual operation. -1
    – Tangiest
    Nov 3, 2009 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, 2009 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.
    – Tangiest
    Nov 3, 2009 at 13:07
3

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 
   }
2
  • Hi W0ut, thank you for your answer, but we are trying to avoid an event receiver for this particular case.
    – Tangiest
    Oct 30, 2009 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, 2012 at 10:52
1

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"...>
3
  • 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.
    – Tangiest
    Oct 30, 2009 at 15:02
  • Ok, sorry. For some reason I seem to have missed that part in your question.
    – Magnus
    Oct 30, 2009 at 16:10
  • Magnus, Apologies, forgot to say "Thanks!" for taking the time to post an answer.
    – Tangiest
    Oct 30, 2009 at 16:54
1

I have created a batch job to replace default.aspx page in blank web site. see the below code.

private static string RootSite = ConfigurationManager.AppSettings.Get("RootSite");
private static string WebSite = ConfigurationManager.AppSettings.Get("WebSite");
private static string OriginalFile = ConfigurationManager.AppSettings.Get("OriginalFile");
private static string BackupFileName = ConfigurationManager.AppSettings.Get("BackupFileName");
private static string NewFileUrl = ConfigurationManager.AppSettings.Get("NewFileUrl");

static void Main(string[] args)
{

    SPSecurity.RunWithElevatedPrivileges(delegate()
    {
        using (SPSite site = new SPSite(RootSite))
        {
            using (SPWeb web = site.OpenWeb(WebSite))
            {
                SPFile originalFile = web.GetFile(OriginalFile);
                originalFile.MoveTo(BackupFileName);
                SPFile newFile = web.GetFile(NewFileUrl);
                newFile.CopyTo(OriginalFile);
                SPFolder rootFolder = web.RootFolder;
                rootFolder.WelcomePage = OriginalFile;
                rootFolder.Update();
            }

        }
    });
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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