Copied to clipboard

Flag this post as spam?

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


  • umbracov 38 posts 58 karma points
    Mar 25, 2011 @ 20:06
    umbracov
    0

     

    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.

     

  • Daniel Bardi 927 posts 2562 karma points
    Mar 25, 2011 @ 21:31
    Daniel Bardi
    0

    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:

    1. Courier: http://our.umbraco.org/projects/umbraco-pro/courier
    2. uComponents : http://our.umbraco.org/projects/backoffice-extensions/ucomponents
  • umbracov 38 posts 58 karma points
    Mar 26, 2011 @ 00:14
    umbracov
    0

    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

    <xsl:param name="currentPage"/>
    <xsl:param name="medianame" select="/macro/medianame"/>
    <xsl:template match="/">
      <xsl:variable name="mediaId" select="number(./data[@alias=$medianame])"/>
      <xsl:value-of select="$medianame"/>
      <xsl:if test="$mediaId > 0">
       <xsl:variable name="mediaNode" select="umbraco.library:GetMedia($mediaId, 0)"/>
       <xsl:if test="count($mediaNode/*/data) > 0 and string($mediaNode/*/data[@alias='umbracoFile']) != ''">
        <img src="{$mediaNode/*/data[@alias='umbracoFile']}" alt="[image]" height="{$mediaNode/*/data[@alias='umbracoHeight']}" width="{$mediaNode/*/data[@alias='umbracoWidth']}"/>
       </xsl:if>
      </xsl:if>
     </xsl:template>

    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.

  • Daniel Bardi 927 posts 2562 karma points
    Mar 26, 2011 @ 03:00
    Daniel Bardi
    0

    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.

  • umbracov 38 posts 58 karma points
    Mar 28, 2011 @ 18:27
    umbracov
    0

    I would be glad if you could post  any working XSLT for this scenario..........................Thanks in advance.

  • Daniel Bardi 927 posts 2562 karma points
    Mar 28, 2011 @ 19:09
    Daniel Bardi
    0

    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')"/>
        using umbraco;
        using umbraco.cms.businesslogic.media;
        using umbraco.presentation.umbracobase;
    
        [XsltExtension]
        [RestExtension("MediaHelper")]
        public class MediaHelper
        {
            [RestExtensionMethod]
            public int GetMediaIdByName(string mediaName)
            {
                var allMedia = GetAllMedia();
                return allMedia.Where(media => media.Text == mediaName).Select(media => media.Id).FirstOrDefault();
            }
    
            [RestExtensionMethod]
            public int GetMediaIdByName(string mediaName, string mediaType)
            {
                var allMedia = GetAllMedia();
                return allMedia.Where(media => media.Text == mediaName & media.ContentType.Text == mediaType).Select(media => media.Id).FirstOrDefault();
            }
    
            private static IEnumerable<Media> GetAllMedia()
            {
                var allMedia = new List<Media>();
                foreach (var rootMedia in Media.GetRootMedias())
                {
                    allMedia.AddRange(GetChildMedia(rootMedia));
                }
                return allMedia;
            }
    
            private static IEnumerable<Media> GetChildMedia(Media media)
            {
                var allMedia = new List<Media> {media};
                if (media.HasChildren)
                {
                    foreach (var childMedia in allMedia)
                    {
                        allMedia.AddRange(GetChildMedia(childMedia));
                    }
                }
                return allMedia;
            }
        }

     

  • Pasang Tamang 258 posts 458 karma points
    Mar 28, 2011 @ 19:19
    Pasang Tamang
    0

    Hi Daniel,

    Thanks for this post. so much helpful and got new idea about umbraco api

    Pnima

  • Daniel Bardi 927 posts 2562 karma points
    Mar 28, 2011 @ 19:26
    Daniel Bardi
    0

    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!

  • umbracov 38 posts 58 karma points
    Mar 28, 2011 @ 21:18
    umbracov
    0

    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.

  • Daniel Bardi 927 posts 2562 karma points
    Mar 28, 2011 @ 21:25
    Daniel Bardi
    0

    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

    using System.Collections.Generic;
    using System.Linq

  • umbracov 38 posts 58 karma points
    Mar 29, 2011 @ 00:02
    umbracov
    0

    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

  • Pasang Tamang 258 posts 458 karma points
    Mar 29, 2011 @ 07:16
    Pasang Tamang
    0

    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

  • Daniel Bardi 927 posts 2562 karma points
    Mar 29, 2011 @ 07:24
    Daniel Bardi
    0

    The methods is GetMediaIdByName

  • Pasang Tamang 258 posts 458 karma points
    Mar 29, 2011 @ 07:45
    Pasang Tamang
    0

    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"

  • Daniel Bardi 927 posts 2562 karma points
    Mar 29, 2011 @ 07:56
    Daniel Bardi
    0

    I found an infinite loop bug.. I had a minor typo.

    Change this line:

    foreach (var childMedia in allMedia)

    to this:

    foreach (var childMedia in media.Children)
  • Pasang Tamang 258 posts 458 karma points
    Mar 29, 2011 @ 08:04
    Pasang Tamang
    0

    I works now. Thanks for your kind help.

  • Daniel Bardi 927 posts 2562 karma points
    Mar 29, 2011 @ 08:04
    Daniel Bardi
    1

    Final working code is below:

    Note:  add the following to the tag ...

    xmlns:MediaHelper="urn:MediaHelper"

    ... 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;
        }
    }
    
  • Daniel Bardi 927 posts 2562 karma points
    Mar 29, 2011 @ 08:12
    Daniel Bardi
    0

    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.

  • Pasang Tamang 258 posts 458 karma points
    Mar 29, 2011 @ 08:14
    Pasang Tamang
    0

    Yes I marked it already.

    Once again thanks for you help

  • Daniel Bardi 927 posts 2562 karma points
    Mar 29, 2011 @ 08:16
    Daniel Bardi
    0

    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.

  • umbracov 38 posts 58 karma points
    Mar 29, 2011 @ 20:25
    umbracov
    0

    Daniel.........that code worked like a charm......Kudos to you!!!!!!

  • Daniel Bardi 927 posts 2562 karma points
    Mar 29, 2011 @ 20:31
    Daniel Bardi
    0

    Very glad I could help.

Please Sign in or register to post replies

Write your reply to:

Draft