What is Property bag?
Property bag is basically a hash table of key-value pair options. Property bag feature is available in Windows SharePoint services 3.0. It allows you to add properties to objects in SharePoint site. Property bag helps you to store meta data as key-value pairs example Connection Strings, file paths, server names and other settings in SharePoint application.
Advantages of using Property bag element
Property helps in enabling client classes to add new properties.
Enables you to assign new values to those new properties.
You can read and manage the current values of the properties.
Usually configuration parameters stored in configuration file(Web.config) for the web application.Imagine if you have to maintain many entries in Web.config file for web application and sites in the web application then it is hard to maintain these keys. In this case you should use SharePoint bag. There is no out of the box user interface available for setting and reading the property bag values, however you can download this wsp to have the interface in central admin for setting the property bag values.
You can use SharePoint designer for getting the Property bag settings. Go to Site | Site settings | then click on the parameter tab option. On this tab user will be able to manipulate all of their custom property bag values. The following code allows you set the Property bag values
SPSecurity.RunWithElevatedPrivileges(delegate() { try { using (SPSite RootSite = new SPSite(URL)) { using (SPWeb SiteCollection = RootSite.OpenWeb()) { try { SiteCollection.AllowUnsafeUpdates = true; // Get connection string from Property bag if (SiteCollection.AllProperties.ContainsKey("ConnectionString")) { ConnectionString = SiteCollection.AllProperties["ConnectionString"].ToString(); } // Set siteID in the Property bag SiteCollection.Properties["siteID"] = siteID; SiteCollection.Properties.Update(); SiteCollection.AllowUnsafeUpdates = false; } catch (Exception ex) { //Handle Exception } } } } catch(Exception ex) { } })
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