DevExpertise

Practical tips and tricks for all things .NET, SharePoint, Silverlight, InfoPath, and general application development.

Setting the Welcome Page in WSS 3.0

Posted by DevExpert on 2nd February 2009

Whenever someone navigates to your SharePoint site without a specific page specified in the URL, such as http://fakeserver/sites/fakesite/, SharePoint navigates you to the site’s welcome page, typically default.aspx.  MOSS publishing sites provide the ability to change this by going to Site Settings > Welcome Page, and changing the page.  What about team sites, or WSS sites?  There is no way to modify the welcome page through the UI.  One could open SharePoint Designer and do it that way, but this isn’t ideal if you don’t have a license for SPD.  I’ll be describing another approach in this post that leverages the Object Model, and I’m even providing a complete SharePoint solution (WSP) file that you can install and activate on any WSS site!

First, let’s take a look at the code snippet to make this happen.  The SPFolder object has a property called WelcomePage, and all that is needed is to set this property on the SPWeb’s RootFolder:

using (SPSite siteCollection = new SPSite(http://server/sites/sandbox)) {
    using (SPWeb site = siteCollection.RootWeb) {
        SPFolder rootFolder = site.RootFolder;
        rootFolder.WelcomePage = "Pages/home.aspx";
        rootFolder.Update();
    }
}


Now, whenever I hit http://server/sites/sandbox, I am directed to my custom Home.aspx page in my Pages document library:

image


Woo hoo!  If I click on the Sandbox title or its icon, or even in the breadcrumb, I’m directed here, because it’s to the site’s root URL, not to a specific page.  But what if I click the Home link on the top navigation bar? It takes me to my original default.aspx page, which defeats the purpose.  To fix this, we need to add the following lines of code:

SPNavigationNode homeNode = site.Navigation.TopNavigationBar.Navigation.Home;
homeNode = site.Navigation.TopNavigationBar[0];
homeNode.Url = SPUrlUtility.CombineUrl(site.Url, "Pages/home.aspx");
homeNode.Update();


Now, no matter what I click to bring me back to the root page, I’m directed to my home.aspx page.  Who said the Welcome Page was a MOSS-only feature?

 

SharePoint Solution

It doesn’t make sense to expect an end user to run a console app or write custom code to do this.  It’s possible to do this via SharePoint designer too, but what if they don’t have it?  It makes a lot more sense to present this to users in the browser, exactly like a MOSS publishing site.  To provide this functionality, it’s just as simple as creating a custom LAYOUTS page and a solution package that can be used to deploy and install it. 

First, we create the file-system structure in the Visual Studio solution:

image


Next, define the feature:

<?xml version="1.0" encoding="utf-8"?>
<Feature
  Id="2D3BDDCF-E6A4-4110-922C-70E79652C6B9"
  Title="DevExpertise Welcome Page Feature"
  Description="Contains the page and shortcuts to change a site's welcome page"
  Version="1.0.0.0"
  Scope="Site"
  Hidden="false" 
  ImageUrl="DevExpertise.WelcomePage\devExpertiseFeatureLogo.png" 
  xmlns="http://schemas.microsoft.com/sharepoint/">
  <ElementManifests>
    <ElementManifest Location="CustomActions.xml" />
  </ElementManifests>
</Feature>


The custom action should be defined to add a link to the Look & Feel category (GroupId=”Customization”):

<?xml version="1.0" encoding="utf-8" ?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <CustomAction
    Id="UserGroupAdminLinkForSettings"
    GroupId="Customization"
    Location="Microsoft.SharePoint.SiteSettings"
    RequireSiteAdministrator="TRUE"
    Sequence="120"
    Title="Welcome Page">
    <UrlAction Url="_layouts/DevExpertise.WelcomePage/WelcomePage.aspx" />
  </CustomAction>
</Elements>


Package up your solution using WSPBuilder and deploy it.  After it’s deployed and activated on the site collection, I’m presented with a Welcome Page link on each and every sub site in the site collection, allowing me to set the welcome page for each site:

image


Clicking the link will navigate to the custom LAYOUTS page I created:

image


This is a great method for providing functionality that may be missing from the user interface.  Once you master the steps required to create a LAYOUTS page (which I’ll be covering step-by-step in a future post), and master creating custom actions, the possibilities to extend and enhance your SharePoint site are endless.

I’ve included this solution package for you to deploy and use.  As always, the code is as-is and without warranty, so if it doesn’t work – fix it!

Download Solution: DevExpertise.WelcomePage.zip

Tags: , , ,
Posted in .NET, Object Model, SharePoint | 3 Comments »