Tuesday, September 7, 2010

Write a value into the property bag - Corey Roth - DotNetMafia.com - Tip of the Day

Write a value into the property bag - Corey Roth - DotNetMafia.com - Tip of the Day

I have ran into a few people that didn't even know what the property bag is on the SPWeb object, so I figured that would be a good topic for a post. The property bag exposed by the Properties and AllProperties collections on the SPWeb object gives the developer a way to cache information on a site. It can be compared to ASP.NET's cache object but the values are shared across the farm and will persist through server reboots. Unfortunately, using the property bag isn't as simple as you would think. In fact to get it working in all situations, it requires a number of settings to allow it to work. It's probably enough to scare a lot of developers away from it.

Before we start, let me talk about the differences between Properties and AllProperties. Properties is a collection of type SPPropertyBag. It is sort of considered deprecated. You can use it to add new items to the property bag, but it will only return a subset of everything in the property bag when retrieving data. Why is that? I have no idea. So instead, Microsoft later implemented the AllProperties Hashtable which contains everything in the property bag.

Here is what the start of our code block looks like to write to the property bag. This assumes it is inside a using block to get access to an SPWeb.

0 comments: