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.

SharePoint
SharePoint
A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.
9,865 questions
SharePoint Development
SharePoint Development
SharePoint: A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.Development: The process of researching, productizing, and refining new or existing technologies.
2,713 questions
{count} votes

1 answer

Sort by: Most helpful
  1. ChengFeng - MSFT 5,010 Reputation points Microsoft Vendor
    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