Sharepoint Integration With Salesforce

by :Rajesh K 

 19

salesforce sharepoint integration

Hi Folks,
In this post, I want to address how we can integrate Sharepoint with Salesforce through Microsoft Graph API. We all know that we are in the era of cloud. We may want to connect two cloud services. So, How can we do that? Through integrating one cloud service into another cloud service. Let’s start the integration. (If you want to integrate Sharepoint to other applications, convert the below code into any language that you used in your application.)

What is Sharepoint?

Organizations use SharePoint to create websites. You can use it as a secure place to store, organize, share, and access information from any device. It is a very robust content management and document management platform used by all kinds of companies, mainly for storing business-related documents and content. It is mostly used as a secure document repository for intranet portals, websites, cloud-based services, and of course as an independent repository as well.

What is the Microsoft Graph API?

Microsoft Graph is the gateway to data and intelligence in Microsoft 365. You can use the Microsoft Graph API to build apps for organizations and consumers that interact with the data of millions of users. With Microsoft Graph, you can connect to a wealth of resources, relationships, and intelligence, all through a single endpoint: https://graph.microsoft.com.

All set, let’s get started!

Register Your Application in Azure Portal

  • Sign in to the Azure Portal. (https://portal.azure.com/)
  • Go to the Azure Active Directory in the left side navigation pane.Azure+Home    
  • 1) Click App Registrations then 2) Click New application registration.
  • Follow the below steps.

          1) Enter the Nameof your application. 

          2) Choose application type as “Web App/ API”. 

          3) Enter Sign-on URL like “https://mycustomdomain.my.salesforce.com“.
          4) Click “Create”.

Register+App

 

  • Now you will get the registered application information. Copy and keep the Application ID somewhere we will use it later.
  • Go to settings in the top menu in your application info section.
  • Go to the Required Permissions.
App+Settings

 

  • Click the “Add” button to add the permissions to your application.
  • Now we have two steps to do. 1) Select an API 2) Select Permissions for that API. In our case, we need to select the “Microsoft Graph” API and in the next step, we will give permissions related to this API. 
  • Currently, I am giving “Read and write files in all site collections” permission only.  We will add more permissions later. 
  • Don’t forget to click the “Done” button after adding permissions.
  • It’s a time to generate Private Key for our application. Go Back to the “Settings” navigation pane. Then click “Keys”. Fill the description of the key and then select expires duration. Then click the “Save” button. It will provide us a private key. Copy the key value and store it in a secure place. You won’t be able to retrieve the same key after. 
  • To add more permissions to your app, go to the “App registrations (Preview)” section. Then select the app that we created recently (Force Application).  Copy the “Directory (tenant) ID” we will use it later.
  • Then Click the “View API Permissions” button. 
  • Now go to “Add a Permission“.
  • In “Select an API” section select “Microsoft Graph” API.
  • Go to the “Delegated Permissions” and add the listed permissions. Also, add the permissions in the “Application Permissions” section as well. 
  • The following permissions are required to create a folder and upload files in Sharepoint.

     Delegated permissions :

          Files.Read

          Files.Read.All

          Files.ReadWrite

          Files.ReadWrite.All

          Sites.Read.All

          Sites.ReadWrite.All

  Application Permissions:

Files.Read.All

          Files.ReadWrite.All

          Sites.Read.All

          Sites.ReadWrite.All

Application Permissions – Your client application (Salesforce) needs to access the Web API (MS Graph API) directly without user context. This type of permission requires administrator consent and is also not available for Native client applications.

Delegated Permissions – Your client application (Salesforce) needs to access the Web API (MS Graph API) as the signed-in user, but with access limited by the selected permission. This type of permission can be granted by a user unless the permission is configured as requiring administrator consent.

  • After adding permissions don’t forget to “Grant admin consent“. (See the bottom of the API Permissions page)

Let’s test our application.

Authorization

As of now, we noted three values 1. Application ID (Client Id) 2. Private Key (Client Secret) 3. Tenant ID. See the below code for authorization.  Store access_token in the public variable so that we can use it in other processes.

String clientId = 'my application id'; 
String clientSecret = 'my private key';
String tenantId = 'my tenant id';
Http h = new Http();
HttpRequest req = new HttpRequest();
req.setMethod('POST');
req.setHeader('Content-Type', 'application/x-www-form-urlencoded');
//https://login.microsoftonline.com/< tenant Id>/oauth2/token
req.setEndpoint('https://login.microsoftonline.com/'+EncodingUtil.urlEncode(tenantId, 'UTF-8').replaceAll('\\+', '%20')+'/oauth2/token'); 
String body = 'grant_type=client_credentials'+
    '&client_id=' + EncodingUtil.urlEncode(clientId, 'UTF-8') +
    '&client_secret='+ EncodingUtil.urlEncode(clientSecret, 'UTF-8') +
    '&resource=https://graph.microsoft.com';                              
req.setBody(body);
HttpResponse res = h.send(req);
System.debug('Response Body: ' + res.getBody());
//Parse the response and store the access_token in public variable to use it in other methods.

I added the sample response from Postman for the above request.

Authorization+Response

Note: Below example are to upload files in the Sharepoint host/root site. If you want  to upload files to other sites your URL format Should be: 

https://graph.microsoft.com/v1.0/sites/{site-id}/drives/    (get drive list)

https://graph.microsoft.com/v1.0/sites/{site-id}/drives/{drive id}/root/children      (create a folder)

https://graph.microsoft.com/v1.0/sites/{site-id}/drives/{drive id}/root:/{folder path}/{file name}:/content (upload a file)

Get the Drive List

Below code shows how to get a list of drives available to your application. In Sharepoint, every “Document Library” considered as a separate drive. If you know your drive (Document Library) ID then skip this step. From drive list find your drive with the help of “webUrl” or “name” in the response and store the “id” in a global variable like Custom Settings in salesforce. 

String access_token= 'access_token';
Http h = new Http();
HttpRequest req = new HttpRequest();
req.setMethod('GET');
req.setHeader('Authorization', 'Bearer '+ access_token);
req.setHeader('Content-Type', 'application/json');
req.setHeader('Accept', 'application/json');
req.setEndpoint('https://graph.microsoft.com/v1.0/drives/');
HttpResponse res = h.send(req);
System.debug('Response Body: ' + res.getBody());
//Get your drive id from the response and store it somewhere.

In my case, I want to store my files in Documents. 

Drive+List+Response

Create a Folder

Run the below code to create a folder in Root library.
URL format to create a child folder: https://graph.microsoft.com/v1.0/drives/{drive id}/root:/{parent folder path}:/children.
URL format to create the Folder with Parent Folder Id instead of folder path : https://graph.microsoft.com/v1.0/drives/{drive id}/items/{parent folder id}/children.
Also, @microsoft.graph.conflictBehavior have three values fail, replace, and rename.

String access_token= 'access_token';
String driveID = 'my drive id';
String folderBody = '{"name": "New Folder","folder": { },"@microsoft.graph.conflictBehavior": "rename"}';
string endPoint = 'https://graph.microsoft.com/v1.0/drives/' + 
                        EncodingUtil.urlEncode(driveID, 'UTF-8').replaceAll('\\+', '%20') +
                        '/root/children';
Http h = new Http();
HttpRequest req = new HttpRequest();
req.setMethod('POST');
req.setHeader('Authorization', 'Bearer '+ access_token);
req.setHeader('Content-Type', 'application/json');
req.setHeader('Accept', 'application/json');
req.setEndpoint(endPoint);
req.setBody(folderBody);
HttpResponse res = h.send(req);
System.debug('Response Body: ' + res.getBody());

Make sure that the folder created successfully in SharePoint.

New+Folder

Upload a File

Run the below code to upload the file in “New Folder” which we created previously. This method only supports files up to 4MB in size.
URL format to upload file with parent folder path: https://graph.microsoft.com/v1.0/drives/{drive Id}/root:/{parent folder path}/{file name}:/content.
URL format to upload file with parent folder id: https://graph.microsoft.com/v1.0/drives/{drive Id}/items/{parent folder id}:/{file name}:/content.

String access_token= 'access_token';
String driveID = 'my drive id';
String folderName = 'New Folder';
String fileBody = 'Hello World!';
String fileName = 'My Document.txt';
String endPoint = 'https://graph.microsoft.com/v1.0/drives/' + 
                                EncodingUtil.urlEncode(driveID, 'UTF-8').replaceAll('\\+', '%20') +
                                '/root:/'+EncodingUtil.urlEncode(folderName, 'UTF-8').replaceAll('\\+', '%20')+'/'+
                                EncodingUtil.urlEncode(fileName, 'UTF-8').replaceAll('\\+', '%20')+':/content';
Http h = new Http();
HttpRequest req = new HttpRequest();
req.setMethod('PUT');
req.setHeader('Authorization', 'Bearer '+ access_token);
req.setHeader('Content-Type', 'text/plain');
req.setHeader('Accept', 'application/json');
req.setEndpoint(endPoint);
req.setBodyAsBlob(Blob.valueOf(fileBody));
HttpResponse res = h.send(req);
System.debug('Response Body: ' + res.getBody());
File+Upload

Make sure that the file uploaded successfully in SharePoint.

Worried about upload large files? We have a Resumable File Upload feature.

Resumable Upload

Create an upload session to allow your app to upload files up to the maximum file size. An upload session allows your app to upload ranges of the file in sequential API requests, which allows the transfer to be resumed if a connection is dropped while the upload is in progress.

To upload a file using an upload session, there are two steps:
1.Create an upload session
2.Upload bytes to the upload session

1.Create an upload session
The response to this request will provide the details of the newly created uploadSession, which includes the URL used for uploading the parts of the file.

String access_token= 'access_token';
String driveID = 'my drive id';
String parentFolderId = 'My parent folder id';
String fileDesc = '{"@microsoft.graph.conflictBehavior": "rename"}';
String fileName = 'largeFile.txt';
string endPoint = 'https://graph.microsoft.com/v1.0/drives/' + 
                                EncodingUtil.urlEncode(driveID, 'UTF-8').replaceAll('\\+', '%20') +
                                '/items/'+EncodingUtil.urlEncode(parentFolderId, 'UTF-8').replaceAll('\\+', '%20')+':/'+
                                EncodingUtil.urlEncode(fileName, 'UTF-8').replaceAll('\\+', '%20')+':/createUploadSession';
Http h = new Http();
HttpRequest req = new HttpRequest();
req.setHeader('Authorization', 'Bearer '+ access_token);
req.setHeader('Content-Type','application/json');
req.setHeader('Accept', 'application/json');
req.setMethod('POST'); 
req.setEndpoint(endPoint);
req.setBody(fileDesc);
HttpResponse res = h.send(req);
System.debug('Response Body: ' + res.getBody());

Copy the uploadUrl from the response.

2.Upload bytes to the upload session

To upload the file or a portion of the file, your app makes a PUT request to the uploadUrl value received in the createUploadSession response. You can upload the entire file, or split the file into multiple byte ranges, as long as the maximum bytes in any given request is less than 60 MB.

The size of each chunk MUST be a multiple of 320 KB (327,680 bytes) except the last chunk. Using a fragment size that does not divide evenly by 320 KiB will result in errors committing some files.

Run the below code to upload the file in a single chunk (less than 60 MB file).  If you have large files then repeat the below request until you upload all the chunks with appropriate Content-Range.

String access_token= 'access_token';
Blob file_body = Blob.valueOf('Hello World!');
String fileLength = String.valueOf(file_body.size());
String uploadUrl = 'upload URL from create an upload session';
Http h = new Http();
HttpRequest req2 = new HttpRequest();
req2.setHeader('Authorization', 'Bearer '+ access_token);
req2.setHeader('Content-Length',fileLength);
req2.setHeader('Content-Range','bytes 0-'+(Integer.ValueOf(fileLength) - 1)+'/'+fileLength);
req2.setHeader('Accept', 'application/json');
req2.setMethod('PUT');        
req2.setEndpoint(uploadUrl);
req2.setBodyAsBlob(file_body);
HttpResponse res = h.send(req2);
System.debug('Response Body: ' + res.getBody());

If you have any other queries/issues drop me an email(rajesh@ibirdsservices.com).

Thanks,
Rajesh K

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.