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


  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)?
  • 1
    The link in your a href redirects to your Org and not the Community https://yadhanadiv.my.salesforce.com and thus it seems it always leads you to the login page. Have you tried using your Community URL to access the same servlet resource, something as https://mycommunity.force.com/rest of the url.
    – Jayant Das
    Dec 5, 2018 at 13:58
  • Good catch @JayantDas Dec 6, 2018 at 10:22
  • 1
    @JayantDas it worked!!!! thank you so much. can you please write that as an answer so I can accept it and you can also be rewarded? Dec 9, 2018 at 10:57
  • Glad that it worked. Added an answer so that it also helps anyone else in same scenario.
    – Jayant Das
    Dec 9, 2018 at 16:12

3 Answers 3


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


Your download link doesn't work, because your user doesn't have access to file or your link is not correct.

1) To build correct link use this code:

String fileDownloadUrl = URL.getSalesforceBaseUrl().toExternalForm()  + '/sfc/servlet.shepherd/document/download/' + contentDocument.Id + '?operationContext=S1';

2) If user don't have access to file. (You can check in on file details page in Share with section). You need to share a file before download it. (Create new ContentDocumentLink between e.g Contact and ContentDocument).

ContentDocumentLink contentDocumentLink = new ContentDocumentLink(
                                          LinkedEntityId = recordIdWithWhichYouWantToShareFile,
                                          ContentDocumentId = contentDocumentIdWhichYouWantShare,
                                          shareType = 'V',
                                          Visibility = 'AllUsers'

insert contentDocumentLink ;

You probably need also ContentDocumentLinkTrigger to allow you user to view access before share.

trigger ContentDocumentLinkTrigger on ContentDocumentLink (before insert) {
    for (ContentDocumentLink cdl : Trigger.new) {
        cdl.visibility = 'AllUsers';

Please check my post where I explain in more details.

  • Welcome to SFSE! This site isn't a place for people to promote their own blogs and such. If you'd like to contribute to the community here, please take the tour and read How to Answer.
    – Derek F
    Jan 12, 2020 at 16:15
  • Hi Derek, I edited my answer so it can be more helpful ;) Jan 12, 2020 at 16:36

As per this Knowledge Article files created before a community exist cannot be shared with the community.
However, any file that is created after the community is created can be shared with the community. If there is a ContentDocumentLink with Visibility=AllUsers and the LinkedEntityId is pointing to the Org, that file will be visible on the community.
So the best solution would be to create a trigger to make sure all relevant files will get the correct sharing settings. Then use Data Loader to download, delete and re-upload all your files.
It could be a side effect of with the new way files are shared in communities in Winter '19. Release Notes.

  • Thank you @AvailableName although the case you have shared here is real, it does not solve my problems - I am trying to download my own files that I have just uploaded to the community, with the same user and the same community that exists for more than a year now. Dec 9, 2018 at 11:00

You must log in to answer this question.

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