Display ContentDocument (Files) on site page

Currently, we have a feature in one of our products that allows you to upload an attachment to one of our objects to use as a thumbnail on the page it’s displayed on. With attachments, I am able to build a URL that allows the site to grab that image for use, and everything works fine.

Because lightning will be doing away with attachments, switching instead to ContentDocuments, I thought I’d look into how to get them to work in a similar way. Unfortunately, I haven’t been able to find a way to actually get a ContentDocument after getting it’s Id. With Attachments, I could just use this url:

sitePrefix + '/servlet/servlet.FileDownload?file=' + attachment.Id

Is there a way to do something similar with ContentDocuments? It would suck pretty bad to tell our clients this just won’t work anymore once SF decides to finally close access to attachments.

Answer

I was having the same issue and frustration. But it looks like this is possible, I just wasnt searching correctly. There are answers out there going back 7+ years.

The key is to use ContentVersionID (ID with 068 prefix). On the ContentDocumentObject, this is the LatestPublishedVersionId. This is what I used within my Community:

 <img src="{!'/<<community name>>/sfc/servlet.shepherd/version/download/'+ContentVersionID }" />

Attribution
Source : Link , Question Author : Dominator_101 , Answer Author : ATA1k

Leave a Comment