Copied to clipboard

Flag this post as spam?

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


  • Steve Smith 75 posts 157 karma points
    Jan 03, 2018 @ 13:52
    Steve Smith
    1

    Programmatically setting "Media picker" value in Umbraco 7.7

    Hi all,

    I've got an event handler that automatically creates and saves a thumbnail image when the user uploads a PDF to the Media tab in the back office.

    The event handler includes code to set the value of a Media Picker field on the original "File" media node, linking it with the automatically generated thumbnail "Image" node.

    I've done the same thing a few times in the past, and this has always worked for me:

    mediaItem.SetValue("thumbnailMediaNodeId", thumbnailNode.Id);
    

    However, I noticed in the latest version of Umbraco (I'm using 7.7.4), it seems that this is not the way to populate a Media Picker field anymore.

    When I try and reference the thumbnail in the front-end views, I get an error something like:

    String "1234" is not a valid udi (where 1234 is the node Id of the thumbnail image).

    If I save the node manually in the back office, then it fixes the problem (because - I guess - it re-saves the media picker field correctly).

    I have come up with a work-around, but I'd rather do it properly in the first place.

    So my question is... if its no longer the right thing to just set the Media Picker field value to be a Media Node Id (string or integer), what is the correct way to set it?

    Thanks!

    Steve.

  • Paul Seal 478 posts 2608 karma points MVP 4x c-trib
    Jan 03, 2018 @ 15:22
    Paul Seal
    103

    Hi Steve You need to do it like this now:

    contentItem.SetValue("thumbnail", mediaItem.GetUdi().ToString());
    

    Where contentItem is an IPublishedContent item representing the page and mediaItem is an IMedia item representing the thumbnail image.

    The data type for thumbnail needs to be MediaPicker2

    See here for an example where I am doing this:

    https://github.com/prjseal/CodeShareUmbracoStarterKit/blob/master/src/CSUSK.Library/PackageActions/CreateMediaHandler.cs

  • Steve Smith 75 posts 157 karma points
    Jan 03, 2018 @ 15:33
    Steve Smith
    0

    Many thanks for replying, that's done the trick :)

  • Paul Seal 478 posts 2608 karma points MVP 4x c-trib
    Jan 03, 2018 @ 15:35
    Paul Seal
    0

    You're welcome Steve.

    It's nice to be able to answer one before someone else got in there first :^D

  • Wojciech Tengler 88 posts 161 karma points
    Sep 09, 2020 @ 11:01
    Wojciech Tengler
    0

    I think it is not needed to create new topic because my problem relates very well to this.

    I have also Media Picker but osbolete version which stores int id instead of udi.

    I'm setting this Media Picker property during saving event and selected image appears in editor but I have this field set as required and validation still displays that this field is required.

    Is there any way to revalidate this property after setting proper value at server side?

Please Sign in or register to post replies

Write your reply to:

Draft