Sunday, November 6, 2011

Property Bags in SharePoint 2010

Property Bags enables the developers to add properties to the SharePoint objects like

a. Farm (SPFarm class)
b. Web application (SPWebApplication class)
c. Site collection (SPSite class)
d. Site (SPWeb class)
e. List (SPList class)

Using Property Bags, developers can avoid storing Key/Value pairs in Web.Config or at some other location. Internally, Property bags are implemented as a hash table of property names and values.

We can set the Property Bag values using two ways:
a. Using SharePoint Designer
b. Programatically

a. Using SharePoint Designer to store Property Bags
(i). Open the site in SP Designer 2010 and click on Site Options.







(ii). Under Parameters, click on Add and Start Adding Key/Value Pairs !


















To read this MyKey value, just use the below code.

SPSite siteCollection = new SPSite("http://sharepoint");
SPWeb website = mySite.RootWeb;
string MyValue = website.AllProperties["MyKey"]);


b. Programatically.

Use the below code to set the property bags Programatically.

SPSite siteCollection = new SPSite("http://sharepoint");
SPWeb website = mySite.RootWeb;
website.Properties.Add("MyKey", "MyValue");
website.Properties.Update

3 comments:

  1. Nice post. Here is one more post explaining to get and set property bag values in SharePoint 2013 apps using CSOM

    http://sureshpydi.blogspot.in/2013/05/set-and-get-property-bag-values-in.html

    ReplyDelete
  2. Using Property Bags, developers can avoid storing Key/Value pairs in Web.Config or at some other location. Internally, Property bags are implemented as a hash table of property names and values. printed carrier bags

    ReplyDelete