Copied to clipboard

Flag this post as spam?

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


  • karen 186 posts 461 karma points
    Apr 15, 2016 @ 19:57
    karen
    0

    how do I get the ID from a ContentPicker ?

    I want to get the value for the Content Picker property on it. (this shouldn't be so hard!)

    The node is strongly typed - returned from a Linq search:

    var nodes = Umbraco.TypedContentAtRoot().DescendantsOrSelf("sPlan").Where( ... blah blah blah ...);
    

    so nodes.First() will be of type IPublishedContent. I want to get the value of a contentPicker that has 1 item in it, but can only find examples for if this was dynamic.

    From this page: https://our.umbraco.org/documentation/getting-started/backoffice/property-editors/built-in-property-editors/content-picker I tried the sample code:

    var node = Umbraco.TypedContent(nodes.First().GetPropertyValue<int>("sContent"));
    

    but does not work, node gives 'Object reference not set to an instance of an object' error.

    This code:

    var s = nodes.First().GetProperty("sContent");
    <div>@s.Value</div>
    

    will output this:

        <MultiNodePicker type="content"><nodeId>2341</nodeId></MultiNodePicker>
    

    So I am wondering how do I get that 2341 into some var so I can then get the node and on to the content I really want? Is there some 'easy' way or do I have to parse the XML in the string?

    Thank you

  • Nicholas Westby 2054 posts 7103 karma points c-trib
    Apr 15, 2016 @ 20:13
    Nicholas Westby
    1

    This is how I do it (IIRC, this is because different versions of Umbraco store things differently):

    https://github.com/rhythmagency/rhythm.umbraco.extensions/blob/e3cce1803863d452dce76784ad554ddb6529dce0/trunk/Rhythm.Extensions/Rhythm.Extensions/ExtensionMethods/PublishedContentExtensionMethods.cs#L213

    And here's a snapshot of that code snippet for your reference:

    /// <summary>
    /// Gets the ID's of the picked nodes.
    /// </summary>
    /// <param name="source">The node with the picker property.</param>
    /// <param name="propertyAlias">The alias of the picker property.</param>
    /// <param name="recursive">Recursively check ancestors (false by default)?</param>
    /// <returns>
    /// The picked node ID's.
    /// </returns>
    /// <remarks>
    /// This is faster than LocalizedGetPickedNodes when you only need node ID's.
    /// </remarks>
    public static IEnumerable<int> LocalizedGetPickedNodeIds(this IPublishedContent source,
        string propertyAlias, bool recursive = false)
    {
        if (recursive) {
            while (source != null) {
                if (source.HasValue(propertyAlias, false)) {
                    break;
                }
                source = source.Parent;
            }
        }
        if (source != null) {
            string pickerValue = source.LocalizedPropertyValue<string>(propertyAlias);
            if (pickerValue != null)
            {
                int nodeId;
    
                // Integer, CSV, or XML?
                if (int.TryParse(pickerValue, out nodeId)) {
                    yield return nodeId;
                }
                else if (CsvRegex.IsMatch(pickerValue))
                {
                    var pickedNodes = StringUtility.SplitCsv(pickerValue);
                    foreach (var nodeItem in pickedNodes)
                    {
                        nodeId = int.Parse(nodeItem.Trim());
                        yield return nodeId;
                    }
                }
                else if (!string.IsNullOrWhiteSpace(pickerValue as string)) {
                    var pickedNodes = new DynamicXml(pickerValue as string);
                    foreach (dynamic nodeItem in pickedNodes) {
                        nodeId = int.Parse(nodeItem.InnerText);
                        yield return nodeId;
                    }
                }
    
            }
        }
    }
    
  • karen 186 posts 461 karma points
    Apr 15, 2016 @ 20:43
    karen
    0

    ok, well still more complicated than I was hoping.

    I trimmed down your example and wrote a very short method that works for me in this case.

        /// <summary>
        /// Gets the ID's from data type content picker stored as XML
        /// </summary>
        /// <param name="source">The node with the picker property.</param>
        /// <param name="propertyAlias">The alias of the picker property.</param>
        /// <returns>
        /// The picked node ID's.
        /// </returns>
        public static IEnumerable<int> getContentPickerIDs(IPublishedContent source, string propertyAlias) {
            if (source != null) {
                var pickerValue = source.GetProperty(propertyAlias).DataValue.ToString();
                if (pickerValue != null) {
                    var pickedNodes = new DynamicXml(pickerValue);
                    foreach (dynamic nodeItem in pickedNodes) {
                        var nodeId = int.Parse(nodeItem.InnerText);
                        yield return nodeId;
                    }
                }
            }
        }
    

    Thanks for the quick response!

Please Sign in or register to post replies

Write your reply to:

Draft