function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Madhusudan Singh 19Madhusudan Singh 19 

Post blob data from Salesforce to Sharepoint

Hi,

I am integrating salesforce with SharePoint where I have to send files directly to SharePoint from the browser without creating File/Attachment inside salesforce.

With the SharePoint REST API, I am able to create a file in Sharepoint but when I download the file, it is corrupted.
If the uploaded file is of PNG type then once it is uploaded if we view it image content is incomplete and colors are faded.
If we upload pdf file then some portion of pdf will be missing after upload.

But when I create an Attachment with the same base64 encoded string, I am able to view the complete and correct file.
 
public static void createFileInSharepoint(String fileName, Blob fileContent, String folderName){
	HttpRequest req = new HttpRequest();
	req.setEndpoint('callout:Sharepoint/_api/web/GetFolderByServerRelativeUrl(\'/sites/SalesforceFiles/Shared%20Documents/'+folderName+'\')/Files/add(url=\''+fileName+'\',overwrite=true)');
	req.setBodyAsBlob(fileContent);
	req.setHeader('Content-Type','application/octet-stream');
	request.setHeader('Content-Length', String.valueOf(req.getBodyAsBlob().size()));
	req.setMethod('POST');		
	Http http = new Http();
	HTTPResponse res = http.send(req);
	System.debug(res.getBody());
}

I went through multiple blogs, but many articles are old and not working anymore.

Please help.

Regards
Madhusudan Singh
ShivankurShivankur (Salesforce Developers) 

Hi Madhusudan,

Please check out the latest suggestions posted in below thread, if it helps you make it more better:
https://salesforce.stackexchange.com/questions/54989/upload-files-to-sharepoint-from-salesforce-and-access-them-from-salesforce/80217

Hope above information helps. Please mark as Best Answer so that it can help others in future.

Thanks.