Wednesday, September 16, 2015

Export and import property bag values from SharePoint using PowerShell

This one is for us oldies still doing on-premises work with SharePoint.

image

I’m currently working on a 2010 to 2013 migration and we need to move some values from the farm and web application property bags as we’re not doing a 1:1 migration, but merging multiple 2010 farms solutions into one new 2013 farm.

So I googelized a bit and came up with this script which can export and import property bag values from SPFarm, SPWebApplication, SPSite and SPWeb. Should be pretty self explanatory to use but it could look like this:

./Export-Import-Props.ps1 –Url http://myserver -CsvFile ./test.csv -Level SPWebApplication -Mode Export

param (
[Parameter(Mandatory=$True)]
[string]$Url,

[Parameter(Mandatory=$True)]
[string]$CsvFile,

[Parameter(Mandatory=$True)]
[ValidateSet("SPFarm","SPWebApplication","SPSite","SPWeb")]
[string]$Level,

[Parameter(Mandatory=$True)]
[ValidateSet("Import","Export")]
[string]$Mode
)

if($Level -eq 'SPFarm' ){
$parent = Get-SPFarm
$properties = $parent.Properties
}
elseif($Level -eq 'SPWebApplication' ){
$parent = Get-SPWebApplication -Identity $Url
$properties = $parent.Properties
}
elseif($Level -eq 'SPSite' ){
$parent = Get-SPSite -Identity $Url
$properties = $parent.RootWeb.Properties
}
elseif($Level -eq 'SPWeb' ){
$parent = Get-SPWeb -Identity $Url
$properties = $parent.Properties
}

if($Mode -eq 'Export'){
$properties.getenumerator() | % {new-object psobject -Property @{Key = $_.name;Value=$_.value} } | export-csv $CsvFile -notype -Delimiter ";"
}
elseif($Mode -eq 'Import'){
$importProperties = Import-Csv -Delimiter ";" -Path $CsvFile
if($importProperties -eq $null){ exit }
$importProperties.getenumerator() | % {
if($properties.ContainsKey($_.Key)) {$properties[$_.Key] = $_.Value} else { $properties.Add($_.Key,$_.Value) }
}
$parent.Update()
}