Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Link to Sitecore Media Item

Tags:

sitecore

In one of my templates, I created a field named UploadedSitecoreFile and set the type to File. I did this so that when a user creates a new item, they can browse the Media Library and select the PDF that they want to link to.

In my code-behind, I have the following:

Dim subItem = TryCast(e.Item.DataItem, Item)
ltResourceInfoTag.Text = ltResourceInfoTag.Text & "<a href='" & subItem("UploadedSitecoreFile").ToString & "'>" & subItem("Name").ToString & "</a>"

Can someone tell me the correct way to link to a media item?

Thanks, C.R. Junk

like image 954
crjunk Avatar asked Nov 29 '22 17:11

crjunk


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.

Example (C# not VB sorry):

Item subItem = Sitecore.Context.Item;
Sitecore.Data.Fields.FileField fileField = ((Sitecore.Data.Fields.FileField)subItem.Fields["UploadedSitecoreFile"]);

string url = Sitecore.Resources.Media.MediaManager.GetMediaUrl(fileField.MediaItem);
like image 58
Sean Kearney Avatar answered Jan 19 '23 19:01

Sean Kearney