Copied to clipboard

Flag this post as spam?

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


  • islam 20 posts 40 karma points
    Oct 30, 2012 @ 12:06
    islam
    0

    Fetching a media item properties

    Hi, I am working on MVC project which must fetch the content of an article in umbraco

    In MVC controller, I managed to fetch from umbraco Content section a node's properties  but now  I am trying to fetch  a media item URL from the Media section 

    Any help will be appreciated,

    Thanks in advance.

    using System.Collections.Generic;

    using System.Linq;

    using System.Text;

    using System.Web;

    using System.Web.Mvc;

    using System.Web.Routing;

    using System.Web.Mvc.Html;

    using umbraco.NodeFactory;

    using umbraco.cms.businesslogic.media;

    using umbraco.BusinessLogic;

    using umbraco.MacroEngines;

    using umbraco.DataLayer;

     

    var media = new umbraco.cms.businesslogic.media.Media(featuredImageId);

    var imageUrl = media.getProperty("umbracoFile").Value.ToString();

  • Fabio Milheiro 74 posts 136 karma points
    Oct 30, 2012 @ 12:51
    Fabio Milheiro
    0

    Whatever you are looking for, I think you can debug your app, place a breakpoint in your code and mouseover on your object. Then, you'll see the information you're looking for and how to get it will be easy.

  • Jeroen Breuer 4908 posts 12265 karma points MVP 4x admin c-trib
    Oct 30, 2012 @ 18:23
    Jeroen Breuer
    1

    It's not good to use the Media api because it's slow. It's better to use umbraco.libabry.GetMedia or the DynamicMedia object.

    You can see some examples here: http://damp.codeplex.com/SourceControl/changeset/view/e6dfc23396f3#DAMP.RazorModel%2fDAMP_Item.cs

    Jeroen

  • islam 20 posts 40 karma points
    Nov 01, 2012 @ 09:19
    islam
    0

    Thanks Jeroen, the examples helped me a lot

    var media = umbraco.library.GetMedia(featuredImageId, false);

    XmlNode  _xmlNode = ((IHasXmlNode)media.Current).GetNode().FirstChild;

    var url= _xmlNode.FirstChild.InnerText;

     

    Cheers,

    Islam

  • Brooke 13 posts 34 karma points
    Nov 08, 2012 @ 16:11
    Brooke
    0

    Hi,
    I have found that the code above by Islam works, however I am puzzled as to why the following would not work in my template.

    var img = Model.MediaById( Umbraco.Field("imageAlias") );
    <img src="@img.Url" />

    When I use the above code, the following error is returned:

    CS1061: 'Umbraco.Web.Models.RenderModel' does not contain a definition for 'MediaById' and no extension method 'MediaById' accepting a first argument of type 'Umbraco.Web.Models.RenderModel' could be found (are you missing a using directive or an assembly reference?)

    However, in the same application, when displayed a uBlogsy post, the above code does work in a cshtml macro.

    Note, I am using Umbraco 4.10beta.  

    Thanks.

    - Brooke

  • Jeavon Leopold 3072 posts 13628 karma points MVP 10x admin c-trib
    Nov 08, 2012 @ 18:07
    Jeavon Leopold
    3

    Hi, this is all very new but I think you need to use the new Razor MVC Helper again. I've just tried this and it seems to work @Umbraco.TypedMedia(CurrentPage.mediaPicker).GetProperty("umbracoFile")

    Have a look at https://github.com/umbraco/Umbraco4Docs/blob/4.8.0/Documentation/Reference/Mvc/querying.md for some more information.

    The Dynamics used in MVC is not exactly the same as the DynamicNode used in the Razor Macros so a lot of samples from Macros will need tweaking to use in MVC.

  • Brooke 13 posts 34 karma points
    Nov 08, 2012 @ 21:45
    Brooke
    0

    Hi Jeavon,

    Thank you for that - yes that works perfectly.

    However, perhaps another newbie question, though how did you know to retrieve the property "umbracoFile", and not "url", which would have been my first instinct.  

    I have checked the github documentation though it doesn't seem to mention the 'umbracoFile' property.

    Thanks

    - Brooke

  • Owen 123 posts 246 karma points
    Nov 09, 2012 @ 00:13
    Owen
    0

    umbracoFile is the property of Media type, you can find other properties at Setting section, under the Media folder. Something like document type.

  • Brooke 13 posts 34 karma points
    Nov 09, 2012 @ 00:55
    Brooke
    0

    Hi Owen,

    Found it - thanks for your help.  Even though I had the solution, I just really needed to know how the answer was reached incase I need other properties in the future.

    Thanks once again to all who replied.

    - Brooke

  • Jeavon Leopold 3072 posts 13628 karma points MVP 10x admin c-trib
    Nov 09, 2012 @ 12:22
    Jeavon Leopold
    1

    Hi Brooke,

    That's a good question, I guess the answer is only experience, however it is worth noting that whilst normally the upload property on a media type is called umbracoFile (and that is best practice name) it doesn't have to be. You can define and change the property name on the media type if you want to, however the additional properties such as width and height are auto populated if you do call it umbracoFile.

    In the sample I posted yesterday I mixed Typed and Dynamic which you can do, however you want to just use one or the other:

    Typed:

    @{
        if(Model.Content.HasValue("mainImage")){
          var mediaItem = Umbraco.TypedMedia(Model.Content.GetPropertyValue("mainImage")); 
          <img src="@mediaItem.GetPropertyValue("umbracoFile")" alt="@mediaItem.GetPropertyValue("Name")"/>    
        }   
    }

    Dynamic:

    @{
        if (CurrentPage.HasValue("mainImage")){
          var dynamicMediaItem = Umbraco.Media(CurrentPage.mainImage);
          <img src="@dynamicMediaItem.umbracoFile" alt="@dynamicMediaItem.Name"/>
        }
    }

    Thanks,

    Jeavon

  • Tom 713 posts 954 karma points
    Dec 03, 2012 @ 04:10
    Tom
    0

    How would i get access to Umbraco.TypedMedia in a partial view?

    It doesn't seem to be available.. 

    Cheers,

    Tom

  • Tom 713 posts 954 karma points
    Jul 18, 2013 @ 03:54
    Tom
    0

    Hi All,

    how would you just use this in a standard view? 6.0.3 doesn't have @Umbraco

  • Jeroen Breuer 4908 posts 12265 karma points MVP 4x admin c-trib
    Jul 18, 2013 @ 06:37
    Jeroen Breuer
    0

    What does your view inherit from? If it has @inherits Umbraco.Web.Mvc.UmbracoTemplatePage or @inherits Umbraco.Web.Mvc.UmbracoViewPage<Model> it should have the @Umbraco helper.

    Jeroen

  • ZIA 2 posts 22 karma points
    Jul 25, 2013 @ 11:21
    ZIA
    0

    Use the following one line:

     <img src="@Model.Media("YOURIMAGEALIAS","umbracoFile")" />

    Tips:

    1-Change Model to the source Page,  if source image is not on current page.

    2-Use the alias of image instead of "YOURIMAGEALIAS".

    3-Use the Media (image in this case) property if you want other then umbracoFile.

    4-You are done.

Please Sign in or register to post replies

Write your reply to:

Draft