Craig100
11/17/2023, 11:40 AMvar 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.Anders Bjerner
11/17/2023, 12:34 PMIMediaService
and then call the GetById
method (it also works for a GUID key).Anders Bjerner
11/17/2023, 12:36 PMCraig100
11/17/2023, 1:14 PMAnders Bjerner
11/17/2023, 1:19 PMIMedia
.Craig100
11/17/2023, 1:24 PMAnders Bjerner
11/17/2023, 1:26 PMkey
and mediaKey
for each picked media. So mediaKey
is the GUID key that you need.Craig100
11/17/2023, 1:27 PMCraig100
11/17/2023, 1:30 PMAnders Bjerner
11/17/2023, 2:00 PMeventImage
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:
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; }
}
}
Craig100
11/17/2023, 2:03 PMstring eventImagePicker = eventItem.GetValue<string>("eventImage");
dynamic eventImagePickerJson = JsonConvert.DeserializeObject(eventImagePicker);
string mediaKey = eventImagePickerJson[1];
But not quite 😉Craig100
11/17/2023, 2:08 PMstring 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! 🙂Anders Bjerner
11/17/2023, 2:15 PMeventImagePicker
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 😁Craig100
11/17/2023, 2:57 PMA hub and casual space for you to interact with fellow community members and learn more about Umbraco!
Powered by