Property bags are power full and good way to store properties and static details that is used in SharePoint. Earlier the most common way was to store for example some URL information in web-config. And still web.config is a good place to store information. Only administrators are able to modify web.config so the information is saved in quite save way. One disadvantage (a big one even) there is with web.config. If you have multi server farm you have to make web.config changes to every server so that everything would work.
However property bags are handles through code or xml file and application server is handling the information so that information is always reachable. You can store information in many different level and object like SPFarm, SPServer, SPWebApplication, SPContentDatabase, SPWeb, SPFolder, SPListItem, SPFile. “Each property bag allows you to store a collection of key/value pairs, each key is unique, and the value has to be a string. If you need more advanced capabilities, you could choose to store complex data in the string using serialization.” Property bag are available both on server solutions and sandbox solutions (from site collection level and below). Even external application can use them if they connect to SharePoint environment through API.
Advantages to use property bags
· There are provided to you out of the box.
· They are saved straight to SharePoint databases.
· There is good and simple API to use them.
· No out of the box UI to modify them. But there is an good farm level solution found from Codeplex http://pbs2010.codeplex.com/
How to use property bag?
But let’s have an example of how you can use property bag in our solutions. This example is light version from real life case. Our customer wanted to have web part that can be used in multiple sites and multiple times in a site. The request also was that they want to make multiple predefined style themes from which site administrators can choose from. This meant that on a site there could be multiple web parts but all of them have different style layout (font color etc.).
On this post I will leave the creating of visual web part with editor part out. But you can download the source code from the link below and there are a lot of examples found with Google of course.
Our web part fulfilled the requirements in a following way.
a) Make the web part based on visual web part and add custom style classes to every control on the page.
b) Themes can be made so that there will be necessary CSS styling done and user can change this style to a single web part.
c) Make editor part where users sees drop down box and styles (like Blue Theme, Red Theme) from where to choose from.
d) This list of styles should be centrally defined by administrators.
e) Theme list will be saved in property bag and administrator can update it with pbs2010 tool found from Codeplex.
First we need to create the actual property bag. On this demo we create the properties on site (or web) level.
1. Create an empty module to your visual studio solution.
2. Into element.xml file here is the xml definition we used to list the possible style themes. More information about property bag xml schema can be found here: Property Bag Schema.
<?xml version="1.0" encoding="utf-8"?>
<Property Name="pbpost.messageboxstyling" Type="string" Value="Blue Theme, Red Theme" />
3. Save the element file and create a feature that will install the module to the site.
Getting the property bag information
As I told there is a editor part where we want to show list of possible styles in drop down list. The styles are saved to web property bag with a key pbpost.messageboxstyling. Teams are saved as comma separated string so we also have to decrypt them as single items.
Here is the create CreateChildControl function where we are creating the drop down list and start to fill that with the values from property bag.
protected override void CreateChildControls()
//Create the Dropdown list
SelectedNewCssClass = new DropDownList();
//Add the item from property bag
Label lblControl = new Label();
lblControl.Text = "Box Custom CssClass";
catch (Exception ex)
SPDiagnosticsService.Local.WriteTrace(0, new SPDiagnosticsCategory("UsingPropertyBag", TraceSeverity.Unexpected, EventSeverity.Error), TraceSeverity.Unexpected, ex.Message, ex.StackTrace);
And here is the actual function that reads the values from the bag and saves them as items to the drop down list.
private void addCSSStyles(DropDownList CssClass)
if (SPContext.Current.Site.RootWeb.AllProperties.ContainsKey ("pbpost.messageboxstyling"))
string cssValues = SPContext.Current.Site.RootWeb.AllProperties ["pbpost.messageboxstyling"].ToString().Split(',');
foreach (string value in cssValues)
So here was the short example and information about SharePoint property bags. You can find the complet Visual Studio 2010 solution from my SkyDrive (zip file names as UsingPropertyBag.zip
 Course 10232A: Designing and Developing Microsoft® SharePoint® Server 2010 Applications material