Deleting a setting programmatically

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
11 anos atrás
Hello,

I've created a Campaign Monitor plugin for 2.40, and in the Uninstall method of the installation service I would like to delete any settings that the plugin has saved, however I can't find a way of doing this. The Setting Service has a DeleteSetting method that requires a Setting object as the only parameter. Unfortunately I can't seem to create a Setting object even if I know the key for the setting. The setting service has a method GetSettingById which requires me to know the Id (which I dont) and then there is a method GetSettingByKey<> but this doesnt return a setting object, it returns a type.

Does anyone know how to get hold of a setting when you know the key?

Thanks
Al
11 anos atrás
higgsy wrote:
Hello,

I've created a Campaign Monitor plugin for 2.40, and in the Uninstall method of the installation service I would like to delete any settings that the plugin has saved, however I can't find a way of doing this. The Setting Service has a DeleteSetting method that requires a Setting object as the only parameter. Unfortunately I can't seem to create a Setting object even if I know the key for the setting. The setting service has a method GetSettingById which requires me to know the Id (which I dont) and then there is a method GetSettingByKey<> but this doesnt return a setting object, it returns a type.

Does anyone know how to get hold of a setting when you know the key?

Thanks
Al


You probably store your settings in a class that inherits ISettings, as in

public class CampaignMonitorSettings : ISettings {}


Then you can delete the settings using:

_settingService.DeleteSetting<CampaignMonitorSettings>();


:)
11 anos atrás
What version is that available in? Im using 2.40 and the optino you have suggested is not supported.....
11 anos atrás
DeleteSetting with generic param is not in 2.40.  You'll need to just DeleteSetting() each of your settings individually.
11 anos atrás
New York wrote:
DeleteSetting with generic param is not in 2.40.  You'll need to just DeleteSetting() each of your settings individually.


Hi - and how would you do that? DeleteSetting takes a Setting as its parameter, but all I know is the key of the setting. How basically do you create a setting object from the key???

Thanks
Al
11 anos atrás
I needed something similar for nop2.50 and ended up with

            foreach(var setting in _settingService.GetAllSettings())
            {
                if ( setting.Key == "campaignmonitorsettings.key1"  ||
                    setting.Key == "campaignmonitorsettings.key2" ||
                    setting.Key == "campaignmonitorsettings.key3"
                    //etc )
                {
                    _settingService.DeleteSetting( setting.Value ) ;
                }
            }


replace key1,2,3 with the names of setting properties and campaignmonitorsettings should be the name of the settings class
11 anos atrás
Hi keesjan,

Thanks for your response. Strangely, that doesnt work for me in 2.40 - I get the error message The object cannot be deleted because it was not found in the ObjectStateManager.

My code is as so:

foreach (var setting in _settingService.GetAllSettings())
            {
                if (setting.Key.StartsWith("widgets.googleanalytics.googleid.") || setting.Key.StartsWith("widgets.googleanalytics.javascript."))
                {
                    _settingService.DeleteSetting(setting.Value);
                }
            }

It's most odd that it doesnt work. Any thoughts?

Regards,
Al
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.