Problem: Cannot retrieve Propertybag value in SharePoint 2010

I ran into a problem that took me some time to solve and by posting it i might be able to help you save some time.

This is the situation: I was deploying a couple of webparts to SharePoint 2010. The webparts all connected to the same SQL 2008 R2 Databases. So I needed to figure out a way to store the connection string.

There are a couple of options, like hardcoding the connectionstring into the webpart, using custom settings like “modify shared webpart” settings or maybe even use a web.config. For me the best thing to use in this case is the propertybag, it can be configured and changed whenever you like. It can be set on different SharePoint levels:

  • Farm
  • Web application
  • Site collection
  • Site
  • List

So if needed I can even use different connectionstrings for different webparts, located on different levels withing my farm. There are several ways to create values in the property bag:

  • Custom code
  • PowerShell
  • Tooling like (pbs2010)
  • SharePoint Designer

Because I needed a solution I could easily work with and I also want it to be reusable I chose the SharePoint tool pbs 2010, it lets you edit yout propertybag settings from the central admin.

So I installed pbs2010 and added my connectionstring, works like a charm. Ok now I just need a piece of code to read it from the propertybag, Google is mostly your best friend in these cases so I found some code:

// Open SharePoint site
SPSite siteCollection = new SPSite("http://sharepoint");
SPWeb site = siteCollection.RootWeb;

//Adding SPSite Property
site.Properties.Add("SPSiteKey", "SPSiteValue");
Example: read property from propertybag - site level
// Open SharePoint site
SPSite mySite = new SPSite("http://sharepoint");
SPWeb myWeb = mySite.OpenWeb("Your Web Name")

//Adding SPWeb Property
myWeb.Properties.Add("SPWebKey", "SPWebValue");

//Reading SPWeb Property
string MyValue = myWeb.Properties["SPWebKey"]);

The page I got it from is here. And to my despair, it didn’t work. I got all properties except the one I added with pbs2010. At first I thought it might be a bug in the tool. So I searched the codeplex site and there did not seem to be any other users with the same problem. I tested my theory by adding a propertybag value with SharePoint designer. Again I could read every property with my code except the property added with SharePoint designer. So it could not be the pbs tool, assuming that SharePoint designer did work as it should.

After a lot of searching I found this article it showed me there are two properties to read the propertybag values, SPWeb.Properties and SPWeb.AllProperties. I am not going to do a comparison, but I do know that SPWeb.AllProperties returns all properties and SPWeb.Properties does not return properties added with psb2010 nor properties added by SahrePoint Designer 2010.

My Conclusion, when retreiving values from the propertybag, use SPWeb.AllProperties!

Untill we meet again, Happy SharePointing.