Change the Welcome Page / Homepage in SharePoint 2010 Programmatically

When exporting and importing sites throughout SharePoint 2010 that are upgraded sites from SharePoint 2007, you probably noticed that the homepage for the imported site is defaulted to SharePoint 2010′s wiki-style page at /SitePages/Home.aspx. However, until you start using these new pages you may want to revert or keep the homepage as the default.aspx page.

To do this manually, you need to go to Site Actions > Site Settings > Look and Feel > Welcome Page.

However, this Welcome Page link is only available for those sites where the Publishing feature is activated at the Site Collection and Site level.

This could be pretty tedious if you have to do this for every site in the web application. So, if you need to do this fast for multiple sites and don’t have direct access to the Welcome Page link, what do you do? <pausing for dramatic effect> You use Power-Shell!

Below is a script I created that will prompt to enter in the Web Application and Welcome Page and then loop through each site in the Web Application and set the Welcome Page. Save this as a .ps1 file and run it from the SharePoint 2010 Management Shell:

$webapp = Read-Host "Enter Web Application"

$welcomepage = Read-Host "Enter Welcome Page"

$webs = Get-SPSite -WebApplication

$webapp -Limit All | Get-SPWeb -Limit All

foreach ($web in $webs){

$rootFolder = $web.RootFolder

$rootFolder.WelcomePage = $welcomepage

Write-Host "Setting"$web.Title"homepage to"$welcomepage    $rootFolder.Update()



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

ASP.NET Dynamic HTML with HTML Controls

There are many times that you need to process a data list and generate HTML to output to an ASP.NET page. Most of the time, a Repeater is the best choice for this, but sometimes using a Repeater is not practical because the HTML has too many levels or is built with too many conditionals. In these cases, you could use a StringBuilder and output the result to a Literal control, but working with strings can be error prone. My preferred method to add a Placeholder control to the page and then just add Controls to its Controls collection in the code behind.

This works out well if you want to use web controls, but not if you want to use HTML controls. One of the goals with web controls was to make the properties more consistent across controls, but if you are familiar with HTML, then using the HTML controls might be more natural because the properties match the attributes you are already familiar with.

The issue with HTML Controls is that they are not as friendly to work with. For many elements, such as divs, you will have to use the HtmlGenericControl class. Although web controls have a property for CssClass, this is not available for HTML controls. If you want to add a new div control with a CSS class, you will end up with something like this:

var divContainer = new HtmlGenericControl(“div”);
divContainer.Attributes.Add(“class”, “div-class”);

To improve on this syntax, I added a derived control with a CSSClass property:

public class HtmlClassedControl : HtmlGenericControl
public HtmlClassedControl(string tag) : base(tag)

public string CssClass
get { return GetAttribute(“class”); }
set { Attributes.Add(“class”, value); }

Next, I added another derived class for the div control to eliminate the constructor parameter:

public class HtmlDiv : HtmlClassedControl
public HtmlDiv() : base(“div”)

With these changes, the syntax becomes:

var divContainer = new HtmlDiv {CssClass = “div-class”};

That may not seem like much of a change, but if you are adding 20 or 30 controls with nesting and looping, it makes a huge difference in readability and maintainabiliy… not to mention less code to write. You could easily add classes for other HTML elements such as ordering list, list item, etc.