Hi Nathan,
What version of CMS are developing for?
I would recommend having a content reference (if on CMS 7.5+) on a page type and use that together with UrlResolver to link to a file.
If you are on CMS 7 and below then you could add a property of type Url to link to a file or create a page type that has logic to redirect to a file (the URL will be to the page and not the file though).
Regards,
Tobias
It's not an uncommon requests but it really is terrible for the end-user.
A link to a PDF should clearly reveal that it is and it's also suggested to display the target's file size. Not nice to have in any navigation area.
In case you're using MVC and EPiServer CMS 7.5+
You can define a new page type as Toni suggested:
[ContentType(GUID = "c5aabd75-eeec-41e1-bfbf-63d1f98b6ba8")] public class FileLinkPage : PageData { [UIHint(UIHint.MediaFile)] public virtual ContentReference File { get; set; } }
And have something like this in the view:
<ul class="nav"> @{ var contentLoader = ServiceLocator.Current.GetInstance<IContentLoader>(); var children = contentLoader.GetChildren<IContent>(ContentReference.StartPage); foreach (var child in children) { <li> @if (child is FileLinkPage) { var fileLinkPage = (FileLinkPage)child; if (!ContentReference.IsNullOrEmpty(fileLinkPage.File)) { <a href="@Url.ContentUrl(fileLinkPage.File)" target="_blank">@fileLinkPage.Name</a> } } else { <a href="@Url.ContentUrl(child.ContentLink)">@child.Name</a> } </li> } } </ul>
Notes:
1. It's not a good practice to have this logic in the view (ServiceLocators, etc.) You can borrow the logic from AlloyTech and create navigation links somewhere inside IPageViewModel > LayoutModel
2. It will not filter container pages. You should check if child page have a template.
Hope this helps :)
Johan, believe me, I know! But our client base can be pretty insistent when they want to be, particularly about bad ideas.
Thank you, Dejan and Toni for your help.
Is it possible to link to a file like a pdf directly from a sub or main nav item into a new tab/window? If so, what's the best way to do it?
Thanks!