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:

 C# |  copy 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.

Tip: Locating the PublicKeyToken of your assembly

Today I ran into a problem concerning a custom webpart. The webpart compiled, so I shipped it ;). But the designtime/compile success was not a guarantee for runtime success. I ran into the following error: Could not load file or assembly ‘xxx, Version=, Culture=neutral, PublicKeyToken=xxx’ or one of its dependencies. The system cannot find the file specified.

So what does this mean? For me the problem was that my libraries were re-signed and in my code there was something like this: <%@ register=”” tagprefix=”CustomControls” namespace=”xxx” assembly=”SharePointControls, Version=, Culture=neutral, PublicKeyToken=123456789″> While in the GAC the assembly had another PublicKeyToken.

When resigning your assemblies, the public key tokens change and an assembly (dll) with the same name is seen as a different version. Which is quite logical because that is what signing with a strong name is meant for.

Next problem is how to get the strong name of my newly signed Assembly, so we can change it in our code. A solution is to deploy your assembly in the GAC and look the PublicKeyToken up in the Assembly folder. There is another solution which is more permament colleague of mine showed it to me:

In Visual Studio, click “Tools” -> “External Tools”. This will open a window in which you can add the command SN as a tool. In the window click “Add” and fill in the fields as seen in the image below. Don’t forget to check the “Use Output window” check box.

PublicKeyToken Tool

PublicKeyToken Tool

When done you can use the tool by selecting the assembly of which you want the PublicKeyToken and clicking the tool  in Visual Studios “Tools” menu. The PublicKeyToken will be displayed in the output window.

That’s all for now folks, happy SharePointing.