Search code examples
asp.netsitecoresitecore-media-library

Sitecore provide links to allow users to view/download media items


How do I provide links in a page to documents within the Sitecore Media Library?

In my template(s) I have a Treelist which allows users to select a number of documents for a given page object. In my sublayout code-behind I iterate over the items in the Treelist field to get their IDs, then use Sitecore.Links.LinkManager.GetItemUrl() to get the URL to the items. At the moment I simply render the links using a hyperlink, but on clicking the links I get the error saying that no layout was found.

Do I need to create a layout which is an ashx handler to handle downloading the content stream of the media item, or is there something built-in which can do this?


Solution

  • To generate links to items within the Media Library you should be using MediaManager. e.g:

    string url = Sitecore.Resources.Media.MediaManager.GetMediaUrl(item)