I am trying to save a setting in Kentico and I get this error:
The settings key with code name 'AvalaraOrderStatus' already exists.
I already created the setting and I have saved a value to it. The code worked fine in Kentico 8, but I was asked for no SiteInfiIdentifer.
Here is the code I created to make the setting:
//if the setting does not exist, then create it
if (SettingsKeyInfoProvider.GetSettingsKeyInfo(siteName + ".AvalaraOrderStatus", siteID) == null)
{
// Create and set up new SettingsKey
SettingsKeyInfo si = new SettingsKeyInfo();
si.KeyName = "AvalaraOrderStatus";
si.KeyDisplayName = "Avalara Order Status";
si.KeyDescription = "Avalara order status for this site";
si.KeyType = "string";
si.KeyValue = string.Empty;
si.KeyCategoryID = category.CategoryID;
SettingsKeyInfoProvider.SetSettingsKeyInfo(si);
}
The code throws the error on the last line. Here is my code:
int currentSiteID = CMS.SiteProvider.SiteContext.CurrentSiteID;
SiteInfoIdentifier siteId = new SiteInfoIdentifier(currentSiteID);
//update settings in system
SettingsKeyInfoProvider.SetValue(siteName + ".AvalaraOrderStatus", siteId, orderStatus.Trim());
A few things to note:
SettingsKeyInfoProvider.GetSettingsKeyInfo
method does not need to be prefixed with the site name. This is why a site identifier is provided (in your case, the SiteID
). Otherwise, you might be getting a null
value every time the if
statement evaluates, which is why the setting key is being recreated even if it exists. So that should be:SettingsKeyInfoProvider.GetSettingsKeyInfo("AvalaraOrderStatus", siteID)
SettingsKeyInfoProvider.SetValue
method - no need to prefix the site name:SettingsKeyInfoProvider.SetValue("AvalaraOrderStatus", siteId, orderStatus.Trim())
CurrentSiteID
integer is a valid SiteIdentifier, so there is no need to explicitly instantiate a SiteInfoIdentifier
object:SettingsKeyInfoProvider.SetValue("AlavaraOrderStatus", CMS.SiteProvider.SiteContext.CurrentSiteID, orderStatus.Trim())