Close Menu
SalesforceCodex
    Facebook X (Twitter) Instagram
    Trending
    • How to Confidently Manage Transactions in Salesforce Apex
    • Building a Dynamic Tree Grid in Lightning Web Component
    • 10 Salesforce Chrome Extensions to Boost Your Productivity
    • How to Build a Generic Modal Window in Lightning Web Component
    • Top 10 Salesforce Flow Features of Salesforce Summer ’25
    • Unlock the Power of Vibe Coding in Salesforce
    • How to Implement Dynamic Queueable Chaining in Salesforce Apex
    • How to Implement Basic Queueable Chaining in Salesforce Apex
    Facebook X (Twitter) Instagram
    SalesforceCodex
    Subscribe
    Sunday, July 13
    • Home
    • Architecture
    • Salesforce
      • News
      • Apex
      • Integration
      • Books Testimonial
    • Questions
    • Certification
      • How to Prepare for Salesforce Integration Architect Exam
      • Certification Coupons
    • Integration Posts
    • Downloads
    • About Us
      • Privacy Policy
    SalesforceCodex
    Home»Salesforce»View Files in Salesforce Lightning Community Portal

    View Files in Salesforce Lightning Community Portal

    Dhanik Lal SahniBy Dhanik Lal SahniFebruary 2, 20203 Comments2 Mins Read
    Facebook Twitter Pinterest LinkedIn Tumblr Email
    View Files in Salesforce Lightning Community Portal
    Share
    Facebook Twitter LinkedIn Pinterest Email

    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

    apex community community user lightning
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email
    Previous ArticleAudio to Text by Google Speech API in Salesforce Lightning
    Next Article Salesforce Interview Question – Security
    Dhanik Lal Sahni
    • Website
    • Facebook
    • X (Twitter)

    With over 18 years of experience in web-based application development, I specialize in Salesforce technology and its ecosystem. My journey has equipped me with expertise in a diverse range of technologies including .NET, .NET Core, MS Dynamics CRM, Azure, Oracle, and SQL Server. I am dedicated to staying at the forefront of technological advancements and continuously researching new developments in the Salesforce realm. My focus remains on leveraging technology to create innovative solutions that drive business success.

    Related Posts

    By Dhanik Lal Sahni7 Mins Read

    How to Confidently Manage Transactions in Salesforce Apex

    July 13, 2025
    By Dhanik Lal Sahni4 Mins Read

    Building a Dynamic Tree Grid in Lightning Web Component

    June 29, 2025
    By Dhanik Lal Sahni9 Mins Read

    10 Salesforce Chrome Extensions to Boost Your Productivity

    June 1, 2025
    View 3 Comments

    3 Comments

    1. joao lopes on May 20, 2020 1:52 pm

      Thank you so much for this post, I was really going in circles trying to see the files in a community in order to make the conga work there and this post really made it posible, so thanks a lot!!

      Reply
      • Dhanik Lal Sahni on May 31, 2020 1:23 am

        Thank You Joao, this post helped you.

        Reply
    2. Pingback: salesforce share case attachments with community plus portal users - howsonline.com

    Leave A Reply Cancel Reply

    Expert Salesforce Developer and Architect
    Ranked #1 SALESFORCE DEVELOPER BLOG BY SALESFORCEBEN.COM
    Featured on Top Salesforce Developer Blog By ApexHours
    Recent Posts
    • How to Confidently Manage Transactions in Salesforce Apex
    • Building a Dynamic Tree Grid in Lightning Web Component
    • 10 Salesforce Chrome Extensions to Boost Your Productivity
    • How to Build a Generic Modal Window in Lightning Web Component
    • Top 10 Salesforce Flow Features of Salesforce Summer ’25
    Ranked in Top Salesforce Blog by feedspot.com
    RSS Recent Stories
    • Discover the Top 10 Salesforce AppExchange Apps to Boost Productivity July 10, 2025
    • Top 20 Salesforce Data Cloud Interview Questions & Answers for Admins June 5, 2025
    • How to Connect Excel to Salesforce to Manage Your Data and Metadata February 9, 2025
    • Difference Between With Security and Without Security in Apex January 2, 2025
    • Top Reasons to Love Salesforce Trailhead: A Comprehensive Guide December 5, 2024
    Archives
    Categories
    Tags
    apex (113) apex code best practice (8) apex rest (11) apex trigger best practices (4) architecture (22) Asynchronous apex (9) AWS (5) batch apex (9) batch processing (4) code optimization (8) codescan (3) custom metadata types (5) design principle (9) flow (15) future method (4) google (6) google api (4) integration (19) integration architecture (6) lighting (8) lightning (66) lightning-combobox (5) lightning-datatable (10) lightning component (32) Lightning web component (64) lwc (53) named credential (8) news (4) optimize apex code (4) Permission set (4) Queueable (9) queueable apex (4) rest api (23) S3 Server (4) salesforce (144) salesforce apex (49) salesforce api (4) salesforce api integration (5) Salesforce Interview Question (4) salesforce knowledge (3) salesforce news (5) salesforce question (5) solid (6) tooling api (5) Winter 20 (8)

    Get our newsletter

    Want the latest from our blog straight to your inbox? Chucks us your detail and get mail when new post is published.
    * indicates required

    Facebook X (Twitter) Instagram Pinterest YouTube Tumblr LinkedIn Reddit Telegram
    © 2025 SalesforceCodex.com. Designed by Vagmine Cloud Solution.

    Type above and press Enter to search. Press Esc to cancel.

    Ad Blocker Enabled!
    Ad Blocker Enabled!
    Our website is made possible by displaying online advertisements to our visitors. Please support us by disabling your Ad Blocker.