MOSS MVP

I've moved my blog to http://blog.falchionconsulting.com!. Please update your links. This blog is no longer in use--you can find all posts and comments at my new blog; I will no longer be posting to this site and comments have been disabled.

Monday, January 28, 2008

Set the Welcome Page for a Web

When I did the gradual upgrade I found that several sites had the welcome page set to UpgLandingPgRedir.aspx when it should have been set to default.aspx. I didn't have time to figure out why it was doing this but the fix was easy enough - I just had to change the welcome page back. To do this I created a new command: gl-sitewelcomepage. The code to set this is really simple - basically just get a PublishingWeb instance and set the DefaultPage property to a valid SPFile object:

   1: string url = Params["site"].Value.TrimEnd('/');
   2: string page = SPHttpUtility.UrlPathDecode(Params["welcomepage"].Value, true);
   3:  
   4: using (SPSite site = new SPSite(url))
   5: using (SPWeb web = site.AllWebs[Utilities.GetServerRelUrlFromFullUrl(url)])
   6: {
   7:  if (!PublishingWeb.IsPublishingWeb(web))
   8:   throw new SPException("The specified site is not a publishing web.");
   9:  
  10:  PublishingWeb pubWeb = PublishingWeb.GetPublishingWeb(web);
  11:  
  12:  bool flag = false;
  13:  SPFile file = null;
  14:  try
  15:  {
  16:   file = pubWeb.Web.GetFile(page);
  17:   flag = file.Exists;
  18:  }
  19:  catch (SPException)
  20:  {
  21:   flag = false;
  22:  }
  23:  catch (FileNotFoundException)
  24:  {
  25:   flag = false;
  26:  }
  27:  catch (ArgumentException)
  28:  {
  29:   flag = false;
  30:  }
  31:  
  32:  if (!flag || file == null)
  33:   throw new SPException("The specified welcome page could not be found.");
  34:  
  35:  
  36:  PublishingWeb currentPublishingWeb = pubWeb;
  37:  currentPublishingWeb.DefaultPage = file;
  38:  currentPublishingWeb.Update();
  39: }

The syntax of the command can be seen below:

C:\>stsadm -help gl-sitewelcomepage

stsadm -o gl-sitewelcomepage

Sets the page to be used as the welcome page for the site.

Parameters:
        -site <url of the site to update>
        -welcomepage <full url to the page to use as the welcome page>
Here's an example of how to set the welcome page for a site:
stsadm -o gl-sitewelcomepage -site "http://intranet/sites/teamsite1" -welcomepage "http://intranet/sites/teamsite1/pages/default.aspx"

4 comments:

Sweet Sparky said...

Gary,
How do you set this for a NON publshing site
Thx

Gary Lapointe said...

For non-publishing sites you have to just make sure that the welcome page is named default.aspx and is in the root folder of the web (not in a doc library (so to change the welcome page you'd have to rename some files).

Kirk Hofer said...

Sure you can. SPFolder.WelcomePage works in WSS. http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spfolder.welcomepage.aspx

Gary Lapointe said...

Doh - should have known that - thanks! Learned something new today :)