How to create and update SharePoint Property Bags from a CSV file!
To get articles like this free in your inbox, subscribe to our newsletter.
As you know, SharePoint Property Bags are a powerful mechanism we have available in SharePoint to create and define configuration properties at the different logical levels in the platform architecture: Farm, Web Application, Site Collection, Site, List / Document Library and Folder). SharePoint Property bags are very useful to store configuration properties such as Database connection strings, specific data for the solutions we are building, etc. Finally, It’s a best practice to use Property Bags to store and retrieve such configuration properties required by our SharePoint solutions. In this article I will so you how to create and update SharePoint Property Bags from a CSV file using PowerShell so you can automate the process of maintaining your Property Bags in your SharePoint deployment.
Creating and updating Property Bags at the Farm & Site Collection Level
In order to create and update SharePoint Property Bags, you need to have some knowledge of the SharePoint API in order to use the right class members required to operate with this properties:
- At the Farm level, the SPFarm class stores all the medatada (all the properties) in the Properties property.
- At the Site Collection level, the SPWeb class ( Note: In order to create SharePoint Property Bags at the Site Collection level, you have to create them in the site collection root site) stores this metadata in the AllProperties property. As you can imagine, the same property is available for any SharePoint site in your farm.
Once you know where the metadata is stored, you are ready to create the PowerShell Script to create and update SharePoint Property Bags at the Farm and Site Collection levels (Note: For Sites, Lists/ Document Libraries and Folders is the same idea):
- First, create your source CSV file where you will add some Property Bags you want to add to your SharePoint CloudShare environment. In my case, I have defined a CSV file with the following structure:
As you can see, my CSV file has three columns that will be used in the PowerShell script to determine the scope for the Property bag to be added / updated, the Property Bag key and the Property Bag value (Just remember a SharePoint Property bag is formed by a key and a value).
- In your SharePoint CloudShare environment, open PowerShell ISE and paste the following PowerShell script (the script is available for download in the following link: How to create and update property bags from a CSV file):
If ((Get-PSSnapIn -Name Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue) -eq $null )
{ Add-PSSnapIn -Name Microsoft.SharePoint.PowerShell }
$host.Runspace.ThreadOptions = “ReuseThread”
#
#Function that reads the CSV file containing the Property Bags
#
function ReadPropertyBagsFromFile
{
param ($sInputFile,$sSiteCollectionUrl)
try
{
$bFileExists = (Test-Path $sInputFile -PathType Leaf)
if ($bFileExists) {
“Loading $sInputFile for processing…”
$tblData = Import-CSV $sInputFile
} else {
Write-Host “File $sInputFile not found. Stopping loading process !” -ForegroundColor Red
exit
}
Write-Host “Creating/Updating Property Bags …” -ForegroundColor Green
foreach ($fila in $tblData)
{
#$fila
$sPBScope=$fila.PropertyBagScope.ToString()
$sPBKey=$fila.PropertyBagKey.ToString()
$sPBValue=$fila.PropertyBagValue.ToString()
#Calling the function that creates/updates the property bags
CreateUpdatePropertyBag -sPropertyBagScope $sPBScope -sPropertyBagKey $sPBKey -sPropertyBagValue $sPBValue
}
}
catch [System.Exception]
{
write-host -f red $_.Exception.ToString()
}
}
#
#Function that creates/updates each property bag
#
function CreateUpdatePropertyBag
{
param ($sPropertyBagScope,$sPropertyBagKey,$sPropertyBagValue)
try
{
switch ($sPropertyBagScope)
{
“Farm” {
$spfFarm=Get-SPFarm
$sPropertyBag=$spfFarm.Properties[$sPropertyBagKey]
if($sPropertyBag -eq “”)
{
Write-Host “Adding Property Bag $sPropertyBagKey ato the farm!!” -ForegroundColor Green
$spfFarm.Properties.Add($sPropertyBagKey,$sPropertyBagValue)
}else{
Write-Host “Updating Property Bag $sPropertyBagKey in the granja” -ForegroundColor Green
$spfFarm.Properties[$sPropertyBagKey]=$sPropertyBagValue
}
$spfFarm.Update()
$sPropertyBag=$spfFarm.Properties[$sPropertyBagKey]
Write-Host “Property bag $sPropertyBagKey has a value $sPropertyBag” -ForegroundColor Green
}
“WebApplication” {
#Code for Web Application Property bags here
}
“SiteCollection” {
$spSite=Get-SPSite -Identity $sSiteCollection
$spwWeb=$spSite.OpenWeb()
$sPropertyBag=$spwWeb.AllProperties[$sPropertyBagKey]
if($sPropertyBag -eq “”)
{
Write-Host “Adding Property Bag $sPropertyBagKey to $sSiteCollection !!” -ForegroundColor Green
$spwWeb.AllProperties.Add($sPropertyBagKey,$sPropertyBagValue)
}else{
Write-Host “Updating Property Bag $sPropertyBagKey for $sSiteCollection” -ForegroundColor Green
$spwWeb.AllProperties[$sPropertyBagKey]=$sPropertyBagValue
}
$spwWeb.Update()
$sPropertyBag=$spwWeb.AllProperties[$sPropertyBagKey]
Write-Host “Property bag $sPropertyBagKey has a value $sPropertyBag” -ForegroundColor Green
}
“Site” {
#Código para Property Bags de Sitio aquí
}
“List” {
#Código para Property Bags de Lista aquí
}
default {
Write-Host “Requested opeartion is not valid!!” -ForegroundColor DarkBlue
}
}
}
catch [System.Exception]
{
write-host -f red $_.Exception.ToString()
}
}
$ScriptDir = Split-Path -parent $MyInvocation.MyCommand.Path
$sInputFile=$ScriptDir+ “\PropertyBags.csv”
$sSiteCollection=”http://<YourSiteCollection>”
Start-SPAssignment –Global
#Calling the function
ReadPropertyBagsFromFile -sInputFile $sInputFile
Stop-SPAssignment –Global
Remove-PSSnapin Microsoft.SharePoint.PowerShell
- As you can see, we are using standard SharePoint cmdlets to get the SPFarm and SPWeb objects required to access respectively to the Properties and AllProperties properties defined in both objects. One we have access to both properties, the process to created and update a Property Bag is quite straightforward using the Add() method for creating a Property Bag and direct property value assignation for updating the Property Bag.
I hope you have enjoyed this post about how to work with SharePoint Property Bags using PowerShell and a CSV file as source for the Property Bags to be created in your SharePoint CloudShare environment. Happy CloudSharing!
What you should do next…
1. Subscribe to our newsletter:
Subscribe to our newsletter below for the latest news, advice and thought-leadership for software professionals. Or visit our blog to browse our most recent articles.
2. Learn how virtual labs can grow your business:
To learn more about how CloudShare helps software organizations grow revenue, increase efficiency and improve quality, visit our resources page. You’ll be able to browser dozens of valuable white papers, eBooks, webinars, case studies, and brochures.
3. Get a FREE, no obligation demo:
Discover just how easy it is to create your cloud environment—in minutes! One of our friendly virtual labs experts will be happy to:
- Show you the platform in action
- Calculate pricing for your business
- Set you up with a 14-day free trial
- Answer any questions you have
- No pressure or obligation