I would like to use special characters as a key in a Name Value List field. Any idea why Sitecore doesn't allow this? Is there any nice way to disallow this validation?

enter image description here

up vote 9 down vote accepted

The Name Value List (and Name Value Lookup List which also inherits from it) only allows keys containing letters and numbers due to the Validate() method in Sitecore.Shell.Applications.ContentEditor.NameValue:

if (!string.IsNullOrEmpty(input) && !Regex.IsMatch(input, "^\\w*$"))
{
  SheerResponse.Alert(string.Format("The key \"{0}\" is invalid.\n\nA key may only contain letters and numbers.", (object) input));
  SheerResponse.SetReturnValue(false);
  return false;
}

Whilst the answer from @alan is correct that the raw data is stored in parameters, the field is able to handle special characters and I have created fields that use a guid as the key (which includes dashes and curly braces). You may want to ensure that values are URL encoded just in case though.

In any case, there are no settings available to change this behvaiour, you have to create a custom field. Unfortunately the Validate() method is private so it's not just a simple case of overriding, but you could provide a new implementation of ParameterChange() method which is where Validate is called from.

protected new void ParameterChange()
{
    ClientPage clientPage = Sitecore.Context.ClientPage;
    if (clientPage.ClientRequest.Source == StringUtil.GetString(clientPage.ServerProperties[this.ID + "_LastParameterID"]) && !string.IsNullOrEmpty(clientPage.ClientRequest.Form[clientPage.ClientRequest.Source]))
    {
        string str = this.BuildParameterKeyValue(string.Empty, string.Empty);
        clientPage.ClientResponse.Insert(this.ID, "beforeEnd", str);
    }
    NameValueCollection form = (NameValueCollection)null;
    System.Web.UI.Page page = HttpContext.Current.Handler as System.Web.UI.Page;
    if (page != null)
        form = page.Request.Form;
    if (form == null) /* || !this.Validate(form)) -- removed validation of Key field */
        return;
    clientPage.ClientResponse.SetReturnValue(true);
}
  • Thanks @jammykam. I would like to publish the exact solution when I am done. I will put it here. – Tamás Tárnok Oct 7 '16 at 6:58
  • 2
    I wrote a blogpost about the exact solution. You can check it here - trnktms.wordpress.com/2016/10/15/… – Tamás Tárnok Oct 15 '16 at 10:29
  • 3
    Hey @TRNKTMS, I suggest linking back to this page on your blog post and also giving credit to Kamruz. The blog post as it stands makes it appear you solved this on your own which is not the case. – Sitecore Junkie Oct 15 '16 at 11:18

I think it might be forbidden as raw value of the field is 'url like' format thus some special characters are forbidden.

Normally you resolve field value in code like this:

Sitecore.Web.WebUtil.ParseUrlParameters(item.Fields['NameValueListField']?.Value);

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.