Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get image src and alt attribute in Sitecore 6?

Tags:

sitecore

I use the following to get, for instance, the header of the current page;

Header = Sitecore.Context.Item["Header"]

But how would i go about getting the src url of a image field?

PictureSrc = Sitecore.Context.Item["MyImage"]
like image 491
brother Avatar asked Jun 26 '12 18:06

brother


1 Answers

You will want to look at leveraging the Sitecore.Resources.Media.MediaManager to get the URL to a media library item.

Before you get there, get the field from the item and cast it to a FileField. Once you have a FileField you can get access to the MediaItem.

Item item = Sitecore.Context.Item;
Sitecore.Data.Fields.ImageField imgField = ((Sitecore.Data.Fields.ImageField)item.Fields["MyImage"]);

string url = Sitecore.Resources.Media.MediaManager.GetMediaUrl(imgField.MediaItem);
string altText = imgField.Alt;

Link to Sitecore Media Item

like image 139
Sean Kearney Avatar answered Oct 04 '22 04:10

Sean Kearney