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

SharePoint Server
SharePoint Server
A family of Microsoft on-premises document management and storage systems.
2,227 questions
SharePoint
SharePoint
A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.
9,684 questions
SharePoint Server Development
SharePoint Server Development
SharePoint Server: A family of Microsoft on-premises document management and storage systems.Development: The process of researching, productizing, and refining new or existing technologies.
1,575 questions
Microsoft Partner Center API
Microsoft Partner Center API
Microsoft Partner Center: A Microsoft website for partners that provides access to product support, a partner community, and other partner services.API: A software intermediary that allows two applications to interact with each other.
316 questions
{count} votes

Accepted answer
  1. RaytheonXie_MSFT 31,606 Reputation points Microsoft Vendor
    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