Generate Public URL for Salesforce Files

by Rijwan Mohmmed
0 comment
generate-public-url-for-salesforce files-techdicer

Hello friends, today we are going to discuss How to Generates Public URLs for Salesforce Files. So we can show these files without salesforce login.

Also check this: Create Reusable Dependent Picklist in LWC

You can handle this very efficiently with Salesforce by using the Content Delivery object; API Name: ContentDistribution.

Creating a record on the ContentDistribution object with a link to the respective ContentVersion Id is sufficient for exposing a file publicly.

Two fields are automatically populated when you create a record of the ContentDistribution type, namely DistributionPublicUrl and ContentDownloadUrl.

DistributionPublicUrl link can be preview and there is a download link so you can also download this.

Process :

There are two options that you can create this

Manually Way : you need to go files and open any file there is a button named “Public link” click on this and we can get a public link and share to another user.

generate-public-url-for-salesforce files-public-lin-button-techdicer
generate-public-url-for-salesforce files-public-lin-button-techdicer

2.By Apex :

GeneratePublicUrl.cls :

public class GeneratePublicUrl {

    public static void generatePublicLink(Id contentDocumentId){
        /* Getting ContentVersion file using ContentDocument Id */
        ContentVersion cv = [SELECT Id, Title FROM ContentVersion WHERE ContentDocumentId = :contentDocumentId];

        /* Creating ContentDistribution record */
        ContentDistribution conDis = new ContentDistribution();
        conDis.Name = cv.Title;
        conDis.ContentVersionId = cv.Id;
        conDis.PreferencesAllowViewInBrowser= true;
        insert conDis;

Output :

generate-public-url-for-salesforce files-output-techdicer

Reference :

  1. ContentDistribution

You may also like

Leave a Comment