Have you ever tried to remove a property from a SPWeb object and after applying the update, when you next check for the property it is still there?
Logically, you’d think the following code would do this for you:
But, I’ve found, if you reload the SPWeb object and check for the key, it still exists, even though it had been previously removed. To successfully remove a property from the web’s property bag, try setting the property to null, as shown below.
webObject.Properties["yourkey"] = null; webObject.Properties.Update();
Now when you reload the SPWeb object and check for the property, it won’t exist. It seems that the Remove method is either flaky, or the change is not being persisted after applying the update on the Properties property.
Of course, you really should be using the new AllProperties property instead, where this issue is not present. This property was added to 2010 and uses a HashTable instead of the SPPropertyBag. Items added to AllProperties will filter down to Properties but not the reverse. Most (if not all) of the internal properties use the old Properties property, so there are still certain cases where you’d want to manipulate them. How you work with them is slightly different, for example:
// Remove from old style Properties webObject.Properties["yourkey"] = null; webObject.Properties.Update(); // Remove from new AllProperties webObject.AllProperties.Remove("yourkey"); webObject.Update();
Nothing too different, instead of updating the property collection directly you you just update the parent web or site object.