I have lot of images that is part of my webpage. I have curently placed all the images under /images folder. The issue is when I go to subpages the images are not displayed, broken links.
I placed each images as a media file and referencing these images as a media file like in
works perfectly fine. But my question with media file is that I have lots of images that need to be referenced. I am currently working in development envirenment.Tommorow when I move this to staging or to Test and production how can these media files be moved. I tried deploying them and the id of each of these files changed and I had to change the id for each of these images. like 1071==>something different.
I am I doing it wrong....... is there any other approach to this. Do I have to move the database records to keep the same id. Kindly help.
You can use Umbraco Courier package (commercial) to sync development with staging or store the images in the /images folder and use the FilePicker in the uComponents package (free).
Though I do not have Courier package my choice for using images is currently Media. I am using the following code to get the file to display.
I prefer to user image name istead of the id field to display the images as these are more versatile and easy to understand on the go. the code I use is as follows
The problem is that the media name is not unique, unless you're controlling that... You could write an xslt extension to get media by name and return the first if there are more than one.
Daniel.....Sorry for asking so many questions.......but I am relly trying here and I am going no where. This is my first time with XSLT extentions and I am glad there is such a thing in Umbraco and some day I will develop my own.but for now...........I am stuck.....in the code you have given....how about the
IEnumerable
<Media>
....is Media already defined in Umbraco dll or is it a class that I will need to wirte.......I am going blank here and I would appreciate if you would help me through this.
Thanks for your quick reply. I got the MediaHelper project working and so does the XSLT file. But when reference is added to the xslt my application pool in IIs stops and I get http:503 server unavailable error.
I am testing the MediaHelper with a test application and I get the following error. Please let me how to proceed. Thanks in advance.
Object reference not set to an instance of an object. @ Media.GetRootMedias()
Thanks for your quick reply. I've made correction as you told but now I can't save xslt file while referencing the assembly. And when I debug the code It shows "An unhandled exception of type 'System.StackOverflowException' occurred in mscorlib.dll error"
... and add MediaHelper to the exclude-result-prefixes attribute
using System.Collections.Generic;
using System.Linq;
using umbraco;
using umbraco.cms.businesslogic.media;
[XsltExtension]
public class MediaHelper
{
/// <summary>
/// Gets the id of the first Media with the specified name.
/// </summary>
/// <param name="mediaName">Name of the media.</param>
/// <returns></returns>
public int GetMediaIdByName(string mediaName)
{
var allMedia = GetAllMedia();
return allMedia.Where(media => media.Text == mediaName).Select(media => media.Id).FirstOrDefault();
}
/// <summary>
/// Gets the id of the first Media item with the specified name of the specified mediaType.
/// </summary>
/// <param name="mediaName">Name of the media.</param>
/// <param name="mediaType">Type of the media.</param>
/// <returns></returns>
public int GetMediaIdByName(string mediaName, string mediaType)
{
var allMedia = GetAllMedia();
return allMedia.Where(media => media.Text == mediaName & media.ContentType.Alias == mediaType).Select(media => media.Id).FirstOrDefault();
}
/// <summary>
/// Gets all media.
/// </summary>
/// <returns></returns>
private static IEnumerable<Media> GetAllMedia()
{
var allMedia = new List<Media>();
var rootMedias = Media.GetRootMedias();
foreach (var rootMedia in rootMedias)
{
allMedia.AddRange(GetChildMedia(rootMedia));
}
return allMedia;
}
/// <summary>
/// Gets the child media.
/// </summary>
/// <param name="media">The media.</param>
/// <returns></returns>
private static IEnumerable<Media> GetChildMedia(Media media)
{
var allMedia = new List<Media> { media };
if (media.HasChildren)
{
foreach (var childMedia in media.Children)
{
allMedia.AddRange(GetChildMedia(childMedia));
}
}
return allMedia;
}
}
Media
I have lot of images that is part of my webpage. I have curently placed all the images under /images folder. The issue is when I go to subpages the images are not displayed, broken links.
I placed each images as a media file and referencing these images as a media file like in
umbraco.library:GetMedia(1071, true())/umbracoFile
works perfectly fine. But my question with media file is that I have lots of images that need to be referenced. I am currently working in development envirenment.Tommorow when I move this to staging or to Test and production how can these media files be moved. I tried deploying them and the id of each of these files changed and I had to change the id for each of these images. like 1071==>something different.
I am I doing it wrong....... is there any other approach to this. Do I have to move the database records to keep the same id. Kindly help.
You can use Umbraco Courier package (commercial) to sync development with staging or store the images in the /images folder and use the FilePicker in the uComponents package (free).
Links:
Though I do not have Courier package my choice for using images is currently Media. I am using the following code to get the file to display.
I prefer to user image name istead of the id field to display the images as these are more versatile and easy to understand on the go. the code I use is as follows
medianame - diplays the file name being passed
mediaId - NaN
I am looking at many available links in forums but with no luck..Any help appreciated.
The problem is that the media name is not unique, unless you're controlling that... You could write an xslt extension to get media by name and return the first if there are more than one.
I would be glad if you could post any working XSLT for this scenario..........................Thanks in advance.
Here's some code I wiped up that can get in the right direction.
Add this to a .cs file and drop it in the App_Code folder.
You should be able to use it from xslt like this:
<xsl:variable name="mediaId" select="MediaHelper:GetMediaByName('my media name')"/>
Hi Daniel,
Thanks for this post. so much helpful and got new idea about umbraco api
Pnima
No problem.. I haven't tested it yet.. let me know if it works.
Please mark my post as the solution if it works. Thanks!
Daniel.....Sorry for asking so many questions.......but I am relly trying here and I am going no where. This is my first time with XSLT extentions and I am glad there is such a thing in Umbraco and some day I will develop my own.but for now...........I am stuck.....in the code you have given....how about the
IEnumerable
<Media>
....is Media already defined in Umbraco dll or is it a class that I will need to wirte.......I am going blank here and I would appreciate if you would help me through this.
I modified my previous post.. you need to include the using statements in order to reference the umbraco assemblies
Also needed are these using statements; add to the top of the .cs file to support the IEnumerable and Linq expressions
Thanks for your quick reply. I got the MediaHelper project working and so does the XSLT file. But when reference is added to the xslt my application pool in IIs stops and I get http:503 server unavailable error.
I am testing the MediaHelper with a test application and I get the following error. Please let me how to proceed. Thanks in advance.
Object reference not set to an instance of an object. @ Media.GetRootMedias()
Thanks
Hi Daniel,
I just try your above code and it's giving me error. Could you please give me some guidance? Here is my error screen
Thanks
The methods is GetMediaIdByName
Thanks for your quick reply. I've made correction as you told but now I can't save xslt file while referencing the assembly. And when I debug the code It shows "An unhandled exception of type 'System.StackOverflowException' occurred in mscorlib.dll error"
I found an infinite loop bug.. I had a minor typo.
Change this line:
to this:
I works now. Thanks for your kind help.
Final working code is below:
Note: add the following to the tag ...
... and add MediaHelper to the exclude-result-prefixes attribute
You are very welcome.. It was a fun break.. thanks for the work... :)
Don't forget to mark my last post as the solution to help other.
Yes I marked it already.
Once again thanks for you help
Thanks for the High Five.. that's awesome! :)
You must click the green check to mark as solution... Your welcome again, it was a pleasure.
Daniel.........that code worked like a charm......Kudos to you!!!!!!
Very glad I could help.
is working on a reply...