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":
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>