1

I have 3 Document records with pdf, word document & image respectively. I need to show the files outside Salesforce(i.e without salesforce login). All of the records have 'Externally Available Image' checked.

Now using the URL - https://c.ap1.content.force.com/servlet/servlet.ImageServer?id=01590000009hxW6&oid=00D90000000w5zN&lastMod=1442409870000 I am able to open the image file, but forming a similar URL for Pdf or word document is not helping. Its redirecting me to the salesforce login page.

Any idea how should I approach for pdfs & word documents? And why is it working for images but not for pdfs?

3 Answers 3

3

An easier approach would be to use Chatter Files.
When you upload a file to chatter, you have an option to Share via Link. That link is publically available.

E.g. the 2 links below are from my Dev Org.

Word Document

PDF Document

An added advantage is that you can preview the file before downloading.

1
  • Thanks a lot for this answer. I was searching for ways to implement your answer when I found out about ContentDelivery and it worked perfectly! I am going to select this as Best Answer and put one answer of my own regarding ContentDelivery. Thanks once again! Commented Sep 18, 2015 at 10:12
1

You can make any Document available through a Force.com site. That is, as long as a) the Force.com site 'profile' has read access to Documents in general and b) the Force.com site 'user' has access to the folder that the Documents are stored in (usually they have, because a folder is generally available to 'everybody').

A URL from a Force.com site linking to a Document, would look generally like this (Sandbox):

https://someForceComSiteName.cs80.force.com/portal/servlet/servlet.FileDownload?file=01223000000DXoDBAG

edit: I see that you use servlet.ImageServer in your URL. That one only works for images. For documents like doc and pdf, you need to use servlet.FileDownload

3
  • True, but my actual requirement doesn't include usage of Site. I implemented this by ContentDelivery & ContentVersion. Thanks anyways for your help! Commented Sep 18, 2015 at 10:07
  • 1
    If that works good enough, then sure that is the best solution! For more complicated scenarios you might still want to use Force.com Sites, it's pretty powerful and free for all enterprise licenses :-) Commented Sep 18, 2015 at 10:16
  • Agreed! I will anyhow explore the potential of chatter files now! Commented Sep 18, 2015 at 10:22
0

Good answers by Willem & AAU. Other than those I was able to find one more way. ContentVersion & ContentDelivery. Upload your document as a ContentVersion & create a related ContentDelivery record, the latter one contains the public URL for your document. Here is a sample code. I converted Attachment to a ContentVersion.

        Attachment attach = new Attachment();
        attach.Body = filebody;
        attach.Name = filename;
        attach.ContentType = contentType;
        attach.ParentId = account.Id;
        insert attach;

        ContentVersion cv = new ContentVersion();
        cv.VersionData = attach.Body;
        cv.Title = attach.Name;
        cv.PathOnClient = attach.Name;
        insert cv;
        ContentDistribution cd = new ContentDistribution();
        cd.name = attach.Name;
        cd.ContentVersionId = cv.Id;
        cd.PreferencesAllowOriginalDownload = true;
        cd.PreferencesAllowPDFDownload = true;
        cd.PreferencesAllowViewInBrowser = true;
        insert cd;

        ContentDistribution cdFetched = [SELECT Id, DistributionPublicUrl from ContentDistribution where Id =:cd.Id];
        System.debug('Public URL'+cdFetched.DistributionPublicUrl);

The best thing about ContentDelivery is that it has a Boolean field called PreferencesAllowViewInBrowser which if true allows you to display the content of the file in browser without any third party document viewer. Pretty Awesome!

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .