Set the Welcome Page for a Web

Posted on Posted in SharePoint 2007, STSADM Commands

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);
   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.");
  10:  PublishingWeb pubWeb = PublishingWeb.GetPublishingWeb(web);
  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:  }
  32:  if (!flag || file == null)
  33:   throw new SPException("The specified welcome page could not be found.");
  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.

        -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 thoughts on “Set the Welcome Page for a Web

  1. 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).

Comments are closed.