Copied to clipboard

Flag this post as spam?

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


  • George 30 posts 122 karma points
    Jan 17, 2018 @ 17:41
    George
    0

    Clearing the value of MultiNodeTreePicker2 on IContent object

    I would like to know how to clear out the set value of a property on an IContent object. The value is associated with a property that uses the Umbraco.MultiNodeTreePicker2 property type. I am hooking into the ContentService.Saving event handler and am running something to the effect:

    var mediaPath = content.GetValue<string>("eventTemplate"); // returns a UDI like umb://media/1234adf...
    IMedia template = ApplicationContext.Current.Services.MediaService.GetById(new Guid(mediaPath.Substring(mediaPath.LastIndexOf('/') + 1)));
    content.SetValue("title", template.GetValue<string>("title"));
    content.SetValue("eventTemplate", null);
    

    The goal is to pull field values from a media node that serves as a "template" to auto-fill the fields of the same name on the content node. Once the fields are filled, I want to clear out the selected template so the auto-fill code doesn't run with each save.

    I've tried all of the following:

    // these come back with template still selected
    content.SetValue("eventTemplate", null);
    content.SetValue("eventTemplate", ""); 
    
    // these throw an error and appear to expect a string
    content.SetValue("eventTemplate", new Udi[0]);
    content.SetValue("eventTemplate", new List<string>());
    

    Incidentally, I'd welcome input as to the best way to get a media object from a Udi.

  • George 30 posts 122 karma points
    Jan 24, 2018 @ 21:40
    George
    100

    I had some thoughts as to how to debug this further. After trying something I just happened to navigate away from the node and come back to it, only to notice the field had cleared.

    It turns out that the call below does the trick on the back end -- the data gets saved as I wanted (with a cleared eventTemplate field).

    content.SetValue("eventTemplate", null);
    

    The problem is in the front end (of the back end, ha!). It appears the angular-driven node editor doesn't sync up that field nicely when it receives the updated data. This results in:

    1. The node editor appearing to have the previously selected node still selected. Indeed, if you save again, it will send the previously selected node as the current selection.
    2. Navigating away from the node and coming back to it reveals the truth that there is no node selected any more.

    This behavior is more acceptable for my purposes than what I had thought was happening, although it is somewhat buggy and quirky. If anyone has any thoughts as to how to fix this, I'd be happy to hear them. As it is, this appears to me to be a bug.

Please Sign in or register to post replies

Write your reply to:

Draft