I trying to upload file to sharepoint folder with public access through salesforce code

Sainath Jaybhaye 0 Reputation points
2023-10-12T12:52:10.1133333+00:00
HttpRequest httpRequestToSend = new HttpRequest();  
String pathToStoreFile = '/sites/Communication%20site/_api/web/GetFolderByServerRelativeUrl(\'/Shared%20Documents\')/files/add(url=\'document3.txt\',overwrite=true)';
httpRequestToSend.setEndpoint('callout:SharepointsFiles'+pathToStoreFile);
httpRequestToSend.setMethod('POST');
httpRequestToSend.setBodyAsBlob(Blob.ValueOf('test Message'));
System.debug('***** httpRequestToSend-->' + httpRequestToSend);
Http http = new Http();   
HttpResponse httpResponse = http.send(httpRequestToSend);  
System.debug('***** httpResponse-->' + httpResponse);

Above code snipped is used to post test file from Salesforce to sharepoint folder getting 403 checked with folder permission have all required. please guide me if anyone can.

Microsoft 365 and Office SharePoint Development
Microsoft 365 and Office SharePoint For business Windows
{count} votes

1 answer

Sort by: Most helpful
  1. Anonymous
    2023-10-13T02:14:18.7566667+00:00

    Hi @Sainath Jaybhaye,

    A 403 error when trying to post a file from Salesforce to a SharePoint folder typically indicates an issue with permissions or authentication. Here are some steps to troubleshoot and potentially resolve the issue:

    1.Verify SharePoint Permissions:

    Double-check the permissions on the SharePoint folder where you are trying to post the file. Ensure that the Salesforce integration user (the user making the request) has the necessary permissions to add files to the folder.

    2.Authentication:

    Ensure that you are correctly authenticated with SharePoint. If you are using callouts to SharePoint, make sure that you have configured the authentication correctly. You may need to use OAuth or other suitable authentication methods.

    HttpRequest httpRequestToSend = new HttpRequest();
    String pathToStoreFile = '/sites/Communication%20site/_api/web/GetFolderByServerRelativeUrl(\'/Shared%20Documents\')/files/add(url=\'document3.txt\',overwrite=true)';
    httpRequestToSend.setEndpoint('callout:SharepointsFiles' + pathToStoreFile);
    httpRequestToSend.setMethod('POST');
    httpRequestToSend.setHeader('Content-Type', 'application/octet-stream'); // Set the appropriate content type
    httpRequestToSend.setHeader('Authorization', 'Bearer ' + yourAccessToken); // Include your SharePoint access token here
    httpRequestToSend.setBody(Blob.toPDF('test Message')); // Ensure that you are setting the appropriate content as per your requirements
    System.debug('***** httpRequestToSend-->' + httpRequestToSend);
    Http http = new Http();
    HttpResponse httpResponse = http.send(httpRequestToSend);
    System.debug('***** httpResponse-->' + httpResponse);
    

    In this code, make sure to replace yourAccessToken with the actual access token required for SharePoint authentication. You should also ensure that the content type and body of the request are correctly set according to SharePoint's requirements.


    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    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.

    Best Regards

    Cheng Feng


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.