A useful, but largely unknown, feature in SharePoint is the Property Bag. The Property bag is place where site-wide settings can be kept. It is implemented as a collection of name/value pairs. The reason it is largely unknown is that it is not visible from the user interface, nor can it be seen using SharePoint Designer. It can only be accessed by developers, or by administrators using a tool like SharePoint Manager.
In SharePoint 2007, using the Property Bag in an application meant writing C# code, deployed to the server. SharePoint 2010 gives us a couple of options to access the property bag without managed code on the server. These are sandbox solutions and the Client Object Model.
Accessing the Property Bag using the JavaScript Client Object Model is a straightforward process, as the API has this ability built in:
//wait until client object model dependencies are loaded before executing our code ExecuteOrDelayUntilScriptLoaded(getWebProperties, "sp.js"); var webProperties; function getWebProperties() { var clientContext = new SP.ClientContext.get_current(); webProperties = clientContext.get_web().get_allProperties(); clientContext.load(webProperties); clientContext.executeQueryAsync(Function.createDelegate(this, this.getWebPropertiesSucceeded), Function.createDelegate(this, this.onQueryFailed)); } function getWebPropertiesSucceeded() { //debugger; //use this to force a break here //returns an object with all properties. //Use the quick watch to expand this out to see all of them. var allProps = webProperties.get_fieldValues(); var customProp = ""; //make sure the property is there before using it. if(webProperties.get_fieldValues().CustomSite_Version != undefined) { var customProp = webProperties.get_fieldValues().CustomSite_Version; } alert(customProp); } function onQueryFailed(args, sender) { //handle errors here }
I have not discovered a way to iterate through the web properties like you can with a .Net object, but you can view all of the web properties using the debugger by opening a watch on the webProperties.get_fieldValues() expression:
Here we can see all the site’s properties, including a custom web property I have added: CustomSite_Version.
To fetch a particular property in code, first ensure it is not undefined, then call it much like a .Net-style property:
var customProp = webProperties.get_fieldValues().CustomSite_Version;