How to get File's extension

Kayla Johnson asked on October 26, 2017 19:00

https://devnet.kentico.com/questions/media-file-extension-in-transformation I tried using this and other guides to get the file extension. But FileExtension field in the File type does not exist so I don't see how Eval("FileExtension") would even work.

Also instead of Eval("Media",true) I have Eval("FileAttachment",true) does not have an ext query var. It just something like "http://website.com/Kentico/CMS/CMSPages/e65912d9-99bd-4022-9a28-f80f1e780d4c"

Correct Answer

Trevor Fayas answered on October 26, 2017 20:12

For the storage issue, try adding a "" after wards like this? THe 2nd statement should store and not declare it, which is odd...

{% path = SiteObjects.Documents.Where("DocumentID = "+ DocumentID ).FirstItem.AllAttachments.FirstItem.ToString().ToUpper(); return ""; %}

Return command

Use the return command to terminate the processing of a macro, and set the attached expression as the macro's final result. You can use the return command inside loops, or anywhere in compound macro expressions.

0 votesVote for this answer Unmark Correct answer

Recent Answers


Trevor Fayas answered on October 26, 2017 19:10

To get some context, where is this file stored? Is it in the Media Library, as a File type on the Content tree, or as an attachment?

Second, how are you trying to pull it in, a repeater? Query Repeater? etc.

Answer those and i'll be able to help ya!

0 votesVote for this answer Mark as a Correct answer

Kayla Johnson answered on October 26, 2017 19:24

It's a transformer. the file is in the page tree as a File type.

0 votesVote for this answer Mark as a Correct answer

Peter Mogilnitski answered on October 26, 2017 19:32

Try Eval("AttachmentExtension")

0 votesVote for this answer Mark as a Correct answer

Kayla Johnson answered on October 26, 2017 19:35

AttachmentExtension also returns nothing

0 votesVote for this answer Mark as a Correct answer

Trevor Fayas answered on October 26, 2017 19:37

For File page type objects, the file is stored as an Attachment in the CMS_Attachment table.

In the CMS_Attachment table, you can find your attachment by the AttachmentDocumentID (which in the repeater will be your DocumentID).

I would recommend using the text/xml transformation so you can leverage the macro engine if possible, and use:

{% SiteObjects.Documents.Where("DocumentID = "+DocumentID).FirstItem.AllAttachments.FirstItem.AttachmentExtension %}

Or just create a custom Transformation Method that performs:

CMS.DocumentEngine.AttachmentInfoProvider.GetAttachments(TheDocumentID, false).FirstObject.AttachmentExtension

0 votesVote for this answer Mark as a Correct answer

Kayla Johnson answered on October 26, 2017 20:02 (last edited on October 26, 2017 20:07)

Using XML Transformation mostly worked. But I can't figure out how to make a variable.

{% string path = SiteObjects.Documents.Where("DocumentID = "+ DocumentID ).FirstItem.AllAttachments.FirstItem; %}

This just outputs it rather than storing it in path...

By removing the type delcaration I am able to use path later in the transformation but it still outputs it in addition to storing it...

    {% path = SiteObjects.Documents.Where("DocumentID = "+ DocumentID ).FirstItem.AllAttachments.FirstItem.ToString().ToUpper(); %}
0 votesVote for this answer Mark as a Correct answer

Peter Mogilnitski answered on October 26, 2017 20:07 (last edited on October 26, 2017 20:07)

Well I guess you can do something ugly like GetAttachmentIcon("FileAttachment") this will give back the string like <i aria-hidden="true" class="icon-file-jpeg icon-file-default"></i>. Then you can use regex to extract the extension. the format is always the same: icon-file-Your extension

0 votesVote for this answer Mark as a Correct answer

Kayla Johnson answered on October 26, 2017 20:28 (last edited on October 26, 2017 20:35)

That syntax is so bizarre. Using that macro worked!

0 votesVote for this answer Mark as a Correct answer

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