Category: Microsoft SharePoint

SP 2010 Site Collection PowerShell Script

The following script is what we’ve been using to create site collections with their own database in SharePoint 2010.

Add-PSSnapin Microsoft.SharePoint.PowerShell –ErrorAction SilentlyContinue

## Configure the script
$databaseName = "SP_Content_Database_Name"
$webApplicationUrl = "http://localhost"
$siteCollectionUrl = "http://localhost/sites/siteName"
$siteCollectionName = "Site Collection Name"
$siteCollectionOwner1 = "DOMAIN\user1"
$siteCollectionOwner2 = "DOMAIN\user2"
$siteCollectionTemplate = "STS#1"

## Create the database
New-SPContentDatabase -Name $databaseName -WebApplication $webApplicationUrl

## Create the site collection
New-SPSite -URL $siteCollectionUrl -OwnerAlias $siteCollectionOwner1 -SecondaryOwnerAlias $siteCollectionOwner2 -ContentDatabase $databaseName -Name $siteCollectionName -Template $siteCollectionTemplate

Download the above code at:

If you need a list of site templates available on the machine you can run:

Get-SPWebTemplate | Sort-Object "Title"

SharePoint Web Part XML Properties

When building a web part for SharePoint, you can use the .webpart file to set default values for almost all of the properties of that web part. But many people don’t know what those properties are or what they pertain to. Below is a listing of all the properties of an empty web part:

    <webPart xmlns="">
            <type name=yourTypeHere" />
            <importErrorMessage>Cannot import this Web Part.</importErrorMessage>
                <property name="AllowClose" type="bool">True</property>
                <property name="AllowConnect" type="bool">True</property>
                <property name="AllowEdit" type="bool">True</property>
                <property name="AllowHide" type="bool">True</property>
                <property name="AllowMinimize" type="bool">True</property>
                <property name="AllowZoneChange" type="bool">True</property>
                <property name="CatalogIconImageUrl" type="string" />
                <property name="ChromeState" type="chromestate">Normal</property>
                <property name="ChromeType" type="chrometype">Default</property>
                <property name="Description" type="string">My WebPart</property>
                <property name="Direction" type="direction">NotSet</property>
                <property name="ExportMode" type="exportmode">All</property>
                <property name="Height" type="unit" />
                <property name="HelpMode" type="helpmode">Navigate</property>
                <property name="HelpUrl" type="string" />
                <property name="Hidden" type="bool">False</property>
                <property name="Title" type="string">Web Part Title</property>
                <property name="TitleIconImageUrl" type="string" />
                <property name="TitleUrl" type="string" />
                <property name="Width" type="unit" />

For a definition and list of possible values for each of these properties, you can visit the MSDN pages for:

What about custom properties?

You can use SharePoint to create a reference for the properties of your web part. Install your web part, setup the web part the way you would like it by default, and then use the Verbs menu to Export the web part. Locate the exported file, open it in your favorite editor, and your XML is there!  Simple right?

Programmatically Update Web Part Properties

Have you ever wanted to give your users access to the personalized properties of a web part through a custom interface built into a web part? In ASP.NET, updating these properties and more importantly persisting the changes was a pretty straight forward process. Unfortunately, the same straight forward techniques can not be used when developing a SharePoint web part.

The code below is a very simple web part inheriting from System.Web.UI.WebControls.WebParts.WebPart. It consists of a TextBox and a Button but it illustrates a complete solution for persisting personalization changes to the SharePoint database programmatically.

using System;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;

namespace UpdatePropertiesWithCode.UpdateMeWebPart
    public class UpdateMeWebPart : System.Web.UI.WebControls.WebParts.WebPart
        protected TextBox MessageTextBox;
        protected Button SaveButton;

        [Category("My Configuration")]
        [WebDescription("Message to display.")]
        public string Message { get; set; }

        protected override void CreateChildControls()
            MessageTextBox = new TextBox();
            MessageTextBox.Text = this.Message;

            SaveButton = new Button();
            SaveButton.Text = "Save";
            SaveButton.Click += new EventHandler(SaveButtonClick);


        protected void SaveButtonClick(object sender, EventArgs e)
            this.Message = MessageTextBox.Text;

            SPWeb web = SPContext.Current.Web;
            SPFile file = web.GetFile(HttpContext.Current.Request.Url.ToString());
            SPLimitedWebPartManager manager = file.GetLimitedWebPartManager(PersonalizationScope.User);
            System.Web.UI.WebControls.WebParts.WebPart webPart = manager.WebParts[this.ID];
            ((UpdateMeWebPart)webPart).Message = this.Message;

                web.AllowUnsafeUpdates = true;
                web.AllowUnsafeUpdates = false;

Setup a ‘Get Public Key’ Tool

If you’re a SharePoint developer, you’ve run into public key tokens but what you might not know is that Visual Studio comes with an executable that can make retrieving them a much easier process.

The following steps will add a “Get Public Key’ option to your Tools menu in Visual Studio:

  • Click ‘Tools > External Tools’
  • Click ‘Add’ and a new entry will appear
  • In the ‘Title’ field type: Get Public Key
  • Click the ‘…’ button and navigate to the appropriate sn.exe
  • If VS 2008: C:\Program Files\Microsoft SDKs\Windows\v6.0A\Bin\sn.exe
  • If VS 2010*: C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin\sn.exe
  • In the ‘Arguments’ field type: -Tp “$(TargetPath)”
  • Select ‘Use Output window’ checkbox
  • Click ‘OK’

External Tools screenshot

This will save you a ton of time if you are still using tools like Reflector to get the public key.

* This path is from a 64-bit version of Windows.

Article: 10 Steps to Better SharePoint Development

Save it. Print it. Do whatever you normally do to catalog your favorite articles but this article by David Mann isn’t just a must read for SharePoint developers everywhere. This article can be used as a checklist or roadmap in any of your future SharePoint development.

I will clarify his point on unit testing though. We all know it can be done with expensive products but they aren’t needed to get worthwhile coverage ratios. Steps 3 and 6 (Patterns and Refactoring) go a long way in making your SharePoint code more unit test friendly. The Patterns and Practice group he mentions have examples of SharePoint code using Repository, MVP, and Service Locator patterns. As you add each one of these to your code, you’ll see it become more flexible and unit test friendly.

Also, his point on throwing exceptions doesn’t mean run out and drop unnecessary try/catch blocks all over your code. If you’re building a library and you find yourself with an empty catch block, you need to assess whether that try/catch is actually necessary or if you can just allow the error to bubble up.