Search code examples
c#asp.netkentico

Kentico CMS 10 - DocumentHelper Attachment URL


When using the CMS.DocumentEngine.DocumentHelper to retrieve documents of a custom page type, how would one get the path of an image attached to a Direct Uploader form control property type?

If documents/page are being retrieved in the following way:

DocumentQuery documents = DocumentHelper.GetDocuments("Custom.PageType")
   .Path("/some-path", PathTypeEnum.Children)
   .OnCurrentSite()
   .Culture("en-us")
   .Published()
   .Page(page, size);

Then looping through retrieved documents to project to a custom class:

foreach(var document in documents) {
    Foo foo = new Foo(document.getStringValue("SomeCustomColumnName", ""));
    foos.add(foo);
}

Using getStringValue() targeting the Direct Uploader field/property of the custom page type returns a document GUID such as "123456f-5fa9-4f64-8f4b-75c52db096d5".

In transformations, I can use GetFileUrl() such as GetFileUrl("AttachmentColumnName") to get the path, how could I use that in ASPX code behind when working with retrieved documents?

The custom page type data is provided to the client via an ASMX service and Ajax. The returned JSON data is used to generate/append markup to the page, including an <img />.

Thank you for any help you can provide!


Solution

  • You can try something like this in your item template:

    /CMSPages/GetFile.aspx?guid=<%# Eval("SomeCustomColumnName")%>
    

    You can also check to see if SomeCustomColumnName is empty or not before generating that link too if you don't want a broken image.

    The other option is to convert that file upload to a URL selector and allow the users to upload files to the media library. This is probably a better approach IMHO.