Not able to Create a file in Sharepoint using REST API

Madhu Sudan Singh 61 Reputation points
2021-07-07T18:58:35.893+00:00

Hello,

I am able to create a file through POSTMAN, but not with the Salesforce Apex code(Java type language).
I am getting the error as "HTTP Error 400. The request is badly formed." while creating the file.

public class SharepointIntegrationController{
private static String accessToken = '';
public static void generateAccessToken(){
Http http = new Http();
HttpRequest request = new HttpRequest();
request.setEndpoint('https://accounts.accesscontrol.windows.net/<MyTenantId>;');
request.setMethod('POST');
request.setHeader('Content-Type', 'application/x-www-form-urlencoded');
String payload = 'grant_type='+EncodingUtil.urlEncode('client_credentials','UTF-8')+'&resource='+EncodingUtil.urlEncode('00000003-0000-0ff1-ce00-000000000000/<MySharepointName>.sharepoint.com@<MyTenantId>','UTF-8')+'&client_id='+EncodingUtil.urlEncode('<MyClientId>@<MyTenantId>','UTF-8')+'&client_secret='+EncodingUtil.urlEncode('<MySecretKey>','UTF-8');
request.setBody(payload);
HttpResponse response = http.send(request);
Map<String, Object> result = (Map<String, Object>)JSON.deserializeUntyped(response.getBody());
accessToken = (String)result.get('access_token'); // Extracting accessToken from the Response and assigning it to class variable
createFile(); // Calling method to Create a sample File
}

public static void createFile(){
Http http = new Http();
HttpRequest request = new HttpRequest();
request.setEndpoint('https://<MySharepointName>.sharepoint.com/sites/SalesforceFiles/_api/web/GetFolderByServerRelativeUrl('/sites/SalesforceFiles/Shared Documents/SalesforceFiles\')/Files/add(url=\'d.txt\',overwrite=true)');
request.setMethod('POST');
request.setHeader('Authorization', 'Bearer '+accessToken);
request.setBody('This is sample file');
HttpResponse response = http.send(request);
System.debug(response.getBody()); // HTTP Error 400. The request is badly formed.
}
}

Can you please highlight where I am going wrong?

Thanks in Advance.

Regards
Madhusudan Singh

Microsoft 365 and Office SharePoint Server For business
Microsoft 365 and Office SharePoint For business Windows
Microsoft 365 and Office SharePoint Server Development
Microsoft Partner Center API
{count} votes

Accepted answer
  1. RaytheonXie_MSFT 40,471 Reputation points Microsoft External Staff
    2021-07-08T10:01:40.667+00:00

    Hi @Madhu Sudan Singh ,
    The wrong url may cause the error 400.
    We should make sure all "'" escaped.
    113011-image.png

    Take the following code as sample:

    request.setEndpoint('https://<MySharepointName>.sharepoint.com/sites/SalesforceFiles/_api/web/GetFolderByServerRelativeUrl(\'/sites/SalesforceFiles/Shared Documents/SalesforceFiles\')/Files/add(url=\'d.txt\',overwrite=true)');  
    

    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.

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.