Question

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

Was it helpful?

Solution

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.

OTHER TIPS

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(); 
  }
}

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 
   }

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"...>

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();
            }

        }
    });
}
Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top