Surendra Sharma

Surendra Sharma

Search This Blog

Wednesday, June 5, 2013

Fetches Associated Sitecore Image from Site core Item

Fetches Associated Sitecore Image from Site core Item


/// <summary>
        /// Fetches Associated Sitecore Image from Site core Item
        /// </summary>
        /// <param name="item">Site core Item containg Image Field</param>
        /// <param name="fieldName">Name of the Image Field</param>
        /// <returns>Associated Sitecore Image from Site core Item</returns>
        public static string GetItemImageURL(Sitecore.Data.Items.Item item, string fieldName) {
            string imageTag = string.Empty;
            Sitecore.Data.Fields.ImageField imageField = item.Fields[fieldName];

            if (imageField != null && imageField.MediaItem != null) {
                MediaItem image = new MediaItem(imageField.MediaItem);
                string src = Sitecore.StringUtil.EnsurePrefix('/', Sitecore.Resources.Media.MediaManager.GetMediaUrl(image));

                if (!string.IsNullOrEmpty(src)) {
                    imageTag = src;                
                   }
            }

            return imageTag;

        }

No comments:

Post a Comment