5

I have attached a File to my custom object record. I want to display a link to the file on a VF page which also shows information about my custom object.

I tried doing something like this.

APEX:

feedAttachmentList = [SELECT ContentDocument.title,ContentDocumentId FROM ContentDocumentLink WHERE LinkedEntityId =:documentID];

Visualforce:

<apex:dataTable value="{!feedAttachmentList}" var="Feedfile" id="fileTable" styleClass="tableClass" headerClass="tablehead" rowClasses="odd,even">
        <apex:column styleClass="centerHeader" headerClass="centerCol"> 
        <apex:facet name="header">Files attached to this record</apex:facet>
        <a href="/sfc/servlet.shepherd/version/download/{!Feedfile.ContentDocumentId}" target="_document">{!Feedfile.ContentDocument.title}</a>
        </apex:column> 



</apex:dataTable>

This displays the link and the title on my page but clicking it results in this.

top.location='https://cs11.salesforce.com/ex/errorduringprocessing.jsp'

What am I doing wrong? I just want to display a link so that the user can download the File.

6

You want to Query the Latest Published Version Id for the given ContentDocument

So when you change the query to:

feedAttachmentList = [SELECT ContentDocument.LatestPublishedVersionId, ContentDocument.title,ContentDocumentId FROM ContentDocumentLink WHERE LinkedEntityId = :document.Id]

And your page to:

<apex:dataTable value="{!feedAttachmentList}" var="Feedfile" id="fileTable" styleClass="tableClass" headerClass="tablehead" rowClasses="odd,even">
    <apex:column styleClass="centerHeader" headerClass="centerCol"> 
    <apex:facet name="header">Files attached to this record</apex:facet>
    <a href="/sfc/servlet.shepherd/version/download/{!Feedfile.ContentDocument.LatestPublishedVersionId}" target="_document">{!Feedfile.ContentDocument.title}</a>
    </apex:column> 

Then you should be good to go!

3
  • 1
    Cool. This worked. So why didn't it work the way I had it? Also in Salesforce on the Chatter file, we can generate a link to share the file to external users where it opens up in a neat File preview window. How can I obtain this link through my code? Thanks a ton! Jul 3 '13 at 20:20
  • I'm really sure how to find that link. You could try to reconstruct the link as it is in the chatter feed. Besides that, i'm not sure the components that SF use to create the preview are available for external users Jul 7 '13 at 20:29
  • Would you know how to change the downloaded file name on client using JS ? Apr 10 '18 at 8:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.