SharePoint Integration with Salesforce
Why Integrate with SharePoint?
One of the main reasons is cost efficiency. With SharePoint integration, an organization can significantly reduce Salesforce storage costs. A standard SharePoint storage provides 1 TB by default, and additional storage is available at an affordable price of $0.20/month per GB (check online for exact pricing).
Getting Familiar with SharePoint Terms
1. Tenant
A tenant represents an organization and is a dedicated instance of Azure Active Directory received after signing up for a Microsoft Azure account.
2. Site
A website that contains various SharePoint web components like Document Library, Calendar, Task List, etc. Multiple sites can exist under a single tenant.
3. Document Library
A secure place to store and manage files related to a specific project or client for efficient collaboration among co-workers.
4. Drive
A top-level object that represents a user’s OneDrive or a document library in SharePoint.
5. Drive Item
Represents a file, folder, or other item stored in a drive.
Prerequisites for Integration
Before implementing the integration, certain configurations in Microsoft Azure and Salesforce are necessary. You may require assistance from the SharePoint Admin to complete these steps.
Microsoft Azure Setup
- Login to Microsoft Azure.
- Navigate to Azure Active Directory.
- Create an app registration for your Tenant:
- Click App Registrations.
- Click New Registration.
- Enter the App Name and select the required options.
- Click Register.
- Note down the Client Id and Tenant Id.
- Configure Authentication:
- Click on Authentication.
- Add a Redirect URI (Salesforce Org domain: https://<domain-name>.my.salesforce.com).
- Generate Client Secret:
- Navigate to Certificates & Secrets.
- Click New Client Secret and note down the value.
- Request API Permissions (Usually granted by SharePoint Admin):
- Click Add a Permission → Select Microsoft Graph.
- Select the following permissions:
- Files.Read
- Files.Read.All
- Files.ReadWrite
- Files.ReadWrite.All
- Sites.Read.All
- Sites.ReadWrite.All
Note: Delegated permissions are for user-based API requests, whereas Application permissions require explicit SharePoint Admin approval.
- Retrieve SharePoint Site ID:
- Access this URL in your browser:
https://<tenant>.sharepoint.com/sites/<site-url>/_api/site/id
Salesforce Setup for SharePoint Integration
Storing Authentication Details in Salesforce
As a best practice, store Tenant Id, Client Id, Client Secret in a Custom Metadata Record in Salesforce:
- Azure Grant Type = client_credentials
- Azure Scope = https://graph.microsoft.com/.default
Configure Remote Site Settings
- Microsoft Graph API: https://graph.microsoft.com
- Microsoft Login: https://login.microsoftonline.com
Integration using Microsoft Graph API
Microsoft Graph API provides multiple endpoints to interact with SharePoint using REST API standards. Below is an Apex class to handle the integration.
Apex Class for SharePoint Integration
public with sharing class SharePointIntegration {
private static String tenantId = ‘Your_TenantId’;
private static String clientId = ‘Your_ClientId’;
private static String clientSecret = ‘Your_ClientSecret’;
private static String grantType = ‘client_credentials’;
private static String scope = ‘https://graph.microsoft.com/.default‘;
private static String accessToken;
private static Datetime tokenExpiry;
public List<String> sharePointFiles { get; set; }
public String fileName { get; set; }
public Blob fileContent { get; set; }
public String fileUploadMessage { get; set; }
public SharePointIntegration() {
sharePointFiles = new List<String>();
}
public static String getAccessToken() {
if (accessToken != null && tokenExpiry != null && tokenExpiry > Datetime.now()) {
return accessToken;
}
HttpRequest req = new HttpRequest();
req.setEndpoint(‘https://login.microsoftonline.com/’ + tenantId + ‘/oauth2/v2.0/token’);
req.setMethod(‘POST’);
req.setHeader(‘Content-Type’, ‘application/x-www-form-urlencoded’);
String body = ‘client_id=’ + clientId +
‘&client_secret=’ + clientSecret +
‘&grant_type=’ + grantType +
‘&scope=’ + scope;
req.setBody(body);
Http http = new Http();
HttpResponse res = http.send(req);
if (res.getStatusCode() == 200) {
Map<String, Object> responseMap = (Map<String, Object>) JSON.deserializeUntyped(res.getBody());
accessToken = (String) responseMap.get(‘access_token’);
// Token validity of 1 hour
Integer expiresIn = (Integer) responseMap.get(‘expires_in’);
tokenExpiry = Datetime.now().addSeconds(expiresIn – 60);
return accessToken;
} else {
return null;
}
}
public static String getSiteId() {
String accessToken = getAccessToken();
if (accessToken == null) return null;
HttpRequest request = new HttpRequest();
request.setEndpoint(‘https://<tenant>.sharepoint.com/sites/<site-name>/_api/site/id’);
request.setMethod(‘GET’);
request.setHeader(‘Authorization’, ‘Bearer ‘ + accessToken);
Http http = new Http();
HttpResponse response = http.send(request);
if (response.getStatusCode() == 200) {
Map<String, Object> responseMap = (Map<String, Object>) JSON.deserializeUntyped(response.getBody());
return (String) responseMap.get(‘id’);
} else {
return null;
}
}
}
Visualforce Page for SharePoint Integration
<apex:page controller=”SharePointIntegration”>
<apex:form>
<apex:pageBlock title=”SharePoint Integration”>
<apex:pageMessages />
<apex:pageBlockSection title=”Fetch Files from SharePoint”>
<apex:commandButton value=”Fetch Files” action=”{!fetchFiles}” />
<apex:pageBlockTable value=”{!sharePointFiles}” var=”file” id=”fileList”>
<apex:column value=”{!file}” headerValue=”File Name” />
</apex:pageBlockTable>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>
Conclusion
This guide provides a step-by-step approach to integrating SharePoint with Salesforce using Microsoft Graph API. By leveraging SharePoint’s storage, organizations can reduce Salesforce storage costs and improve document management.