Copied to clipboard

Flag this post as spam?

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


  • Umbraco Newbie 22 posts 124 karma points
    Jul 25, 2017 @ 04:39
    Umbraco Newbie
    0

    Umbraco 7 Archetype unable to get media udi

    Hi

    I have read these posts https://our.umbraco.org/forum/templates-partial-views-and-macros/85993-archetype-mediapicker-udi

    https://our.umbraco.org/forum/using-umbraco-and-getting-started/85598-getting-image-url-from-media-picker-not-working-in-76

    http://www.codeshare.co.uk/blog/how-to-get-the-file-path-of-a-media-item-in-umbraco/

    but I'm still unable to get the value from the media picker.

    On my last attempt, I got the following error.

    Object reference not set to an instance of an object

    If I try the following:

    string img = "";
                foreach (var d in archtypePanel.Properties)
                {
                    if (d.Alias == "imageToDisplay")
                    {
                        img = d.Value.ToString();
                    }
                }
    

    I can see the media UDI: "umb://media/15cc14d318f3444a86ab3bc7fc5787ad" but I'm unable to get any further.

    I have now run into a wall, so any help on solving this issue will be helpful.

    My code is:

    Guid idValue = new Guid(id); var blogPosts = CurrentPage; IPublishedContent blogPosts2 = CurrentPage; var archtypePanel = blogPosts2.GetPropertyValue

            ArchetypeWithTextAndImageModel model = new ArchetypeWithTextAndImageModel
            {
                TextContent = new HtmlString(archtypePanel.GetValue("textRichTextEditor")),
                ImageId     = archtypePanel.GetValue<IEnumerable<IPublishedContent>>("imageToDisplay").ToString()
            };
    
  • Dan Diplo 1554 posts 6205 karma points MVP 6x c-trib
    Jul 25, 2017 @ 11:57
    Dan Diplo
    0

    Do you want to get the actual UDI or just the image? Assuming you have used the default MediaPicker datatype (Umbraco.MediaPicker2) set to pick single (not multiple) images then you can do this:

    If you want just the image (as IPublishedContent) you should be able to go:

    var image = archetypePanel.GetValue<IPublishedContent>("imageToDisplay");
    
    <img src="'@image.Url" />
    

    If you want the actual UDI then you can go:

    var udi = archetypePanel.GetValue<IEnumerable<Udi>>("image").First();
    
    string udiString = udiToString();
    

    NB. to get HTML content you can just go:

    TextContent = archtypePanel.GetValue<IHtmlString>("textRichTextEditor");
    
  • Umbraco Newbie 22 posts 124 karma points
    Jul 25, 2017 @ 20:39
    Umbraco Newbie
    0

    Hi Dan

    My issue was a corrupt install of some Nuget packages, I removed all and reinstalled and now works OK.

    Thanks for the reply.

Please Sign in or register to post replies

Write your reply to:

Draft