Thursday 23 February 2012

Tag a SharePoint 2010 Site with PowerShell (PropertyBags)

Here's an easy one. My current project requires quite alot of automation. It's a migration but prior to migration I am rolling out over 100 sites of varied type, each type of site has it's own roll-out requirements such as web parts, lists, content types and so forth all being built, configured and deployed from powershell.

It's all publishing sites, so saving as a template is out of the question and building the custom site definitions in Visual Studio 2010 (although would be best practice) - was also ruled out during the planning phases. I'll explain why in some other post.

A common question, and perhaps limitation of SharePoint 2010 is the lack of site directories - you can't associate meta-data with a site or subsite like you can an item and so forth.

For this reason, we're using site property bags, on deployment we set the metadata for each site, then as my later scripts go round to make changes they can reference the site properties to add a little logic.

First, get the site properties.

$siteUrl = http://sharepointSite/subsite/targetSite
$web = Get-SpWeb $siteUrl
$web.properties

you will see some standard out of box properties like default language, a property for the custom upload page.

To add properties to the site property bag:

$siteUrl = http://sharepointSite/subsite/targetSite
$web = Get-SpWeb $siteUrl
$web.properties["customProperty1"]="propertyValue"
$web.properties["customProperty2"]="Another propperty value"

calling another $web.properties will show you your new properties,
you can reference them directly using $web.properties["customProperty1"].


Have Fun!

Ryan

3 comments: