Property Bags

Working with Property Bags Programmatically

Posted on Updated on

Property Bags are very powerful, they can store information in an Hash Table in IIS at several 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)

I wrote an Article for 2010 in here, take a look

//Add
protected static void AddProperty(SPWeb web, string key, string value)
{
    web.AllowUnsafeUpdates = true;
    web.Properties[key] = value;
    web.AllProperties[key] = value;
    web.Update();
    web.Properties.Update();
} 
//Remove
protected void RemoveProperty(SPWeb web, string key)
{
    web.AllowUnsafeUpdates = true;
    web.AllProperties.Remove(key);
    web.Properties[key] = null;
    web.Update();
    web.Properties.Update();
}

//Get
protected string GetPropertyValue(SPWeb web, string key)
{
    return web.AllProperties.ContainsKey(key) ? web.AllProperties[key].ToString() : null;
}