0

The url for preview and download of a docx file is not working on Community.

The ContentDocumentLink is connected via the LinkedEntityId field to a custom sObject called SupportingDocuments__c, which is linked to the Contact for the current Community User.

There are two files under this Community User. One is a pdf, and the other is a docx.

The pdf preview and download works well, but the docx does not. The preview url gives me a blank page, and the download url gives me the "URL No Longer Exists": URL No Longer Exists

The ContentDocumentLink for both has Visibility set to "All Users" and ShareType set to "I".

I am essentially querying the connected ContentDistribution, ContentVerison, and ContentDocumentLink and returning a list of my DocumentWrapper subclass.

The Pdf has a ContentDistribution, but the docx does not. Would I need one? And am I building the url correctly? The download and preview urls are built in the Apex classes.

Apex Class

public without sharing class FileController {

   private static final String BASE_URL = URL.getSalesforceBaseUrl().toExternalForm();
        private static List<Network> network;
    
  @AuraEnabled
public static MyResponse getContactFiles(){
--Essentially queries the current user. Queries the Contact. Queries the records of Supporting_Documents under this Contact. Gets the CommunityDocumentLinks under the Supporting Document. Gets the related CommunityDistribution and ContentVersion, returns a response with a list of DocumentWrapper objects. ---
}
   
public class DocumentWrapper{
                @AuraEnabled
                public String name;
                @AuraEnabled
                public String previewUrl;
                @AuraEnabled
                public String downloadURL;
            public DocumentWrapper(contentDistribution distribution, contentVersion version, ContentDocumentLink documentLink){
    network = [SELECT Name, UrlPathPrefix FROM Network WHERE Name = 'communityname'];
        this.name = version.title;
        
if (distribution != null) {
                    this.previewUrl = distribution.DistributionPublicUrl;
                    this.downloadURL = distribution.ContentDownloadUrl;
                    return;
                }
                this.previewUrl = BASE_URL + '/' + network[0].UrlPathPrefix + '/sfc/servlet.shepherd/version/renditionDownload?rendition=THUMB720BY480&versionId=' + version.Id;
                this.downloadURL = BASE_URL + '/' + network[0].UrlPathPrefix + '/sfc/servlet.shepherd/document/download/' + documentLink.ContentDocumentId + '?operationContext=S1';
}

}
}

The Following is a snippet of my JS

import { LightningElement, track, api } from "lwc";
import { NavigationMixin } from "lightning/navigation";
import getFiles from "@salesforce/apex/FileController.getContactFiles";

const columns = [
    {label:'File Name', fieldName:'name', type:'text'},
    {label:'File Extenstion', fieldName:'fileExtension', type:'text'},
    {label: 'Preview', type: 'button', typeAttributes:{
        label: 'Preview', name: 'Preview', variant: 'brand-outline',
        iconName: 'utility:preview', iconPostion: 'right'
    }},
    {label: 'Download', type: 'button', typeAttributes:{
        label: 'Download', name: 'Download', variant: 'brand',
        iconName: 'action:download', iconPostion: 'right'
    }}
];    
export default class DisplayFiles extends NavigationMixin(LightningElement) {

@track DocumentWrapperList;
    @track columnsList = columns;
    connectedCallback() {
        this.getFiles();
    }

getFiles() {
        getContactFiles()
            .then((data) => {
                this.response = data;
                console.log(this.response.isSuccess);
                console.log(this.response.values.DocumentWrapperList);
    if (this.response.isSuccess == true) {
        this.DocumentWrapperList = 
           this.response.values.DocumentWrapperList;
        console.log(this.DocumentWrapperList);

    }
}
handleRowAction(event){
    const actionName = event.detail.action.name;
    const row = event.detail.row;
    switch (actionName) {
        case 'Preview':
            this.previewFile(row);
            break;
        case 'Download':
            this.downloadFile(row);
            break;
        default:
    }
}
previewFile(file) {
            console.log('attempting preview');
            console.log(JSON.stringify(file));
            this[NavigationMixin.Navigate]({
                type: "standard__webPage",
                attributes: {
                    url: file.previewUrl
                }
            }, false);
        }
    
        

downloadFile(file){
            console.log('attempting download');
            console.log(JSON.stringify(file));
            this[NavigationMixin.Navigate]({
                    type: 'standard__webPage',
                    attributes: {
                        url: file.downloadURL
                    }
                }, false 
            );
        }

This is my Table in the LWC

 <div class=slds-p-vertical_small>
            <lightning-datatable
                data={DocumentWrapperList}
                columns={columnsList}
                hide-checkbox-column
                key-field="id"
                onrowaction={handleRowAction}
            >
        </lightning-datatable></div>
2
  • 1
    What's not working? Is there an error? Commented Jun 10, 2021 at 15:54
  • Hey Nicolas. I added an image of what the download url redirects me to. The preview url redirects me to a white page with nothing on it. Commented Jun 10, 2021 at 16:00

1 Answer 1

1

I figured it out! Well someone else figured it out for me. I needed to set the org-wide-defaults under the sObject that is the LinkedEntityId under the ContentDocumentLink (in this case it was my custom object, SupportingDocuments__c) to allow external read access.

Unfortunately, the preview is low-quality compared to the preview I have in Salesforce, or that I have if there is a ContentDistribtuion. On top of that, the preview does not always generate for docx files. I'll upload the same docx file multiple times, and 50% of the time it will say that a preview is not available.

Thanks everyone.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .