Transformation code to check if file is uploaded or not

Shweta S asked on November 27, 2019 20:04

Hi,

Currently I have a code to check if file is uploaded or not as below:

<%# IfEmpty(Eval("BrochurePDF"), "","<div>Some html</div>") %>

This works when a file has never been uploaded but we are having a weird issue. We removed files for some of the pages and the check for IfEmpty fails. If I see the value it is something like '4e3cfb16-fda6-49ca-81b8-b78c280345ec'.

Is there any other way of checking if file is uploaded or not? It's like the reference for that file still exists in the database. The pages are published as well.

Recent Answers


Mike Wills answered on November 28, 2019 02:36

Hi Shweta,

It would probably work to ensure the attachmentGuid resolves to a file URL, by using the GetAttachmentUrlByGUID transformation method.

<%# GetAttachmentUrlByGUID(Eval("AttachmentGUIDColumn"), "<custom_file_name>") %>

Reference:
Reference - Transformation methods

Mike

0 votesVote for this answer Mark as a Correct answer

Shweta S answered on December 2, 2019 19:32

Hi Mike, Thank you for the suggestion. I tried using GetAttachmentUrlByGUID method and it gives me an error that that method is not found. Am I missing something?

0 votesVote for this answer Mark as a Correct answer

Mike Wills answered on December 2, 2019 19:39

Hi Shweta,

What version of Kentico are you using? I believe GetAttachmentUrlByGUID was added in K11. If you are using K10 or earlier, you need to use GetFileUrl.

See:
https://devnet.kentico.com/documentation/api-changes/kentico-11/24985

Mike

0 votesVote for this answer Mark as a Correct answer

Shweta S answered on December 2, 2019 20:29 (last edited on December 2, 2019 20:29)

Hi Mike,

I am using version 10. I used GetFileUrl and the url is still having the guid id for the attachement even though it is removed. When I check the database, it still has guid stored even after removing it. That field value should be null.

We have prev and dev servers and they are both acting differently after removing the attachment pdf.

Prev database: After attachment is removed, page is saved and published, the column still has guid 4e3cfb16-fda6-49ca-81b8-b78c280345ec.

Dev database: After attachment is removed, page is saved and published, the column has guid 0000......

0 votesVote for this answer Mark as a Correct answer

Mike Wills answered on December 3, 2019 03:16

Hi Shweta,

Even though the column still has a guid, what is returned by GetFileUrl when the attachment no longer exists? Does it successfully return a URL even though the attachment is gone?

Also, just to double check, when you say the attachment is removed, is it removed from the published version of the page, or just the current version? Your point about the two servers behaving differently makes me wonder if one server is using workflow and another is not.

Thanks,

Mike

0 votesVote for this answer Mark as a Correct answer

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