0

I'm trying to download a ContentDocument from community, here is the url:

String downloadUrl = Url.getSalesforceBaseUrl().toExternalForm() + '/sfc/servlet.shepherd/document/download/' + ContentDocumentId + '?operationContext=S1'

But every time I press the download's button, I'm redirected to the maintenance page...

2 Answers 2

0

It's because, Salesforce files aren't hosted on your salesforce domain, instead if you download your file and check the source it will show something like,

https://<your-instance>-dev-ed--c.documentforce.com/sfc/servlet.shepherd/version/download/documentId?operationContext=S1

So either you need to trim down the URL like,

String documentId = Cd.ContentDocumentId;
String baseURL = URL.getSalesforceBaseURL().toExternalForm(). split ('-ed.')[0];
basURL+='c.documentforce.com/sfc/servlet.shepherd/version/download/'+documentId+'?operationContext=S1';

which you can try but i wouldn't recommend. Since their is a dedicated SObject to handle file URL's, the ContentDistribution. Though, it isn't created automatically like ContentDocument when ContentVersion is created, instead either you create it manually or when the user clicks 'Public URL' under salesforce files.

So additionally you need to create ContentDistribution for your files if they're being shared publically.

0

After some research and attempts I found the solution. Here is the code I used:

String UrlDownload = '/' + yourCommunityName + '/sfc/servlet.shepherd/document/download/' + contentDocumentId;

You must log in to answer this question.

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