[SalesForce] Community user cannot download file

I am using an old portal for my customers, with customer community user licenses.

In the portal, my users should upload files and then download the files that they have uploaded.

The portal is written in VisualForce pages, and the file mechanism is written in a lightning component with lightning out.

Uploading the file with apex, I am creating a ContentDocumentLink record with the field Visibility='AllUsers' – this is how the file should be available in the community:

// insert a version of the file - if no ContentDocumentId, then it creates a new ContentDocument
ContentVersion cv = new ContentVersion();
cv.ContentLocation = 'S'; // S = within Salesforce, E = External
cv.VersionData = body;
cv.Title = 'fileName';
cv.PathOnClient = 'filename';
insert cv;


cv = [SELECT Id, ContentDocumentId FROM ContentVersion WHERE Id =: cv.Id LIMIT 1];
ContentDocumentLink cdl = new ContentDocumentLink();
cdl.ContentDocumentId = cv.ContentDocumentId;
cdl.LinkedEntityId = parentRecordId;
cdl.ShareType = 'V';
cdl.Visibility = 'AllUsers';
insert cdl;

In my community I use a link to download the file:

<a href="https://yadhanadiv.my.salesforce.com/sfc/servlet.shepherd/version/download/0680y0000035XwEAAU" target="_blank">myFileName</a>

In the past, this method worked OK, and clicking on this link would download the file.

Today, this link is not working for a community user – whenever I am trying to access that url from the user, I get redirected to Salesforce login page with a red text error: To access this page, you have to log in to Salesforce

enter image description here

  • For internal users this link works fine

QUESTIONS

  1. Is this the right way to implement my use case?
  2. Did something change in Salesforce that causes the url now not to work anymore?
  3. Is there a way to fix it or to implement in a different way so my users could download the files that they have uploaded (The file ownership is the community user itself)?

Best Answer

Rolling up comment as answer.

The link in your a href redirects to your Org https://yadhanadiv.my.salesforce.com and not the Community, and thus it always leads you to the login page.

To redirect to your Community URL to access the same servlet resource, you should point it to the Community url, something as https://mycommunity.force.com/rest of the url

Related Topic