Search code examples
salesforcesalesforce-lightningsalesforce-communities

How to use URLFOR() in Lightning web component of salesforce?


I am new to file handling in Salesforce. I am currently working on downloading files of all records of a custom object. I read many blogs and found there are a couple of ways to do it. I decided to go with the shepherd URLFOR() approach as it seems simple and easy but now I have all the ContentDocumentId in a string format and I don't know how to use URLFOR() in the Lightning web component. Every blog and post gives the URL but I was not able to find how to use it once it is made. Is it used in Apex or HTML pages?

Can someone guide me?

Here is the URL I have formed

{!URLFOR('/sfc/servlet.shepherd/version/download/' & StringOfContentDocumentIds &'?')}


Solution

  • This is the expression syntax used in VisualForce and in Aura component.

    You're talking about Lightning Web Component (known as LWC) so this syntax is not valid. I would advise to use the standard approach which is using the navigation service.

    If you really want to use the shepherd trick, then you need to write something like this:

    fileDownload.js

    import { LightningElement } from 'lwc'
    
    export default class FileDownLoad extends LightningElement {
      contentVersionIds = []
    
      get urlOfContentDocumentFile () {
        return `/sfsites/c/sfc/servlet.shepherd/version/download/${this.contentVersionIds.join('/')}?`
      }
    }
    

    fileDownload.html

    <template>
      <a href={urlOfContentDocumentFile} target="_blank">Download</a>
    </template>
    

    Please note that I changed the url to add /sfsites/c before as you seems to talk about a community. If it's not, then you can just remove that part.

    P.S: I don't know how you get the list of contentVersionIds so I didn't cover that part.