+ Start a Discussion
sonninhosonninho 

Can documents be downloaded from Force.com Sites?

Hi community,

 

Wondering if anyone can shed me some lights. I'm this close to bang my head against the wall.

 

On our Force.com Sites, we have a "Case Study" page which lists some PDF/Excel/Words files. These files are stored in Documents in Salesforce. We want people visit our site to download them.

 

I wrote a simple VF page to output a download link, and a controller to query the Document object. But when I click on the Download link, I get the "Authorization Required" error message. I guess this is to do with Security setting between the Documents Object and the Guest User License. I did grant the Read permission on this object but no luck.

 

Is there any way to make this work? I don't really want to use Static Resource to hold these PDF files.

 

Here is my code:

 

DocumentDownload.page

 

<apex:page controller="DocumentCon" action="{!getFile}">
<apex:outputLink value="/servlet/servlet.FileDownload?file={!doc.Id}&oid={!$Organization.Id}">
Download Now!
</apex:outputLink>
</apex:page>

 

 

DocumentCon.cls

 

public class DocumentCon {	
public Document doc {get;set;}

public PageReference getFile() {
String fileId = System.currentPageReference().getParameters().get('id');

if(fileId != null) {
doc = [SELECT Id, Name, Description, ContentType, Type, Url, BodyLength, Body
FROM Document WHERE Id = :fileId];
}
return null;
}
}

 

 

 

Best Answer chosen by Admin (Salesforce Developers) 
dwwrightdwwright

Sonninho,

 

I ensured the following things, and was able to make an .xls file attached to a document downloadable on a sites page:

 

1. In the Sites Profile ("public access settings"), enable Read access to Documents (You indicate that this is already the case)

 

2. Edit the folder in which the document is stored, and ensure that the Sites profile has access to the folder. To do this, I went to Admin setup ---> Manage Users ---> Public Groups and created a group consisting of ONLY the User defined by my Sites profile. You can then add this "group" to the permissions on the folder.

 

Use <apex:outputLink> and in the value put "/servlet/servlet.FileDownload?file=(your docs ID here)"

 

Hope this helps.

All Answers

BulentBulent

please use the new "Preview as Admin" feature to find the reason for the authorization error.

It could be the read permission on site public access settings for documents or something else.

RyanGuestRyanGuest

I notice your link starts with /servlet..., double check that you don't have a site prefix, otherwise the link would be /prefix/servlet...

sonninhosonninho

Thanks for the replies Bulent, Ryan. But still no luck.

 

I added the component preview as admin but there is no error to display since it only appears if you click on the "Download Now" link.

 

I guess the issue I am trying to resolve here is: can public users read the documents that are stored in salesforce?

<site:previewAsAdmin/>
dwwrightdwwright

Sonninho,

 

I ensured the following things, and was able to make an .xls file attached to a document downloadable on a sites page:

 

1. In the Sites Profile ("public access settings"), enable Read access to Documents (You indicate that this is already the case)

 

2. Edit the folder in which the document is stored, and ensure that the Sites profile has access to the folder. To do this, I went to Admin setup ---> Manage Users ---> Public Groups and created a group consisting of ONLY the User defined by my Sites profile. You can then add this "group" to the permissions on the folder.

 

Use <apex:outputLink> and in the value put "/servlet/servlet.FileDownload?file=(your docs ID here)"

 

Hope this helps.

This was selected as the best answer
sonninhosonninho

dwwright, awesome. Works like a charm. Thanks a lot.

BritishBoyinDCBritishBoyinDC

Just wondering - isn't Content a better solution for this?

dev401hasdev401has

Yes Content would have been better option.

In Documents you can upload a single file upto 5 MB size only.

For Content you can go for much higher size.

 

I have developed a site and used Content in it.

Site incase you want to refer: http://nbcuni.force.com/commops

sonninhosonninho

dev401has, definitely Content is a much better solution, nicer UI but it has its downside, for example: users must have flash installed on their browsers to view the content (there are still number of people out there using IE6, god helps me). And also with Documents I can do something like this:

 

 

List<Document> docs = [SELECT Name, Description, Id, ContentType, Type, Url,BodyLength FROM Document where Document.Folder.Type='Document' and Document.Folder.DeveloperName='Public_Documents'];

 

Then display them on the site automatically, presume I have a number of files, I don't have to manually fix the link with content delivery links.

 

I personally much prefer Content, but most of time is about what business wants and not what we want .

 

dev401hasdev401has

Hi Sonninho

 

Flash is a downside when using content deliveries but that can be solved.

 

Refer the link Sites using Content Version. Solution has been described using content version instead of Content deliveries. In this the downside of Flash as well as pasting the link have been removed. You get direct link and no need of Flash.

 

I am not sure about documents. I think it will ask for ID/Password?

 

And yes it definitely boils down to one point i.e business. I had worked hard and gave quite a time on the content version and finally as per business requirement I am using Content deliveries in my site http://nbcuni.force.com/commops

 

Anyways thanks a lot for your insight on content.

sonninhosonninho

Thanks a lot dev401has, brilliant, I never thought of that. Now I can get around my 5Mb limit issue in Documents.

 

It works very much similar to my solution with Documents, I just need to replace my custom controller to query from ContentVerision instead of Document object.

 

Re the authentication (ID/Password) you mentioned, I got around it by creating a public group for the Guest user (again, similar to the thread you gave), works fine for me. Have a look at our site here.

 

I can't wait for Salesforce to open up their ContentDelivery API to do some funky stuff with it ;). Love this community forum, thanks everyone for the suggestions.

 

 

 

 

dev401hasdev401has

Hi Sonniho

 

Well It was actually Bulent's solution in that link. So you need to thank him but I will take some credit of posting that link here! :)

 

Your site looks cool. Mine is simple VF page with very less customization.

I am looking forward for indexing my site: NBC Commercial Guidelines in major search engines.

Do you have any Idea or can you give any insight into that if you have followed any crucial steps for your site?

 

 

sonninhosonninho

Hi dev401has,

 

Sorry I haven't been on this thread lately. Just want to catch up with you and see if you have managed to get your site index? I have done a site recently and it works quite well. Google picks up few hundres pages over a night. 

 

Cheers,

 

Sonny