question

MadhuSudanSingh-1785 avatar image
0 Votes"
MadhuSudanSingh-1785 asked RichaSingh-1115 commented

Files are not clear when uploaded through Sharepoint REST API

Hi,

When I am uploading files to Sharepoint using REST API, files are getting stored in Sharepoint, but those files are incomplete. For Image files, either color are not clear or only half of the image is uploaded. For pdf files, some portion of the page itself is not displaying.

I am not sending any header other than 'Authorization' and in body, I am sending Blob content.

I am doing Salesforce integration with Sharepoint through Salesforce's APEX coding and not JQuery/Javascript

Regards
Madhusudan Singh

office-sharepoint-onlineoffice-sharepoint-server-developmentoffice-sharepoint-server-customization
· 8
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @MadhuSudanSingh-1785 ,
Could you please provide more details about your code and Blob content for test?

0 Votes 0 ·

@RaytheonXie-MSFT ,

Below code, I am using, which is creating a file in Sharepoint but those files are incomplete.

HttpRequest req = new HttpRequest();
req.setEndpoint('callout:Sharepoint/_api/web/GetFolderByServerRelativeUrl(\'/sites/SalesforceFiles/Shared%20Documents/Test\')/Files/add(url=\''+fileName+'\',overwrite=true)');
req.setBodyAsBlob(fileContent); // this is blob file content
req.setHeader('Content-Type','application/octet-stream');
req.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 also posted a question on stack overflow but no response there as well. This is turning a blocker for the entire project.

Stackoverflow question
https://salesforce.stackexchange.com/questions/349530/post-file-to-sharepoint-from-apex




0 Votes 0 ·

If you were to do an incomplete upload of a file (i.e. not transfering the whole blob) then this could result in broken images and PDFs like yours...

Is there something wrong with the "Content-Length" in your header?
Could you try to set it directly as "fileContent.size()" instead of "req.getBodyAsBlob().size()"?

0 Votes 0 ·
Show more comments

Hi @MadhuSudanSingh-1785 ,
I am checking to see if the problem has been resolved.

0 Votes 0 ·

@RaytheonXie-MSFT , Yes it is resolved(found a workaround). It seems the problem is from the salesforce end. I raised a ticket to salesforce.

0 Votes 0 ·

Hi @MadhuSudanSingh-1785 ,
It is very glad that the issue is resolved.
Thank you for your sharing and it will help others have the same issue.

0 Votes 0 ·
RichaSingh-1115 avatar image RichaSingh-1115 MadhuSudanSingh-1785 ·

Hi @MadhuSudanSingh-1785 - I am trying to implement the same and facing similar issues, can you let me know how did you solve it?

0 Votes 0 ·

1 Answer

RaytheonXie-MSFT avatar image
0 Votes"
RaytheonXie-MSFT answered sadomovalex commented

Hi @MadhuSudanSingh-1785 ,
Please try upload files by postman like following picture:
114934-image.png
I have tested to upload pdf and image files, and it works.


If an Answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.



image.png (33.5 KiB)
· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

through postman it is working, but not with the code

0 Votes 0 ·

Hi @MadhuSudanSingh-1785 ,
Since the url can work in postman, we consider that something might be wrong in code.
But Salesforce's APEX code is out of our support scope. You can get answers from Salesforce's APEX code support.

0 Votes 0 ·
sadomovalex avatar image sadomovalex MadhuSudanSingh-1785 ·

you may check in Fiddler details of your http request (which doesn't work) and request from Postman (which works) and compare them. If there will be difference - try to use the same request properties as Postman uses in your request.

0 Votes 0 ·