Share via


SharePoint 2010: Introduction to Property Bags

Overview / Survival Guide

For those not familiar with the Property Bags, what are they?

A place to store metadata or properties of your SharePoint sites. The Property Bags are implemented as an hash table consisting of property names and values ​​to Level Site Collection etc ... On the other hand we can store in the web.config parameters, but these parameters are not the same level that we want (see levels) which in itself is a disadvantage.

Media Type/Task

LEVELS

Property Bags can be defined in the following levels:

Farm (SPFarm class)
Web application (SPWebApplication class)
Site collection (SPSite class)
Site (SPWeb class)
List (SPList class)

EXAMPLE

Analyzing a real example, imagine that we have our application divided by areas, each area is bound and a Site Collection, from the start would be good to know in which area we are, good? .. On the other hand we can persist a Customer ID of a LOB Application at the Site level, so we just have to set it and invoke it without having to always make requests to the database. Consider the diagram below

In this case we can persist the information from the area where and know when the ribbon is visible or not. In the 3rd example we can persist the Customer ID and Company simply by using small methods or properties to get information as we see below.

Code  

//SET
 
SPSite siteCollection = new  SPSite("http://site/"); 
SPWeb site = siteCollection.RootWeb; 
site.Properties.Add("SiteKey", "SiteValue");  
site.Properties.Update(); 
 
//READ
 
SPWeb.AllProperties["SiteKey"].ToString() 
 
//REMOVE
 
SPWeb.AllProperties.Remove("SiteKey"); 
SPWeb.Properties["SiteKey"] = null; 
SPWeb.Update(); 
SPWeb.Properties.Update();