El Blanco's Office 2007 Blog

Monday, March 17, 2008

You Can’t Remove an Item from the SPPropertyBag

Hopefully as most of us know by now, the SPWeb class has a property called "Properties" which returns an object of type SPPropertyBag. This is effectively a class sub-classed from the StringDictionary class, and the collection is persisted in the SharePoint content database making it an ideal location to store any custom web-based properties you need to persist.

To store something in the property bag for a web is very easy indeed:

SPWeb web = . . . .
web.Properties.Add("some key", "some value");
web.Properties.Update();

This will store a new key-and-value pair in the StringDictionary and then persist the StringDictionary to the SharePoint content database.

You can retrieve the stored value as follows:

String storedValue = web.Properties["some key"];

The problem I've encountered is that it is not possible to remove this key-and-value pair from the StringDictionary. One would expect the following code to perform this action, but it doesn't work.

web.Properties.Remove("some key");
web.Properties.Update();

If you debug this code and interrogate the StringDictionary after the call to the Remove() method, then you will see that the key-and-value pair has indeed been removed, however for some reason it looks as if the call to the Update() method does not handle the removal correctly as this change is not persisted to the content database.

Anyone found a solution for this ??!

3 Comments:

  • Set the property to String.Empty

    By Anonymous Anonymous, at 10:56 am  

  • set the property to null

    By Anonymous Anonymous, at 3:36 pm  

  • Hi, Can we know by browsing the site can we see these properties or in which table in Content DB these values will be persisted

    By Blogger Unknown, at 10:09 am  

Post a Comment

<< Home