Posted by DevExpert on February 2nd, 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:
Now, whenever I hit http://server/sites/sandbox, I am directed to my custom Home.aspx page in my Pages document library:
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:
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?
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:
Next, define the feature:
The custom action should be defined to add a link to the Look & Feel category (GroupId=”Customization”):
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:
Clicking the link will navigate to the custom LAYOUTS page I created:
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