Search This Blog

Monday, January 16, 2012

How to change the default home page or Set Welcome Page of a SharePoint site using PowerShell Script - SharePoint 2010

By default a SharePoint 2010 publishing site uses default.aspx as its welcome page.
There are so many conditions where we use our custom web part page as the default page for our publishing site in place of general default.aspx. The best example for this is an application dashboard as the default page.

Below are four ways to set another page as your home page: (all four work for both 2007 and 2010)

1. From Site Settings (If the publishing features are enabled)
2. From SharePoint Designer
3. From code / API
4. From PowerShell

1. If the publishing features are enabled for a site then:
Go to Site Actions -> Site Settings -> Welcome Page
In SharePoint 2007

In SharePoint 2010

2. From SharePoint Designer:
Right-click the new page and click "Set as Home Page".
(For SharePoint 2007 this only appears to work from SharePoint Designer if the file is in the root of the site. I.e. the same place as default.aspx.)

3. From code / API:

using (SPSite oSiteCollection = new SPSite("http://sharepoint2010:2400"))
            {
                SPWeb oWebsite = oSiteCollection.OpenWeb();
                SPFolder oFolder = oWebsite.RootFolder;
                oFolder.WelcomePage = "SiteAssets/BecomeFan.aspx";
                oFolder.Update();
                oWebsite.Dispose();
            }

4. From PowerShell:
In SharePoint 2010, Create a powershell script file e.g. SetWelcomePage.ps1, with the following script:

Add-PsSnapin Microsoft.SharePoint.PowerShell
$assignment=Start-SPAssignment
$web=Get-SPWeb -Identity "http://sharepoint2010:2400" -AssignmentCollection $assignment
$rootFolder=$web.RootFolder
$rootFolder.WelcomePage="SiteAssets/BecomeFan.aspx"
$rootFolder.Update()
Stop-SPAssignment $assignment
$web.Update()
$web.Dispose()
Write-Host 'Welcome Page Set Successfully…'
Write-Host 'Press any key to exit…'
$x=$Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
$Host.SetShouldExit(1)

After creating the script file, just run that file by right click and Run with PowerShell:


Now, navigate back to a site and see that the homepage is now set to the given page.


No comments:

Post a Comment