2

We need to upload files to sharepoint and able to view the uploaded files to sharepoint in salesforce. So we cant achieve this in File Connect, where it is not supporting two way synchronisation.

So im trying to achieve this from Apex using REST API, I'm able to get the access token, able to get the list of files and folders from Sharepoint.

When i try to upload files into sharepoint, im getting response like this:

[Status=Bad Request, StatusCode=400]

But when i try to upload the files using Postman i can able to upload it.

HttpRequest httpRequestToSend = new HttpRequest();  

httpRequestToSend.setEndpoint('https://testsharepoint.sharepoint.com/sites/testsite/_api/web/GetFolderByServerRelativeUrl(\''+'/sites/testsite/Shared Documents/Test 1'+'\')/files/add(url=\''+'document3.txt'+'\', overwrite=true)');
httpRequestToSend.setMethod('POST');
httpRequestToSend.setHeader('Authorization', 'Bearer ' + access_token);
httpRequestToSend.setHeader('Content-Type','application/json;odata=verbose');

//httpRequestToSend.setBody('test message');
httpRequestToSend.setBodyAsBlob(Blob.ValueOf('test Message'));

System.debug('***** httpRequestToSend-->' + httpRequestToSend);

Http http = new Http();   
HttpResponse httpResponse = http.send(httpRequestToSend);  
System.debug('***** httpResponse-->' + httpResponse);

I'm able to get the access token, able to the list of files and folders. I was wondering why it's only happening for file upload, i checked with permissions as well while registering the app in sharepoint

I'm referring this blog, i didnt found any proper documentation related to sharepoint integration from apex

Any suggestions/ideas are much appreciated

1

Finally after i spent 6 hours with more patience, i figured it out whats been causing the issue, In the end point there is space between the folder names, so some how postman managed that to encode the string and sending it, but salesforce is not encoding that

Prev Code:

httpRequestToSend.setEndpoint('https://testsharepoint.sharepoint.com/sites/testsite/_api/web/GetFolderByServerRelativeUrl(\''+'/sites/testsite/Shared Documents/Test 1'+'\')/files/add(url=\''+'document3.txt'+'\', overwrite=true)');

It should be like this:

httpRequestToSend.setEndpoint('https://testsharepoint.sharepoint.com/sites/testsite/_api/web/GetFolderByServerRelativeUrl(\''+'/sites/testsite/Shared%20Documents/Test%201'+'\')/files/add(url=\''+'document3.txt'+'\', overwrite=true)');

This works for me. Thanks.

0

The thing I'm seeing here is that you (and the blog you were following here) failed to put a space in between "Bearer" and your access token.

httpRequestToSend.setHeader('Authorization', 'Bearer' + access_token);

should be

httpRequestToSend.setHeader('Authorization', 'Bearer ' + access_token);

  • actually i tried with adding space next to the Bearer, for my another testing purpose i just removed the space, i mistakenly added the code without space. Apologises for that, i updated my question as well – Yaswanth Tikkisetti Feb 15 at 7:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.