Copied to clipboard

Flag this post as spam?

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


  • Damiano Piva 5 posts 85 karma points
    Aug 22, 2023 @ 13:39
    Damiano Piva
    0

    ContentService IContent GetValue from MediaPicker3 from unpublished version

    Hi I get image url from a saved content and not from the current published element, but I don't want to use JObject or JArray.

    How I can retrieve values directly in models?

    var content = ContentService.GetById(Model.Id);
    string imagePropertyValue = content.GetValue<string>("immagine");
    // imagePropertyValue is: [{"key":"49d738c1-296b-4687-8ef6-519eb483d1d9","mediaKey":"8106bdd2-19bf-4f7c-8c51-a2fb0c86f345"}]
    dynamic imageProperty = JArray.Parse(imagePropertyValue).First();
    var media = MediaService.GetById(Guid.Parse(imageProperty.mediaKey.ToString()));
    string umbracoFileValue = media.GetValue<string>("umbracoFile");
    // umbracoFileValue is: {"src":"/media/0s4bnan5/percorsodiformazione-test.png"}
    dynamic umbracoFile = JObject.Parse(umbracoFileValue);
    string url = umbracoFile.src.ToString();
    

    Thankyou for help

    Damiano

  • Marc Goodson 2155 posts 14408 karma points MVP 9x c-trib
    Aug 23, 2023 @ 09:37
    Marc Goodson
    0

    Hi Damiano

    When you use published content to access properties, the raw saved values in the database are run through an appropriate PropertyValueConverter to turn them into Models that make working with them easier in implementation scenarios, controllers/views etc.

    Usually, you wouldn't use the ContentService in the front end of your site as it operates directly on the database, and is slow, but there isn't usually the need to convert the stored JSON into nice models for presentation, which is why you have the raw Json to play with :-(

    The MediaPickerWithCropsValueConverter is here:

    https://github.com/umbraco/Umbraco-CMS/blob/7b336c45f7cdfcc7e9cfd0f33bedd63c06333c1b/src/Umbraco.Infrastructure/PropertyEditors/ValueConverters/MediaPickerWithCropsValueConverter.cs#L78

    I'm wondering if this would perhaps show a way to convert the JSON stored for the MediaPickerWithCrops into more familiar models?

    regards

    Marc

Please Sign in or register to post replies

Write your reply to:

Draft