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;
} 
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s