Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • John Bergman 483 posts 1132 karma points
    Aug 14, 2017 @ 19:22
    John Bergman
    0

    How do you set a multi-select dropdown's value? (plus sample for single select included)

    Below is a code snippet of how I am setting the value of a dropdown that is a single-selection dropdown. Can anyone tell me how you would set multiple values into the "value" property, is it simply a comma delimited list, or something else

    /// <summary>
    /// Set the default value of the specified property to the desired value if (and only if) the value is blank
    /// or null.
    /// </summary>
    /// <param name="node">Current Content Node to update if needed</param>
    /// <param name="propName">Property Name to set</param>
    /// <param name="desiredDefaultValue">desired value (string) that the field should be set to</param>
    private void SetDropDownListSingleValue(IContent node, string propName, string desiredDefaultValue)
    {
      if (node.HasProperty(propName))
      {
        var currentValue = node.Properties[propName].Value;
        if (string.IsNullOrEmpty(DataUtilities.ObjectAsString(currentValue)))
        {
          var dtDefs = umbraco.library.GetPreValues(node.Properties[propName].PropertyType.DataTypeDefinitionId);
          dtDefs.MoveNext();
    
          foreach (XPathNavigator item in dtDefs.Current.SelectChildren("preValue", ""))
          {
            if (desiredDefaultValue.Equals(item.InnerXml, StringComparison.OrdinalIgnoreCase))
            {
              node.Properties[propName].Value = item.GetAttribute("id", "");
              break;
            }
          }
    
          ApplicationContext.Current.Services.ContentService.SaveAndPublishWithStatus(node);
        }
      }
    }
    
  • John Bergman 483 posts 1132 karma points
    Aug 19, 2017 @ 23:33
    John Bergman
    0

    Anyone? Anyone?

  • Simon Yohannes 58 posts 188 karma points
    Jan 14, 2019 @ 11:57
    Simon Yohannes
    1

    you've probably figured it out but it's comma separated list of prevalue id for anyone else who sees this

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies