Friday, January 7, 2011

Property Bag issues

This time a minor blog post. It’s more for myself so I don’t forget.

This time it’s about the property bag in various object in SharePoint 2010. The problem I came across was how to remove a property bag key-value. But lets start at the beginning.

1- Add an value to the property bag.
 
//set value on (a new) Property bag 
if (web.Properties.ContainsKey("ExpireOn"))
{
web.Properties["ExpireOn"] = "some sting";
}
else
{
web.Properties.Add("ExpireOn","some string");
}
web.Properties.Update();

2- Remove an item from property bag

//Remove Property bag  
//Does not work
////web.Properties.Remove("ExpireOn");
////web.Properties.Update();

//Does work
web.Properties["ExpireOn"] = null;
web.Properties.Update();

As you can see, using Remove doesn’t work. You should set the value to null, call Update() on properties to persist the change to the database. At this point the null-ed Property bag key-value will be removed from the collection.

2 comments:

  1. It is a useful post. i was searching my mistake why the key is not removed from the property bag. and my mistake is i didn't assign null instead of that i called remove method. this post shows i didn't do any mistake. thanks dude

    ReplyDelete
  2. But it is working even if i don't use Add method. if i add a record directly like propertyBag["newKey"]="newValue" it is working. is it ok. or will it be a problem.

    ReplyDelete

Note: Only a member of this blog may post a comment.