Copied to clipboard

Flag this post as spam?

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


  • Eran Bodokh 11 posts 101 karma points
    Sep 25, 2017 @ 13:22
    Eran Bodokh
    0

    Programmatically set the value of a MultipleMediaPicker field

    I have a custom document type with a MultipleMediaPicker field/control, and I just want to assign a media object to it, basically set its value to an existing media object. Here is the document type:

    UserPhoto field

    Here is my code:

    var images = ApplicationContext.Current.Services.MediaService.GetDescendants(1360);
    var person = ApplicationContext.Current.Services.ContentService.GetChildrenByName(1358, "23329741").FirstOrDefault();
    var h = person.GetValue<int>("userId");
    var oop = images.FirstOrDefault(x => x.Name.StartsWith(h.ToString()));
    person.SetValue("userPhoto", oop.GetValue(umbracoFile));
    

    The code executes without any issues, the oop variable is a media object and I checked that it does indeed contain the image in question, but no matter how i try to set the userPhoto field it remains empty with no image. I don't know value userPhoto expects, is it an ID of the media object? An umbracoFile? I couldn't find the answer anywhere.

  • Markus Johansson 1701 posts 4879 karma points c-trib
    Sep 25, 2017 @ 20:31
    Markus Johansson
    100

    Hi!

    A media picker are only storing the ID of the media item, you could try something like this:

    person.SetValue("userPhoto", oop.Id);
    

    Are you calling the save-method of the content service as well?

    ContentService. SaveAndPublishWithStatus(person);
    

    Cheers!

  • Eran Bodokh 11 posts 101 karma points
    Sep 26, 2017 @ 07:00
    Eran Bodokh
    0

    Thank you Markus! I've been breaking by head over this simple thing for a while now, much appreciated!

  • Markus Johansson 1701 posts 4879 karma points c-trib
    Sep 26, 2017 @ 08:21
    Markus Johansson
    0

    Cheers!

  • 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