In today's digital age, efficient data management and sharing across platforms are crucial for businesses to thrive. Salesforce and SharePoint are two powerful tools that many organizations utilize to manage customer relationships and collaborate on documents. In this blog, we will provide a step-by-step guide along with full working code on how to generate a Salesforce report in CSV format and upload it to a SharePoint site using Apex, Salesforce's programming language.
Step 1: Prerequisites
- Necessary user permissions to access and export reports in Salesforce.
- A valid SharePoint site URL, document library title, and active OAuth bearer token.
- Remote Site Settings or Named Credentials configured in Salesforce for the SharePoint endpoint.
- Basic working knowledge of Salesforce Apex programming and REST integrations.
Step 2: Generate the Salesforce Report in CSV
To generate a Salesforce report in CSV format, we'll utilize the built-in reporting functionality and convert the report data into a CSV string.
// Apex code snippet to generate Salesforce report in CSV String reportId = 'YOUR_REPORT_ID'; String reportCsv = Reports.exportReport(reportId, Reports.FileFormat.CSV);
Step 3: Prepare SharePoint Upload Request
Before uploading the CSV to SharePoint, we need to assemble the target API endpoint and authentication headers.
// Apex code snippet to prepare SharePoint upload configuration
String sharepointUrl = 'YOUR_SHAREPOINT_URL';
String sharepointLibrary = 'DOCUMENT_LIBRARY_NAME';
String accessToken = 'YOUR_SHAREPOINT_ACCESS_TOKEN';
String uploadUrl = sharepointUrl + "/_api/web/lists/getByTitle('" + sharepointLibrary + "')/RootFolder/Files/Add(url='report.csv', overwrite=true)";
Step 4: Perform the HTTP Upload Callout
Using Apex's HttpRequest class, we perform the HTTP POST callout to transfer the CSV blob to SharePoint.
// Apex code snippet to perform file upload to SharePoint
HttpRequest request = new HttpRequest();
request.setEndpoint(uploadUrl);
request.setMethod('POST');
request.setHeader('Authorization', 'Bearer ' + accessToken);
request.setHeader('Content-Type', 'application/octet-stream');
request.setBody(Blob.valueOf(reportCsv));
Http http = new Http();
HttpResponse response = http.send(request);
if (response.getStatusCode() == 200 || response.getStatusCode() == 201) {
System.debug('File uploaded successfully');
} else {
System.debug('File upload failed with status code: ' + response.getStatusCode());
}
Step 5: Complete Apex Class Implementation
Now, let's bring all these steps together into a single, clean Apex utility class.
public class SalesforceToSharePoint {
public static void uploadReportToSharePoint() {
// 1. Generate Report CSV
String reportId = 'YOUR_REPORT_ID';
String reportCsv = Reports.exportReport(reportId, Reports.FileFormat.CSV);
// 2. Configure Endpoint Parameters
String sharepointUrl = 'YOUR_SHAREPOINT_URL';
String sharepointLibrary = 'DOCUMENT_LIBRARY_NAME';
String accessToken = 'YOUR_SHAREPOINT_ACCESS_TOKEN';
String uploadUrl = sharepointUrl + "/_api/web/lists/getByTitle('" + sharepointLibrary + "')/RootFolder/Files/Add(url='report.csv', overwrite=true)";
// 3. Prepare & Execute HTTP Request
HttpRequest request = new HttpRequest();
request.setEndpoint(uploadUrl);
request.setMethod('POST');
request.setHeader('Authorization', 'Bearer ' + accessToken);
request.setHeader('Content-Type', 'application/octet-stream');
request.setBody(Blob.valueOf(reportCsv));
Http http = new Http();
HttpResponse response = http.send(request);
// 4. Handle Response
if (response.getStatusCode() == 200 || response.getStatusCode() == 201) {
System.debug('File uploaded successfully');
} else {
System.debug('File upload failed with status code: ' + response.getStatusCode() + ' Response: ' + response.getBody());
}
}
}
- Rule: Execute long-running report generation and file upload callouts asynchronously inside a Queueable or Schedulable class using
Database.AllowsCallouts. - Gain: Automated document synchronization, elimination of manual CSV exports, and seamless team collaboration in Microsoft 360.
- Price: Requires managing OAuth 2.0 refresh token lifecycles and configuring Named Credentials/External Credentials in Salesforce.
- Limits: Subject to Apex callout payload size limits (6 MB for synchronous, 12 MB for asynchronous) and standard HTTP timeout caps (120s).
Conclusion
By following the steps outlined in this guide, you can seamlessly export a Salesforce report as a CSV and upload it to a SharePoint document library using Apex. This automated integration streamlines cross-platform data sharing, eliminates manual exports, and enhances productivity. Always ensure your remote site settings and OAuth configurations are configured before running callouts in production. Happy coding!