Copied to clipboard

Flag this post as spam?

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


  • Scott Severance 13 posts 123 karma points
    Jun 28, 2018 @ 15:58
    Scott Severance
    0

    Retreiving media ID fail in Umbraco 7.6+

    Hello,

    I am having some difficulty retrieving a media ID now that I have upgraded to Umbraco 7.6. I see that in version 8 we are losing a lot of dynamic stuff so I am rebuilding templates to be strongly typed. Is this related to the new GUID being generated for media objects in 7.6+?

    In the below code block I can retreive my "bannerimage" property URL In this case a "bannerimage" is an image selected via media picker.

    Using the same format and countless others I have tried I can NOT retreive my "attachment" property ID. In this case a .pdf that was selected from a media picker.

    I am building a partial view to represent a news post. I have been able to pull all of my other properties from my doc type to include text blocks, formatted dates and images.

    Is there some other format needed to pull media that is not specifically an image? Does the file type matter? I understand it should not. It's all just media in the media library, right?

    Also, future state I will need video as well. So how might that differ from these media types in the strongly typed model?

    Edit: I should include the error. "System.NullReferenceException: Object reference not set to an instance of an object. "

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
    @{string imageUrl = Model.Content.GetPropertyValue<IPublishedContent>("bannerimage").Url;
    var mediaId = Model.Content.GetPropertyValue<IPublishedContent>("attachment").Id;}
    

    Thank you for your time.

  • Scott Severance 13 posts 123 karma points
    Jun 28, 2018 @ 17:28
    Scott Severance
    101

    False alarm? I solved my own problem but I spent three days cruising forums on and off before I did so I'm going to post this again to add one more example of how things work in the strongly typed model.

    Solution: I was actually using a multi media picker as opposed to a single media picker and making the call wrongly for that reason.

    This link contains the best examples and documentation I have seen thus far regarding working on all things of the strongly typed model: https://github.com/Jeavon/Umbraco-Core-Property-Value-Converters/blob/v3/Docs/MultipleMediaPicker.md

    Code Solution for multi media picker "enabled box- checked":

    var typedMultiMediaPicker = Model.Content.GetPropertyValue<IEnumerable<IPublishedContent>>("attachment");
            @{
                        foreach (var item in typedMultiMediaPicker)
                        {
                            <a href="@item.Url">@item.Name</a><br/>
                            }
                            }
    
Please Sign in or register to post replies

Write your reply to:

Draft