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();
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.
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.
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")
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.
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.
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.
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:
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.
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();
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.
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
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
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
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.
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
umbracoFile is the property of Media type, you can find other properties at Setting section, under the Media folder. Something like document type.
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
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
How would i get access to Umbraco.TypedMedia in a partial view?
It doesn't seem to be available..
Cheers,
Tom
Hi All,
how would you just use this in a standard view? 6.0.3 doesn't have @Umbraco
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
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.
is working on a reply...