+ Start a Discussion
Radha Rathinavel PandianRadha Rathinavel Pandian 

how to fetch my content of my document

Hi,
I need to fetch my content of my document which is uplaoded to the attachment.
I need to pass the query in REST API through workbench
So far I tried , Select id,ContentDocumentId,ContentDocument.LatestPublishedVersionId from ContentDocumentLink where LinkedEntityId IN ('a011I00000D55nx') , This gives the result as url,
Anyone of them pleae let me know how to get the body of the file?
Best Answer chosen by Radha Rathinavel Pandian
harsha__charsha__c
The file body is stored in VersionBody field of ContentVersion object. When you query for this using SOQL, it gives you a link some thing like below
 
/services/data/v42.0/sobjects/ContentVersion/06890000003oTkqAAE/VersionData

Run a REST GET request hitting this url. It should give you the file content. You can try this via WORKBENCH, which downloads your file. If you try the same using REST API call in apex, it should give you the body of the file.

Give a try and see if it works.

Sample Http REST request is below. Replace the Id of the ContentVersion in below code and it returns you the file body.
 
Http h = new Http();
HttpRequest req = new HttpRequest();
req.setEndpoint(URL.getSalesforceBaseUrl().toExternalForm() + '/services/data/v42.0/sobjects/ContentVersion/06890000003oTkqAAE/VersionData');
req.setMethod('GET');
req.setHeader('Authorization', 'OAuth ' + UserInfo.getSessionId());
req.setHeader('Content-Type', 'application/json');
HttpResponse res = h.send(req);

system.debug(res.getBody());

- Harsha

All Answers

harsha__charsha__c
The file body is stored in VersionBody field of ContentVersion object. When you query for this using SOQL, it gives you a link some thing like below
 
/services/data/v42.0/sobjects/ContentVersion/06890000003oTkqAAE/VersionData

Run a REST GET request hitting this url. It should give you the file content. You can try this via WORKBENCH, which downloads your file. If you try the same using REST API call in apex, it should give you the body of the file.

Give a try and see if it works.

Sample Http REST request is below. Replace the Id of the ContentVersion in below code and it returns you the file body.
 
Http h = new Http();
HttpRequest req = new HttpRequest();
req.setEndpoint(URL.getSalesforceBaseUrl().toExternalForm() + '/services/data/v42.0/sobjects/ContentVersion/06890000003oTkqAAE/VersionData');
req.setMethod('GET');
req.setHeader('Authorization', 'OAuth ' + UserInfo.getSessionId());
req.setHeader('Content-Type', 'application/json');
HttpResponse res = h.send(req);

system.debug(res.getBody());

- Harsha
This was selected as the best answer
Radha Rathinavel PandianRadha Rathinavel Pandian
Harsa,

Can I have the version data(BLOB) in workbench without using code that you have given.
harsha__charsha__c
I don't think we can. Not 100% sure. May I know, what are you trying to achieve by getting the blob data through workbench??

- Harsha
Radha Rathinavel PandianRadha Rathinavel Pandian
We are integrating ICM to save the document. Inorder to get the BLOB data we can able to pass throught the API link with BLOB format.
ranjit_1992ranjit_1992
Hi Radha
There is another approach by using only queries.
You can use ContentNote. Use quer like below:
recId = Id of the related record e.g. Case 
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

The TextPreview field will give you the value of the note Content.

Thanks
Ranjit
 
Rajendiran Karuppaiah 1Rajendiran Karuppaiah 1
Hi Harsha,
What would be the format of the response body from  '/services/data/v42.0/sobjects/ContentVersion/06890000003oTkqAAE/VersionData'  REST service?

I am trying to fetch few pdf and xls files from SFDC org1 and store it in SFDC org2. I could get the contentversion versondata body using the service '/services/data/v42.0/sobjects/ContentVersion/06890000003oTkqAAE/VersionData'. But I am not sure what is the format of the response and how to store it in SFDC org2. I tried the below code, it creates pdf and xls files but the files are corrupted.
 
Http h = new Http();
    HttpRequest req = new HttpRequest();
    req.setEndpoint(URL.getSalesforceBaseUrl().toExternalForm() + '/services/data/v42.0/sobjects/ContentVersion/06890000003oTkqAAE/VersionData');
    req.setMethod('GET');
    req.setHeader('Authorization', 'OAuth ' + UserInfo.getSessionId());
    req.setHeader('Content-Type', 'application/json');
    HttpResponse res = h.send(req);
    
    contentVersion cv = new contentVersion();
    cv.FirstPublishLocationId = hpeKavId;
    cv.Title = 'Solution pdf';
    cv.PathOnClient = 'Solution.pdf';
    cv.VersionData = Blob.valueof( response.getbody() );
    
    insert cv;