Copied to clipboard

Flag this post as spam?

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


  • jake williamson 207 posts 873 karma points
    Sep 23, 2018 @ 10:13
    jake williamson
    0

    the 'MultiNodeTreePicker' returns 'IEnumerable<Udi>' not 'IEnumerable<IPublishedContent>' when umbraco starts up?!

    this is a weird one...

    i've a property on a doctype that uses the MultiNodeTreePicker.

    in a controller, i do this to get the property value and it works as expected:

    var values = model.Content.GetPropertyValue<IEnumerable<IPublishedContent>>("myMultiNodeTreePickerPropertyAlias");
    

    trick is, i want to put the guid's of the items that are picked into my examine index so i tried this in my start up class (inheriting from IApplicationEventHandler):

    public void OnApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
    {
        var helper = new UmbracoHelper(UmbracoContext.Current);
    
        var myindexer = ExamineManager.Instance.IndexProviderCollection["MyIndexer"];
        myindexer.GatheringNodeData += (sender, e) => mySite_GatheringNodeData(sender, e, helper);
    }
    
    private static void myindexerSite_GatheringNodeData(object sender, IndexingNodeDataEventArgs e, UmbracoHelper helper)
    {
        try
        {
            var content = helper.TypedContent(e.NodeId);
    
            var values = content.GetPropertyValue<IEnumerable<IPublishedContent>>("myMultiNodeTreePickerPropertyAlias");
    
            var valueKeys = new List<string>();
            foreach (var value in values)
            {
                valueKeys.Add(value.GetKey().ToString());
            }
    
            e.Fields.Add("values", string.Join(" ", valueKeys));
        }
        catch (InvalidOperationException)
        {
        }
    }
    

    it throws an exception saying that 'vales' is null! weird...

    a bit of debugging and it turns out the property isn't returning as IEnumerable<IPublishedContent> it's coming back as IEnumerable<Udi>. if i update the code to:

    private static void myindexerSite_GatheringNodeData(object sender, IndexingNodeDataEventArgs e, UmbracoHelper helper)
    {
        try
        {
            var content = helper.TypedContent(e.NodeId);
    
            var values = content.GetPropertyValue<IEnumerable<Udi>>("myMultiNodeTreePickerPropertyAlias");
    
            var valueKeys = new List<string>();
            foreach (var value in values)
            {
                valueKeys.Add(GuidUdi.Parse(value.ToString()).Guid.ToString());
            }
    
            e.Fields.Add("values", string.Join(" ", valueKeys));
        }
        catch (InvalidOperationException)
        {
        }
    }
    

    then everything works as expected!

    so, my question is: why is the 'MultiNodeTreePicker' returning an IEnumerable<IPublishedContent> in a controller but then returns a IEnumerable<Udi> in the start up class?!

    bit of a head scratcher...

  • Søren Gregersen 441 posts 1884 karma points MVP 2x c-trib
    Sep 23, 2018 @ 18:07
    Søren Gregersen
    100

    Could it be that the value converter is not registered at the time when the event is first fired?

    I’m not really sure how/when they are registered, but I would look into that, to see if that is the issue.

  • jake williamson 207 posts 873 karma points
    Sep 24, 2018 @ 08:35
    jake williamson
    0

    hi søren,

    i think you might be on to something there...

    tbh it doesn't make a hill of beans getting back the udi's rather than the IPublishedContent as i'm only storing the guids. if i needed more info from the node i could use 'helper.TypedContent()' so i could get the properties.

    interesting though!

    thank you for your reply,

    jake

  • jake williamson 207 posts 873 karma points
    Sep 24, 2018 @ 18:57
    jake williamson
    0

    one additional thing that does bug me is that every so often the property value comes back as null even though there are selected items in the backoffice...

    i'm wondering if this is due to me getting the typed content using the helper? it might be better to store the raw value in the index and then process it. i've given it a go and the values from a mntp get stored as a csv of udi's eg:

    umb://member/d432cc516cea46cbbb782ffb0cbb8d01,umb://member/05416b895118413892fc68d621ce29da
    

    i guess it would be then a case of splitting, parsing and saving the guids?

    interesting...

Please Sign in or register to post replies

Write your reply to:

Draft