1

I understand that there are several approaches to get content version data.

The default and widely accepted approach is to make self-callout to REST API to get VersionData of VersionData field on Content Version:

public with sharing class ContentVersionService {
public static Blob getContent(ContentVersion cv) {
    Http h = new Http();
    HttpRequest r0 = new HttpRequest();
    r0.setMethod('GET');
    r0.setEndpoint(URL.getSalesforceBaseUrl().toExternalForm() + '/services/data/v47.0/sobjects/ContentVersion/0681x000001p65tAAA/VersionData');
    r0.setHeader('Authorization', 'Bearer ' + UserInfo.getSessionId());
    HTTPResponse res0 = h.send(r0);
    System.debug(LoggingLevel.ERROR, '@@@ : status' + res0.getStatus());
    System.debug(LoggingLevel.ERROR, '@@@ : status' + res0.getStatusCode());
    System.debug(LoggingLevel.ERROR, '@@@ : keys' + res0.getHeaderKeys());
    for (String key: res0.getHeaderKeys()) {
        System.debug(LoggingLevel.ERROR, '@@@ : key: ' + key + ' header = ' + res0.getHeader(key));
    }
    System.debug(LoggingLevel.ERROR, '@@@ : ' + res0.getBody());

    return res0.getBodyAsBlob();
}

}

However, there are two drawbacks on this approach. First, it requires Remote Site Settings to be added manually for every client who might use this code, so it doesn't see a viable solution for ISV.

Second, it doesn't work with Lightning Session Id.

When a user tries to use this approach, an error message is returned

[{"message":"This session is not valid for use with the REST API","errorCode":"INVALID_SESSION_ID"}]

Another suggested approach is to query ContentNote.

Id contentDocId = [SELECT ContentDocumentId FROM ContentDocumentLink WHERE LinkedEntityId = 'recId '];
ContentNote cn = [ select id,Title,CreatedDate, Content,TextPreview from ContentNote Where Id = 'contentDocId' ]; // gives value of note body and title

However, this approach doesn't work for me at all because it throws an error

[INVALID_TYPE] sObject type 'ContentNote' is not supported.

Does anyone know or have a clues if is this possible to get content version data without making self callout?

Patlatus
  • 16,621
  • 12
  • 76
  • 178

1 Answers1

1

I think I will use the fragile approach of screen-scraping like this

    PageReference pr = new PageReference('/sfc/servlet.shepherd/version/download/' + contentVersionId + '?asPdf=false&operationContext=CHATTER');
    System.debug(LoggingLevel.ERROR, '@@@ : ' + pr.getContent() );

I understand that it might be broken in the next version of Salesforce, however, this looks like the only working solution or workaround in this case.

It works from Lightning Web Component or Aura Component. It doesn't require a Remote Site Settings or Named Credentials to be manually setup on every subscriber org

Patlatus
  • 16,621
  • 12
  • 76
  • 178