[Solved] How to get an IMedia from a MediaPicker in a POC controller
c
Umb 10.6.1 In a controller, I'm deleting a content node and need to delete it's associated image held in a single image media picker. If I've got
Copy code
var eventImage = eventItem.GetValue("eventImage")
giving me a key and a mediaKey as guids, I'm guessing that's the reference to the media picker. How do I get from there to an IMedia that I can use in mediaService.MoveToRecycleBin(IMedia, int32)? Thanks.
a
You can inject
IMediaService
and then call the
GetById
method (it also works for a GUID key).
Not that there is much information in the API docs, but it's this method that you should use: https://apidocs.umbraco.com/v10/csharp/api/Umbraco.Cms.Core.Services.IMediaService.html#Umbraco_Cms_Core_Services_IMediaService_GetById_Guid_
c
But that's the problem, I need to get the ID. eventItem.GetValue("eventImage") doesn't get me it because that's the image picker. How do I get down the level of the image? Easy in a view, don't know how in a POCcontroller.
a
What do you need the ID for? The GUID key should be sufficient for looking up the
IMedia
.
c
Well the Guid then. I can't get that either.
a
The media picker saves a
key
and
mediaKey
for each picked media. So
mediaKey
is the GUID key that you need.
c
Ok, and how do I get that? The eventImage is just an "object" so I need to know exactly what to call as I don't get any hints off intellisense.
Sorry to sound so thick but there's precious little useful documentation really. A "reference" doesn't really show you how.
a
From your initial description, I thought you had those already. If the
eventImage
property on your page holds a valid media picker value, your
eventImage
variable will be a string with the RAW JSON value. Illustrated via a Razor partial, you could parse it something like this:
Copy code
csharp
@using Newtonsoft.Json
@{

    //object eventImage = eventItem.GetValue("eventImage")

    object eventImage = @"[
        {
            ""key"": ""d38b4eba-cb02-4090-b7ad-8547d3c6fc35"",
            ""mediaKey"": ""257374a5-d30e-43c8-9f03-bf0ee0ec7c17""
        },
        {
            ""key"": ""913f4ac1-d9f2-41cc-bfbd-7790c50f6834"",
            ""mediaKey"": ""f725b3be-8823-401d-ac08-a44722951296""
        }
    ]";


    if (eventImage is string str && str.DetectIsJson()) {

        List<MediaItemDto> items = JsonConvert.DeserializeObject<List<MediaItemDto>>(str)!;

        foreach (MediaItemDto item in items) {
            <pre>@item.Key => @item.MediaKey</pre>
        }

    }

}

@functions {

    public class MediaItemDto {

        public Guid Key { get; set; }

        public Guid MediaKey { get; set; }

    }

}
c
Crikey, no wonder I was having trouble, lol. I was pursuing this course, thought I was nearly there...
Copy code
string eventImagePicker = eventItem.GetValue<string>("eventImage");

                        dynamic eventImagePickerJson = JsonConvert.DeserializeObject(eventImagePicker);

                        string mediaKey = eventImagePickerJson[1];
But not quite 😉
Aha, got it!...
Copy code
string eventImagePicker = eventItem.GetValue<string>("eventImage");

dynamic eventImagePickerJson = JsonConvert.DeserializeObject(eventImagePicker);

string mediaKey = eventImagePickerJson[0].mediaKey;
Just need a quick new Guid() and I'm done 🙂 Thanks for your inspiration! 🙂
a
Might be good with some validation - eg. if you have an event without any images. In that case
eventImagePicker
might be either
null
or an empty JSON array. I think
dynamic
is also considered bad practice these days. But if it works, it works 😁
c
I agree, but whether there's an image is already checked further up the code. 🙂
142 Views