I know personally and our developers here are finding it increasingly difficult to keep up with best practice razor especially when some people are advocating the use of Library.MediaById and there are still items like .Media and DynamicMedia.
We had a simple example of trying to get the umbracoFile property for a media item in 4.7.1 which raised discussion internally. What's the deal with having 2 implementations and why is something like Library.MediaById considered best practice.. and if this is the case then why are we getting objects like a DynamicBackingItem where to do things like below we have to resort to get property:
var albumCoverImage = !string.IsNullOrEmpty(album.albumCover.ToString()) ?
Library.MediaById(album.albumCover).GetProperty("umbracoFile") :
Library.MediaById(album.albumImagesFolder).ChildrenAsList[0].GetProperty("umbracoFile");
Any advice or a clear indication of how best to deal with Media would be most appreciated. Of note in the same example is ChildrenAsList[0] I couldn't use ChildrenAsList.First() even when declaring @using System.Linq
Thanks for any clarification on the issue as these changes between versions are not well documented enough for us to know what is best to use where going forward
You could also use DAMP for using your media. It can store an id or the complete media xml which can make it easier to use in Razor. In the Cultiv Razor Examples package you can find some more examples.
thanks for the reply. But I'm really interested in what is the best practice syntax to use without DAMP just accessing media in umbraco 4.7.1
and even on the cheatsheet for 4.7.1 why can we access media in multiple ways and which is the best to use and why.. the same goes for alot of the razor calls.
I'm not sure why you percieve there to be a difference, they are exactly the same actually.
Unfortunately, Model.MediaById was there first and couldn't be removed when the @Library was introduced. The @Library is there to make things like this more consistent ("Model" really is something that relates to your current contentnode, why should that know how to get media?). So that's why Library.MediaById is preferred.
I get the exact same result using umbracoFile on both, .Url might just be a bit of syntactic sugar:
@{var x = Model.MediaById(1284);
var y = Library.MediaById(1284);
}@x.umbracoFile // result: /media/6313/tulips.jpg
@y.umbracoFile // result: /media/6313/tulips.jpg
What Seb says is correct. Library.MediaById is defined as:
public object MediaById(int Id)
{
return (object) new DynamicMedia(new DynamicBackingItem(ExamineBackedMedia.GetUmbracoMedia(Id)));
}
And DynamicNode.MediaById just calls this function and is only there to retain backward compatability (for reasons Seb outlines). The two are functionally identical.
I know this is an old thread but I'm always stuck when it comes to images in Umbraco. I use DAMP (storing id's not xml) and Razor (the old version apparently as I'm not using MVC yet). Every time I come to build a site the image requirements are never the same as the last one and never the same as any of the often quoted example sites. So, as there's NO DOCUMENTATION I can find that would lead me to work stuff out simply and quickly myself, I end up spending many many hours of wasted time shooting in the dark. Building an Umbraco site is fast becoming uneconomic. I REALLY hope this can be addressed at the upcoming CodeGarden. I really like Umbraco generally but image handling is killing it for me.
Something that needs clearing up apart from documentation is the ability to bulk upload files as custom media types. Jeroen's great video from last CodeGarden is something I'm trying to do with my own sites but the designers hate having to upload single files using "create" when they can see a bulk uploader available that they can't use because it only uploads as "Image" media types.
Also a way to get hold of images when they're stored as ID's in the DAMP Picker. I can NEVER work out the way to do this and can't find a workable solution or example that clearly explains which bit of code is what, what's standard in Umbraco or DAMP and what has been invented by the example writer and won't work in your instance if you use it. Documentation would help this enormously.
This is a longer post than anticipated but I'm two hours down trying to output a simple banner image from a custom DAMP picker and getting absolutely nowhere!
Thanks for listening.
Craig
P.S. Niels did say to vent frustrations, if you don't squeak, you don't get oiled, lol
I can appreciate your frustation, as the thing known as "Razor" in Umbraco has gone over a lot of permutations (to improve it) since it crept out around 4.6 and often you can never tell if a code snippet will work with the version you are using. What makes this worse, as you alude to, is the lack of a permanent home for any documentation in the first place. I guess the good news is that "HQ" have listened to these frustrations and, going forward, http://our.umbraco.org/documentation is going to be the single resource for all needs, though this is still a work in progress.
Anyway, back to your question. I can't quite remember what features are in 4.7 but normally you would do this (assuming your image Id is in a property called "image" that is part of your current Model):
But these method has been deprecated in favour of Library.MediaById() and using @media.Url rather than @media.umbracoFile - but I can't remember which version it was added in. So if the first doesn't work, try the second. (Which confirms your point about it being confusing!).
You may also want to put in some logic to check whether there is an Id and that Library.MediaById returns a valid media item.
4.7.1 Dealing With Media Best Practice
Hi,
I know personally and our developers here are finding it increasingly difficult to keep up with best practice razor especially when some people are advocating the use of Library.MediaById and there are still items like .Media and DynamicMedia.
We had a simple example of trying to get the umbracoFile property for a media item in 4.7.1 which raised discussion internally. What's the deal with having 2 implementations and why is something like Library.MediaById considered best practice.. and if this is the case then why are we getting objects like a DynamicBackingItem where to do things like below we have to resort to get property:
Any advice or a clear indication of how best to deal with Media would be most appreciated. Of note in the same example is ChildrenAsList[0] I couldn't use ChildrenAsList.First() even when declaring @using System.Linq
Thanks for any clarification on the issue as these changes between versions are not well documented enough for us to know what is best to use where going forward
The confusion is things like Model.MediaById ends up using umbracoFile, Library.MediaById uses Url
My example above should have used Url but yes could I please get some clarification as to best practice and why there are multiple implementations?
You could also use DAMP for using your media. It can store an id or the complete media xml which can make it easier to use in Razor. In the Cultiv Razor Examples package you can find some more examples.
Jeroen
Hi Jeroen,
thanks for the reply. But I'm really interested in what is the best practice syntax to use without DAMP just accessing media in umbraco 4.7.1
and even on the cheatsheet for 4.7.1 why can we access media in multiple ways and which is the best to use and why.. the same goes for alot of the razor calls.
I'm not sure why you percieve there to be a difference, they are exactly the same actually.
Unfortunately, Model.MediaById was there first and couldn't be removed when the @Library was introduced. The @Library is there to make things like this more consistent ("Model" really is something that relates to your current contentnode, why should that know how to get media?). So that's why Library.MediaById is preferred.
I get the exact same result using umbracoFile on both, .Url might just be a bit of syntactic sugar:
What Seb says is correct. Library.MediaById is defined as:
And DynamicNode.MediaById just calls this function and is only there to retain backward compatability (for reasons Seb outlines). The two are functionally identical.
Thanks for clarifying Sebastiaan and Dan! Most appreciated!
I know this is an old thread but I'm always stuck when it comes to images in Umbraco. I use DAMP (storing id's not xml) and Razor (the old version apparently as I'm not using MVC yet). Every time I come to build a site the image requirements are never the same as the last one and never the same as any of the often quoted example sites. So, as there's NO DOCUMENTATION I can find that would lead me to work stuff out simply and quickly myself, I end up spending many many hours of wasted time shooting in the dark. Building an Umbraco site is fast becoming uneconomic. I REALLY hope this can be addressed at the upcoming CodeGarden. I really like Umbraco generally but image handling is killing it for me.
Something that needs clearing up apart from documentation is the ability to bulk upload files as custom media types. Jeroen's great video from last CodeGarden is something I'm trying to do with my own sites but the designers hate having to upload single files using "create" when they can see a bulk uploader available that they can't use because it only uploads as "Image" media types.
Also a way to get hold of images when they're stored as ID's in the DAMP Picker. I can NEVER work out the way to do this and can't find a workable solution or example that clearly explains which bit of code is what, what's standard in Umbraco or DAMP and what has been invented by the example writer and won't work in your instance if you use it. Documentation would help this enormously.
This is a longer post than anticipated but I'm two hours down trying to output a simple banner image from a custom DAMP picker and getting absolutely nowhere!
Thanks for listening.
Craig
P.S. Niels did say to vent frustrations, if you don't squeak, you don't get oiled, lol
Hi Craig,
I can appreciate your frustation, as the thing known as "Razor" in Umbraco has gone over a lot of permutations (to improve it) since it crept out around 4.6 and often you can never tell if a code snippet will work with the version you are using. What makes this worse, as you alude to, is the lack of a permanent home for any documentation in the first place. I guess the good news is that "HQ" have listened to these frustrations and, going forward, http://our.umbraco.org/documentation is going to be the single resource for all needs, though this is still a work in progress.
Anyway, back to your question. I can't quite remember what features are in 4.7 but normally you would do this (assuming your image Id is in a property called "image" that is part of your current Model):
You can also do:
But these method has been deprecated in favour of Library.MediaById() and using @media.Url rather than @media.umbracoFile - but I can't remember which version it was added in. So if the first doesn't work, try the second. (Which confirms your point about it being confusing!).
You may also want to put in some logic to check whether there is an Id and that Library.MediaById returns a valid media item.
I am not getting Library to work in Umbraco 4.7. It says The name 'Library' does not exist in the current context
but if i Include @using umbraco.MacroEngines.Library;
at the top of my Razor script then I get
The type or namespace name 'Library' does not exist in the namespace 'umbraco.MacroEngines' (are you missing an assembly reference?)
is working on a reply...