Remove Keys From PropertyBag on FeatureDeactivating Override

December 13 2011 3 comments

You can add keys to spweb propertybag trough a feature using CAML:

<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <PropertyBag ParentType="Web" RootWebOnly="False">
    <Property Name="MyCustomProperty"
             Type="string" Value="MyValue" />
  </PropertyBag>
</Elements>

The property is written to SPWebs AllProperties propertybag. When you deactivate the feature, it does not remove the keys.
If you want to remove the keys when deactivating the feature, add Event Receiver to your Feature and the following code to the FeatureDeactivating override.

public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
{
    var web = (SPWeb)properties.Feature.Parent;
    var allowUnsafeUpdates = web.AllowUnsafeUpdates;
    var elementDefinitions = properties.Definition.GetElementDefinitions(CultureInfo.CurrentCulture);

    foreach (var n in from SPElementDefinition elementDefinition in elementDefinitions
                        where elementDefinition.ElementType == "PropertyBag"
                        select elementDefinition.XmlDefinition into xml
                        where xml.Attributes != null && xml.Attributes["ParentType"].Value == "Web"
                        from n in
                            (from XmlNode xNode in xml
                            let xmlAttributeCollection = xNode.Attributes
                            where xmlAttributeCollection != null
                            select xmlAttributeCollection["Name"])
                        select n)
    {
        web.AllProperties.Remove(n.Value);
        web.Update();
    }
    web.AllowUnsafeUpdates = allowUnsafeUpdates;
    web.Update();
}

This sample only removes the keys, if the propertybag ParentType is “Web”, which means the key will be written to SPWebs AllProperties propertybag.

***********************************************
Jerry Seinfeld: Why not? We’re neighbors. What’s mine is yours.
Cosmo Kramer: [leaning against the door-frame and looking around in wonder] Really?
***********************************************

Popularity: 1% [?]

3 comments to “Remove Keys From PropertyBag on FeatureDeactivating Override”

  1. Leo Surrency says:

    Suh studies are important and re good to keep tab onthe happenings n th insurnce secto.Many peοple love thei pet a fthey were th own hilren, and ωill go to any lesngth to protect the animal’s health and wll-being.You shoul be er lar in creatng your custod agrement.

  2. Evelyn says:

    Your style is really unique compared to other people I have read
    stuff from. Many thanks for posting when you have the opportunity, Guess I’ll just bookmark
    this web site.

    My homepage :: ask.fm annon tracker [Evelyn]

  3. ipk.khsu.ru says:

    great insight. Really enjoyed skimming through
    this blog. Keep up the good work and to everyone keep on learning!

    Feel free to visit my page :: free psn code generator with proof –
    ipk.khsu.ru -

Leave a Reply