Copied to clipboard

Flag this post as spam?

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


  • Jim 5 posts 83 karma points
    Aug 19, 2014 @ 17:53
    Jim
    0

    Get Media ID

    Is there any better way to do this?  I want to display a media item directly in the browser.  Parsing the property seems kind of convoluted.

    umbraco.cms.businesslogic.media.Media displayMedia = new umbraco.cms.businesslogic.media.Media(Int32.Parse(Node.GetCurrent().GetProperty("mediaFile").Value));
    
    Response.Redirect(displayMedia.GetProperty<string>("umbracoFile"));
    

    Thanks,

    -Jim

  • Charles Afford 1163 posts 1709 karma points
    Aug 25, 2014 @ 11:41
    Charles Afford
    0

    Hi what version of Umbraco are you using? Charlie

  • Jeroen Breuer 4908 posts 12265 karma points MVP 4x admin c-trib
    Aug 25, 2014 @ 12:01
    Jeroen Breuer
    0

    Hello,

    If you are using Umbraco 6 or higher you can use Umbraco.TypedMedia(id) for this: http://our.umbraco.org/documentation/Reference/Mvc/querying

    Jeroen

  • Jim 5 posts 83 karma points
    Aug 26, 2014 @ 16:23
    Jim
    0

    @Charles Afford,  I'm using 4.11.10.

     

    Thanks Jeroen Breuer ... any suggestions for 4.x?

     

    -Jim

  • Jeroen Breuer 4908 posts 12265 karma points MVP 4x admin c-trib
    Aug 26, 2014 @ 16:25
    Jeroen Breuer
    0

    Uhm I think the feature was actually added in Umbraco 4.10 already so you can probably use it.

    Jeroen

  • Charles Afford 1163 posts 1709 karma points
    Aug 27, 2014 @ 11:46
    Charles Afford
    0

    Yea i think TypedMedia was added as well.

    So you can do (i think)

    @Umbraco.TypedMedia(Model.Content.GetPropertyValue("your alias")).Url

  • Jim 5 posts 83 karma points
    Aug 27, 2014 @ 22:51
    Jim
    0

    I'm using Web forms so it looks like I would expose the UmbracoHelper and then use TypedMedia.  Does this sound right?

    UmbracoHelper umbracoHelper = new UmbracoHelper(Umbraco.Web.UmbracoContext.Current); Response.Redirect(umbracoHelper.TypedMedia(Node.GetCurrent().GetProperty("mediaFile")).UrlName);

     

  • Jeroen Breuer 4908 posts 12265 karma points MVP 4x admin c-trib
    Aug 28, 2014 @ 09:37
    Jeroen Breuer
    0

    Hello,

    The code is correct, but Node.GetCurrent is legacy. Beside Umbraco.TypedMedia you also have Umbraco.TypedContent. With that you also get the property. Something like this:

    UmbracoHelper umbracoHelper = new UmbracoHelper(Umbraco.Web.UmbracoContext.Current); 
    Response.Redirect(umbracoHelper.TypedMedia(umbracoHelper.TypedContent(id).GetPropertyValue<int>("mediaFile")).UrlName);

    You can also get the current node through the Umbraco helper like this:

    UmbracoHelper umbracoHelper = new UmbracoHelper(Umbraco.Web.UmbracoContext.Current); 
    Response.Redirect(umbracoHelper.TypedMedia(umbracoHelper.AssignedContentItem.GetPropertyValue<int>("mediaFile")).UrlName);

    Jeroen

  • Jim 5 posts 83 karma points
    Aug 28, 2014 @ 23:18
    Jim
    0

    Jeron,

     

    umbracoHelper.AssignedContentItem.GetPropertyValue<int>("mediaFile")

    wouldn't compile because IPublishedContent doesn't have a GetPropertyValue member so I changed it to:

    Response.Write(umbracoHelper.TypedMedia(umbracoHelper.AssignedContentItem.GetProperty("mediaFile").Value).UrlName)

    but that only gives me the name of the media...I need the full URL.  How would you recommend I obtain that?

     

    Thanks,

    -Jim

  • Jeroen Breuer 4908 posts 12265 karma points MVP 4x admin c-trib
    Aug 29, 2014 @ 15:01
    Jeroen Breuer
    0

    Hello,

    Sorry I forgot to mention that .GetPropertyValue<> is an extension method. So you need to add the Umbraco.Core namespace before you can use it.

    If you want to full url try something like this:

    var uri = new Uri(HttpContext.Current.Request.Url.AbsoluteUri);
    var domainUrl = string.Format("{0}://{1}", uri.Scheme, uri.Authority);
    Response.Write(domainUrl + umbracoHelper.TypedMedia(umbracoHelper.AssignedContentItem.GetPropertyValue<int>("mediaFile").Value).UrlName)

    Jeroen

  • Jim 5 posts 83 karma points
    Aug 29, 2014 @ 16:34
    Jim
    100

    That won't give me the full path.  The following seems to work however.

           UmbracoHelper umbracoHelper = new UmbracoHelper(Umbraco.Web.UmbracoContext.Current);
    
            int iMediaFileId = umbracoHelper.AssignedContentItem.GetPropertyValue<int>("mediaFile");
            string sMediaURL = umbracoHelper.TypedMedia(iMediaFileId).GetPropertyValue<string>("umbracoFile");
    
            Response.Redirect(sMediaURL);
    
    -Jim
  • Charles Afford 1163 posts 1709 karma points
    Aug 30, 2014 @ 12:26
    Charles Afford
    0

    Yep that will do it Jim. You need to get the value of "umbracoFile"

Please Sign in or register to post replies

Write your reply to:

Draft