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?

8
  • Use named credentials.. ? you dont need session id. Commented Jan 15, 2021 at 12:54
  • Named credentials? You can't put dynamic value inside a named credential, it is only static value.
    – Patlatus
    Commented Jan 15, 2021 at 12:55
  • Apex mdapi? gist.github.com/AltiusRupert/0670513bb4a4a2c3af09d7d51633705a ? Commented Jan 15, 2021 at 12:56
  • Before you start using Apex Mdapi, I think you still need Remote Site Setting to be created manually for that to work, correct?
    – Patlatus
    Commented Jan 15, 2021 at 12:59
  • If you want to use session ID approach, I posted an answer for fetching VFDoman session ID when in lightning session. salesforce.stackexchange.com/a/330223/45062 . Basically its an intermediate page which gives you the session id. Commented Jan 15, 2021 at 13:18

1 Answer 1

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

You must log in to answer this question.

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