File sharing via Lightning Component in Salesforce

Posted By : Sukirti Sharma | January 14, 2020

While working on Salesforce, one might need to share documents, images, pdfs outside the organization. This is useful when communicating with clients, contacts or leads who are outside the salesforce org environment. There are other ways to do it as well, but in this article, I am going to talk about how I did it in the Salesforce Lightning component.

We are an ERP development company that provides Salesforce development services that enable businesses to enhance customer experience. Our developers integrate Salesforce into existing systems to streamline information flows and provide visibility into customer information visibility.

Key Steps for File Sharing Via Salesforce Lightning Component

The Salesforce lighting component I was working with was like a custom chatbox and I needed to send attachments outside the organization to the client, I am chatting with.

I used “lightning:fileinput” element in the “cmp” file like below  :

File sharing via Lightning Component in Salesforce
Share images and pdfs outside the organization with the Salesforce lightning component. Explore how file sharing is done via lightning component in Salesforce.

<aura:attribute name=“accept” type=“List” default=“[‘.jpg’, ‘.jpeg’ , ‘.png’ ,’.pdf’]”/>

   <aura:attribute name=“multiple” type=“Boolean” default=“false”/>

   <aura:attribute name=“disabled” type=“Boolean” default=“false”/>

   <aura:attribute name=“recordId” type=“String” default=“”/>

<lightning:fileUpload  name=“fileUploader

                          multiple=“{!v.multiple}”

                          accept=“{!v.accept}”

                          disabled=“{!v.disabled}”

                          recordId=“{!v.recordId}”

              onuploadfinished=“{!c.handleUploadFinished }”/>

Accept attribute in the element filter out the unnecessary file type while browsing the file. It shows only the file type you have mentioned in the Accept List.

A recorded attribute is mandatory, as each file must be related to some record. In my case, I am using a dynamic contact recorded of a contact I am chatting with.

Controller.js is just pointing out to the Helper.js :

handleUploadFinished:function(component, event, helper){

       helper.handleUploadFinishedH(component, event , helper);

o

   }

 

Here lightning:fileUpload is automatically uploading the file details to ContentDocumet and ContentVersion object of salesforce, and returning us the Name of the file as well as the Document Id(which is ContentDocumet Id)

Helper.js is calling a function to send media in the apex controller that takes the returned Name of the file and Document Id and performs DML operation to get ContentVersionId.

This ContentVersionId is required to generate DistributionPublicUrl by inserting a new record in ContentDistribution of related ContentVersionId.

DistributionPublicUrl is the URL we get when we share a file externally by manually clicking on ‘Share via link’ option for an uploaded file.

Helper.js :

handleUploadFinishedH:function(component, event, helper){

       var uploadedFiles = event.getParam(“files”);

       var documentId = uploadedFiles[0].documentId;

       var fileNamewithExt = uploadedFiles[0].name;

       var action = component.get(“c.sendMedia”);

         action.setParams({

                fileNamewithExt: fileNamewithExt,

                documentId : documentId

               })         

 

         action.setCallback(this, function(response){

         var state = response.getState();

         if(state === ‘SUCCESS’){

             var dpu = response.getReturnValue();

                var prev = component.get(“v.messageBox”);

                var newdata = prev.concat(dpu)

                component.set(“v.messageBox”,newdata);

         }else{

                   alert(‘Error in generating media URL. Please try again after reloading page.’);

         }

       });

       $A.enqueueAction(action);

   } 

chatboxController.apcx :

@AuraEnabled

   public Static String sendMedia(String fileNamewithExt , String documentId){

       String contentVersionId = [select Id from ContentVersion where ContentDocumentId= :documentId].Id;

       ContentDistribution cd = new ContentDistribution();

       cd.Name = fileNamewithExt;

       cd.ContentVersionId = contentVersionId;

       cd.PreferencesAllowViewInBrowser= true;

       cd.PreferencesLinkLatestVersion= true;

       cd.PreferencesNotifyOnVisit = false;

       cd.PreferencesPasswordRequired = false;

       cd.PreferencesAllowOriginalDownload= true;

       insert cd;

       String dpu = [select DistributionPublicUrl from ContentDistribution where Id= :cd.Id].DistributionPublicUrl

       return dpu; 

   }

 The returned URL is then set into the text box and we can send it via the chat. This URL does not require any authentication or password to view so that it can be shared with anyone manually too.

If you want to generate a password for users to view it, it can easily be done by setting PreferencesPasswordRequired to true.

cd.PreferencesPasswordRequired = true;

And then you can either see the password in the ContentDistribution table under the password field or get it via a simple query.

Thus, we can create a File Sharing Link through our Salesforce Lightning Component.

At Oodles, we provide Salesforce integration services to enhance user experience with a single authentication. Our developers streamline complex CRM operations to meet specific business requirements.

Connect with our ERP development team to avail benefits of the Salesforce lightning component!

To Read Next: Enhancing Customer Experience with Salesforce Chatbot Integration

guest
0 Comments
Inline Feedbacks
View all comments

Latest Blogs