Tuesday, July 22, 2014

Property Bags in SharePoint 2013 - Store, Retrieve Custom Configurations

As its name suggests, Property bag feature provides a convenient way to store and retrieve custom configurations, instead of having to store them in web.config files. Property bags in SharePoint stores configuration data in the Hash-Table (Key-Value) format. We can store and retrieve properties at the following levels: 
  1. Farm (SPFarm )
  2. Web application (SPWebApplication )
  3. Site collection (SPSite )
  4. Site (SPWeb )
  5. List (SPList )
  6. Folder (SPFolder)
  7. File (SPFile)
  8. List Item (SPListItem)
Property bags can be created, edited and deleted at all of these level.  Property bag keys are case sensitive!

How to use property bag SharePoint?
If your SharePoint application uses any specific setting for each and individual SharePoint object, such as Web Application, Site Collection, Site, etc. you have maintain those entries in the config files. Here is where SharePoint property bags can be a great helper.

There is no out-of-box user interface to manage property bag settings in SharePoint. We use object model codes to set/get properties programmatically. Any method that interacts with SharePoint object properties such as AddProperty, DeleteProperty, GetProperty, SetProperty doesn't has any effect on "Properties" property. You have to access "AllProperties" instead.  "Property" property is available for backward compatibility.

Managing SharePoint Property bag using PowerShell:
Here is an example to manage Property bag at the site level using PowerShell. 
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

$WebURL ="http://intranet.crescent.com/sites/Sales/APAC"
#Get the Web Object
$Web = Get-SPWeb -Identity $WebURL

#Variables for Key-Value 
$PropertyName ="masterPage"
$PropertyValue ="Crescentv1.Master"

#add property bag in sharepoint 2010/2013
$web.AddProperty($PropertyName,$PropertyValue) #add property bag
Write-host "Created new Property: "$PropertyName

#how to read property bag in sharepoint - query property bag
Write-host "Retrieved $PropertyName Value:" $web.GetProperty($PropertyName)   

#set property bag sharepoint 2010 powershell
$PropertyNewValue ="Crescentv2.Master"
$web.SetProperty($PropertyName, $PropertyNewValue)
Write-host $PropertyName updated with new value:$PropertyNewValue

#Search for a Property
     #access property bag in sharepoint
     write-host Property Found! Value: $web.GetProperty($PropertyName)
#Remove Property from Property Bag
write-host "Property $PropertyName has been deleted!"

#Get all Properties from sharepoint property bag 


Same code can be translated in to C# object model to use with Event Receiver, Web Part, etc - Add new property bag at site collection level: Here is a SharePoint property bag example:
#Get Site collection
SPSite site = new SPSite("http://your-sharepoint-site-url");

//Add new Property bag key and value
//Old Method
//site.Properties.Add("MasterPage", "Crescentv1");

//create property bag in sharepoint
site.AllProperties["MasterPage"] = "Crescentv1.master";
Tips: You can also manage property bag settings using SharePoint Designer!

SharePoint property bag codeplex Tools:
There are some codeplex utilities to manage SharePoint property bag settings.
  1. http://pbs2013.codeplex.com/
  2. http://sp2010dynsettings.codeplex.com/
  3. http://spm.codeplex.com/ - Can be used as a Property bag editor,viewer as well

SharePoint property bag access denied!
If you don't have permission to SharePoint objects you can't interact with them. E.g. To access/add SharePoint Farm property bag you must be a  SharePoint farm administrator. You may get "Access Denied" Error if not! Use Elevated Privileges to store and retrieve from such objects.

SharePoint property bag limitations: As its a HashTable - you can store data values up to 4 KB in size limit.

You might also like:
SharePoint Usage Reports
Usage reports, collaboration and audit for SharePoint.
Document SharePoint Farm
Automatically generate SharePoint documentation.

Check out these SharePoint products:

No comments :

Post a Comment

Please Login and comment to get your questions answered!

You might also like:

Related Posts Plugin for WordPress, Blogger...