{"id":747,"date":"2011-12-19T22:28:46","date_gmt":"2011-12-19T22:28:46","guid":{"rendered":"http:\/\/www.stuartroberts.net\/?p=747"},"modified":"2012-04-18T10:11:01","modified_gmt":"2012-04-18T09:11:01","slug":"remove-property-sppropertybag","status":"publish","type":"post","link":"https:\/\/www.stuartroberts.net\/index.php\/2011\/12\/19\/remove-property-sppropertybag\/","title":{"rendered":"Remove property from SPPropertyBag"},"content":{"rendered":"<p>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?<\/p>\n<p>Logically, you&#8217;d think the following code would do this for you:<\/p>\n<pre lang=\"csharp\">\r\nwebObject.Properties.Remove(\"yourkey\");\r\nwebObject.Properties.Update();\r\n<\/pre>\n<p>But, I&#8217;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&#8217;s property bag, try setting the property to null, as shown below.<\/p>\n<pre lang=\"csharp\">\r\nwebObject.Properties[\"yourkey\"] = null;\r\nwebObject.Properties.Update();\r\n<\/pre>\n<p>Now when you reload the SPWeb object and check for the property, it won&#8217;t exist.  It seems that the <em>Remove<\/em> method is either flaky, or the change is not being persisted after applying the update on the <em>Properties<\/em> property.<\/p>\n<p>Of course, you really should be using the new <em>AllProperties<\/em> 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 <em>AllProperties<\/em> will filter down to <em>Properties<\/em> but not the reverse.  Most (if not all) of the internal properties use the old <em>Properties<\/em> property, so there are still certain cases where you&#8217;d want to manipulate them.  How you work with them is slightly different, for example:<\/p>\n<pre lang=\"csharp\">\r\n\/\/ Remove from old style Properties\r\nwebObject.Properties[\"yourkey\"] = null;\r\nwebObject.Properties.Update();\r\n\r\n\/\/ Remove from new AllProperties\r\nwebObject.AllProperties.Remove(\"yourkey\");\r\nwebObject.Update();\r\n<\/pre>\n<p>Nothing too different, instead of updating the property collection directly you you just update the parent web or site object.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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&#8217;d think the following code would do this for you: webObject.Properties.Remove(&#8220;yourkey&#8221;); &hellip; <a href=\"https:\/\/www.stuartroberts.net\/index.php\/2011\/12\/19\/remove-property-sppropertybag\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_mi_skip_tracking":false,"jetpack_post_was_ever_published":false,"jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":[]},"categories":[3],"tags":[40,81],"jetpack_publicize_connections":[],"aioseo_notices":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/plx2I-c3","_links":{"self":[{"href":"https:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/posts\/747"}],"collection":[{"href":"https:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/comments?post=747"}],"version-history":[{"count":7,"href":"https:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/posts\/747\/revisions"}],"predecessor-version":[{"id":979,"href":"https:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/posts\/747\/revisions\/979"}],"wp:attachment":[{"href":"https:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/media?parent=747"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/categories?post=747"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/tags?post=747"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}