Clone CMS.File and have it's Url Path render the file

Trevor Fayas asked on February 24, 2017 18:31

Hey everyone,

anyone ever figure out how to get the same functionality as a CMS.File in a custom (cloned) page type? I know you can reference the file guid filed and get the url, but just curious if there is a way to replicate the whole "Url goes to the file" (ex if you put a CMS.File object as /Assets/MyFile", that /Assets/MyFile will bring up that file.

Checked web.config keys and couldn't find anything.

Correct Answer

Brenden Kehren answered on February 24, 2017 18:52

What I've done is created a specific page template for that new page type and assign that page type to use that page template by default On the template, I have a repeater which gets that file/attachment and renders it via a redirect. It works and allows users to create page aliases (original request for me). See transformation code below:

<script runat="server">
  protected override void OnInit(EventArgs e)
  {
     if(CMS.PortalEngine.PortalContext.ViewMode == CMS.PortalEngine.ViewModeEnum.LiveSite)
     {
         string guid = ValidationHelper.GetString(Eval("FileAttachment"), "");
         if(!string.IsNullOrEmpty(guid))
         {
             Response.Redirect(URLHelper.ResolveUrl("/getattachment/" + guid + "/" + ValidationHelper.GetString(Eval("FileLongName"), "file") + ".aspx"));
         }
     }
  }
</script>

Another way might be to create a handler vs. the redirect.

1 votesVote for this answer Unmark Correct answer

Recent Answers


Trevor Fayas answered on February 24, 2017 19:39

Thank you Brenden! I'll add this to my code snippets!

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.