Wednesday, July 11, 2012

Storing values in SharePoint 2010 Property Bag

Property bags?
Property bags are place to store metadata or properties.The property bags are implemented as hash table consisting of property names and values.

Property bags can be defined at following levels:


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


Property bags can be set using SharePoint designer and programmatically.

Using SharePoint Designer 2010:

  • Open Site in SharePoint Designer 2010
  • Go to Site->Site Options
  • Click on Parameters tab where you can see the list of existing properties, from the same place you can even perform add/modify/remove actions.


Programmatically

To Read the Set Key:


SPSite siteCollection = new SPSite("");
SPWeb website = mySite.RootWeb;
string MyValue = website.AllProperties["KeyName"]);

To Set the Value
SPSite siteCollection = new SPSite("  ");
SPWeb website = mySite.RootWeb;
website.Properties.Add(" KeyName ", "KeyValue");
website.Properties.Update