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: https://gist.github.com/4488619

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="http://schemas.microsoft.com/WebPart/v3">
            <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;