Remove Keys From PropertyBag on FeatureDeactivating Override

December 13 2011 10 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% [?]

10 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 -

  4. Cassandra D. Everhart says:

    This article proof that there are writers who really care about the quality of content they share. Thank you for being this kind of writer. http://www.procarpetcleaningorlando.com/lakemary

  5. site says:

    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.

  6. If you are looking for the best sexy chat experience on the whole web you must to visit hausfrauensex berlin

  7. Tranny Sex UK is the most popular web page in United Kingdom for a free chat with local girls

  8. Sex Kontakt in Göppingen is fine web place for your own sexy chat pleasure

  9. Angel17 says:

    What an informative post. Thanks for sharing! ADVANCE Appliance Ltd

  10. Spend some extratime and find hot ladies for casual contacts in Italy Trans Sesso a Bari

Leave a Reply