简体   繁体   中英

The settings key with code name 'XX' already exists

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:

  • The first parameter of the 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)
  • The same applies for the SettingsKeyInfoProvider.SetValue method - no need to prefix the site name:
    SettingsKeyInfoProvider.SetValue("AvalaraOrderStatus", siteId, orderStatus.Trim())
  • The 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())

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM