Tag:

community

    View Files in Salesforce Lightning Community Portal

    Community Portals are very important for business to engage customer and partners. Salesforce provide community cloud to make these portals using point and click as well as advance custom lightning portals.

    As per part of community cloud, we mostly required to show files to our customers.  By default files as only accessible to internal user. These are not accessible to external user (community users are external users).

    This post is explain how we can show files in lighting community portal.  Below permission is required to set for community users

    1. Create a custom profile by cloning standard Customer Community Plus profile
    2. Add permission “View Content in Portal” in newly created custom profile
    3. Assign this profile to community users

    Let us create lightning component to show and download files which will be used in lightning community portal.

    As mentioned above, by default files are only accessible to internal users. So we have to make it accessible for others users as well. This can be done by setting visibility of files to all users.

    We can make a trigger on ContentDocumentLink object. Whenever new file is uploaded this trigger make that file visible to all.

    trigger ContentDocumentLinkTrigger on ContentDocumentLink (before insert) {
        for(ContentDocumentLink l:Trigger.new) {
            l.Visibility='AllUsers';
        }
    }

    To make existing files visible to external user run below code in anonymous window. We can retrieve all files using ContentDocument and iterate each file and make visible to all users.

    List<ContentDocumentLink> toUpdate = new List<ContentDocumentLink>();
    List<ContentDocument> docs=[Select id from Contentdocument];
    for(ContentDocument doc:docs)
    {
        for(ContentDocumentLink link : [
            select Id, Visibility
            from ContentDocumentLink
            where contentdocumentid=:doc.id
            ]){
            link.Visibility = 'AllUsers';
            toUpdate.add(link);
        }
    }
    update toUpdate;

    After above step is done, files will be visible to community user as well. Now we have to get content when files will be accessed from community so let us create apex class to get files.

    There is two way to download files

    1. using Blob
      file.Content=EncodingUtil.base64Encode(file.VersionData)
      
      file.ContentType='application/pdf'

      and use this blob like

       <a href="{!'data:' + v.file.ContentType+';base64,'+v.file.Content}">Download File</a>
      
    2. using apex system url
      //This will return base url of request
      private static final String  BASE_URL = URL.getSalesforceBaseUrl().toExternalForm();
      
      //This will download files
      data.DownloadUrl =BASE_URL + '/sfc/servlet.shepherd/document/download/'+file.ContentDocumentId;
      
      //This will create preview of file 
      data.FileUrl =BASE_URL + '/sfc/servlet.shepherd/version/renditionDownload?rendition=THUMB720BY480&versionId='+file.Id;
                 
      

    Full Apex Class:

    Lightning Component:

    As we can retrieve all file of case so we can use function GetEntityRecordFiles of above class.  GetFile will retrieve specific document

    Reference:

    https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_url.htm

    https://help.salesforce.com/articleView?id=collab_salesforce_files_parent.htm&type=5

    February 2, 2020 0 comment
    0 Facebook Twitter Google + Pinterest