Try our conversational search powered by Generative AI!

Link to PDF from a Nav item

Vote:
 

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!

#120952
Apr 28, 2015 17:57
Vote:
 

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

#120958
Apr 28, 2015 22:54
Vote:
 

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.

#120965
Apr 29, 2015 9:26
Vote:
 

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 :)

#120968
Apr 29, 2015 9:47
Vote:
 

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.

#120984
Apr 29, 2015 14:35
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.