DocumentAttachment by FileName

Sylvain C. asked on November 17, 2020 21:46

Hello,

I have an URLs which only contain the name of a document such as (/url?file=doc_1.pdf or /url?file=doc_2.ppt). These documents have been loaded into the Kentico database and appears in the dbo.CMS_Attchment table with their names being stored in the AttachmentName field.

The only way to find a DocumentAttchment I have found is by using the GUID... Is there a way of getting the DocumentAttachment by using the fileName directly?

Thank you,

Sylvain

Correct Answer

Liam Goldfinch answered on November 17, 2020 23:23

Hi Sylvain,

It sounds like you're trying to access a Document Attachment through a unique URL of /url?file=doc_1.pdf which has no context of the document the attachment is attached to?

You could do something like:

// Take file name from query string, just using a local variable as example.
var fileName = "doc_1.pdf";

var attachment = AttachmentInfoProvider.GetAttachments()
    .TopN(1)
    .WhereEquals(nameof(AttachmentInfo.AttachmentName), fileName)
    .FirstOrDefault();

But doing this would just get the first match in the database with this file name. The file name might not be unique as it might be uploaded to multiple documents across different cultures (if the site has multiple cultures).

Ideally you would want to include DocumentGUID or something so you could filter down the list further.

1 votesVote for this answer Unmark Correct answer

Recent Answers


Sylvain C. answered on November 18, 2020 17:17

Thank you very much Liam, it will make it.

0 votesVote for this answer Mark as a Correct answer

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