SharePoint 2013 Hosting :: How to create and update sharepoint property bags

How to create and update sharepoint property bags from a csv file

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.

ahp_freehostSHP

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 environment. In my case, I have defined a CSV file with the following structure:

a

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:

 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.