Unable to remove key from property bag.

Aug 12, 2010 at 3:43 PM

I have a site feature (scoped at Web level). I have following inside the feature activate and deactivate events. However the RemoveKeyFromPropertyBag method call doesn't seem to remove the Key from the property bag. I have a user control in a webpart which on load conditionally checks for the existence of the key. It always returns true.

 

public override void FeatureActivated(SPFeatureReceiverProperties properties)

{

using (SPSite mySite = new SPSite("http://myserver/sites/test" ))

{

 if (mySite != null)

 

{

 

configMgr.SetWeb(mySite.RootWeb); 

// Store configuration data at the SPWeb level.

bag = configMgr.GetPropertyBag(ConfigLevel .CurrentSPWeb);

   

configMgr.SetInPropertyBag("TestKey.Name", "XXX", bag); 

}

}

}

 

 

public override void FeatureDeactivating(SPFeatureReceiverProperties properties)

{

IPropertyBag bag = configMgr.GetPropertyBag(ConfigLevel .CurrentSPWeb);

using (SPSite mySite = new SPSite("http://myserver/sites/test"))

 

{

if (mySite != null)

}

}

}

{

configMgr.SetWeb(mySite.RootWeb);

configMgr.RemoveKeyFromPropertyBag("TestKey.Name", bag);

 

 Following code is in the Page_Load of the user control---------------------------

 IConfigManager configMgr = SharePointServiceLocator.GetCurrent().GetInstance<IConfigManager>();

using (SPSite site = new SPSite("http://myserver/sites/test")){

configMgr.SetWeb(site.RootWeb);

IPropertyBag bag;

bag = configMgr.GetPropertyBag(ConfigLevel.CurrentSPWeb);

if (configMgr.ContainsKeyInPropertyBag("TestKey.Name", bag))

{

lblCompanyName.Text = configMgr.GetFromPropertyBag<string>("TestKey.Name", bag);}

}


IPropertyBag bag;

 
Coordinator
Aug 12, 2010 at 7:42 PM

In this case you are actually storing the data on the rootweb, not on the web the feature is installed to.  This still should work, and it works in my repro.  So I'm really not sure what the issue is that you are experiencing.  I assume you have the feature installing and removing the settings separate from your web part.

My repro has 2 features, the first only manages the settings and is scoped at the web.

   [Guid("1a3ca33c-5bad-4ee1-95f5-fb97ec3562ec")]
    public class Feature2EventReceiver : SPFeatureReceiver
    {
        // Uncomment the method below to handle the event raised after a feature has been activated.

        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            SPWeb web = properties.Feature.Parent as SPWeb;

            var cfgMgr = SharePointServiceLocator.GetCurrent().GetInstance();
            cfgMgr.SetWeb(web);
            var bag = cfgMgr.GetPropertyBag(ConfigLevel.CurrentSPWeb);
            cfgMgr.SetInPropertyBag("TestKey.Name", "XXX", bag);

            cfgMgr = SharePointServiceLocator.GetCurrent().GetInstance();
            cfgMgr.SetWeb(web.Site.RootWeb);
            bag = cfgMgr.GetPropertyBag(ConfigLevel.CurrentSPWeb);
            cfgMgr.SetInPropertyBag("TestKeySite.Name", "YYY", bag);
        }


         //Uncomment the method below to handle the event raised before a feature is deactivated.

        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
            SPWeb web = properties.Feature.Parent as SPWeb;

            var cfgMgr = SharePointServiceLocator.GetCurrent().GetInstance();
            cfgMgr.SetWeb(web);
            var bag = cfgMgr.GetPropertyBag(ConfigLevel.CurrentSPWeb);
            cfgMgr.RemoveKeyFromPropertyBag("TestKey.Name", bag);

            cfgMgr = SharePointServiceLocator.GetCurrent().GetInstance();
            cfgMgr.SetWeb(web.Site.RootWeb);
            bag = cfgMgr.GetPropertyBag(ConfigLevel.CurrentSPWeb);
            cfgMgr.RemoveKeyFromPropertyBag("TestKeySite.Name", bag);
        }

 

The second feature is a site scoped feature containing a web part with a simple literal control:

      protected void Page_Load(object sender, EventArgs e)
        {
            SPWeb web = SPContext.Current.Web;

            var cfgMgr = SharePointServiceLocator.GetCurrent().GetInstance();
            cfgMgr.SetWeb(web);
            var bag = cfgMgr.GetPropertyBag(ConfigLevel.CurrentSPWeb);
            string val = cfgMgr.GetFromPropertyBag("TestKey.Name",  bag);

            cfgMgr = SharePointServiceLocator.GetCurrent().GetInstance();
            cfgMgr.SetWeb(web.Site.RootWeb);
            bag = cfgMgr.GetPropertyBag(ConfigLevel.CurrentSPWeb);
            string siteRootVal = cfgMgr.GetFromPropertyBag("TestKeySite.Name", bag);

            if(val == null)
            {
                ResultsArea.Text = "web val: null";
            }
            else
            {
                ResultsArea.Text = "web val: " +  val;
            }


            if (siteRootVal == null)
            {
                ResultsArea.Text += " ** site val: null";
            }
            else
            {
                ResultsArea.Text += " ** site val: " + siteRootVal;
            }
        }

 Initially both features are installed and the screen shows

 

 

VisualWebPart1 

VisualWebPart1 Web Part Menu
<input id="SelectionCbxWebPartctl00_m_g_061fc5b8_4911_4020_863c_8ec3c193a172" class="ms-WPHeaderCbxHidden" title="Select or deselect VisualWebPart1 Web Part" onclick="TrapMenuClick(event); return false;" onmouseup="WpCbxSelect(event); return false;" onkeyup="WpCbxKeyHandler(event);" type="checkbox" value="on" />  
web val: XXX ** site val: YYY


I then deactive the feature with the settings from Site Actions->Site Settings->Manage Site Features.  The following is now displayed:

 
web val: null ** site val: null

So I am unable to reproduce the behavior you are seeing.  Have you split the features for the web part and config setting?  Are you certain that the config settings really have been removed when you uninstall?

Chris