views:

33

answers:

1

How do I open a pdf document stored in Sitecore when a user clicks a link? The pdf document is stored in the Media Library. Here's the code I have now:

Sitecore.Data.Fields.LinkField linkField = item.Fields["Url"];
tab.NavigateUrl = linkField.Url;
+1  A: 

Same rules apply to a PDF media as to any other type in media library. If you need just to retrieve the media url to construct a link, do the following:

MediaItem mediaItem = linkField.TargetItem;
if(mediaItem != null)
MediaManager.GetMediaUrl(mediaItem);

You can also simply use web control or xsl control to render the link: Web control:

<sc:Link Field="Url" Item="if you need to process specific item" runat="server" />

If your question concerns browser behavior when the link is clicked, set ForceDownload to true:

        <mediaType name="PDF file" extensions="pdf">
          <mimeType>application/pdf</mimeType>
          **<forceDownload>true</forceDownload>**
          <sharedTemplate>system/media/unversioned/pdf</sharedTemplate>
          <versionedTemplate>system/media/versioned/pdf</versionedTemplate>
        </mediaType>
Alex Shyba