Monday 8 April 2019

Sharepoint Integration With Salesforce

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.
  • 1) Click App Registrations then 2) Click New application registration.
  • Follow the below steps.
          1) Enter the Name of your application. 
          2) Choose application type as "Web App/ API". 
          3) Enter Sign-on URL like "https://mycustomdomain.my.salesforce.com".
          4) Click "Create".



  • 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.

 
  • 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.

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. 


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.

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());

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

19 comments:

  1. I read over your blog, and i found it inquisitive, you may find My Blog interesting. So please Click Here To Read My Blog

    Salesforce Products
    Salesforce Consultant
    Salesforce Integration Service

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
  3. Nice blog information
    Sanjary Kids is one of the best play school and preschool in Hyderabad,India. The motto of the Sanjary kids is to provide good atmosphere to the kids.Sanjary kids provides programs like Play group,Nursery,Junior KG,Serior KG,and provides Teacher Training Program.We have the both indoor and outdoor activities for your children.We build a strong value foundation for your child on Psychology and Personality development.
    ­play school in hyderabad

    ReplyDelete
  4. Thank you for this brief explanation and very nice information. Well, got good knowledge. SharePoint Consulting Services

    ReplyDelete
  5. I have read your blog and I gathered some needful information from your blog. Keep update your blog. Awaiting for your next update. Thanks
    DedicatedHosting4u.com

    ReplyDelete
  6. This is my first time i visit here and I found so many interesting stuff in your blog especially it's discussion,Sharepoint consultants near me thank you.

    ReplyDelete
  7. Nice information of the blog

    Pressure Vessel Design Course is one of the courses offered by Sanjary Academy in Hyderabad. We have offer professional Engineering Course like Piping Design Course,QA / QC Course,document Controller course,pressure Vessel Design Course,Welding Inspector Course, Quality Management Course, #Safety officer course.
    Document Controller course
    Pressure Vessel Design Course
    Welding Inspector Course
    Safety officer course
    Quality Management Course
    Quality Management Course in India

    ReplyDelete
  8. Use "Long Path This very useful if you are having problems
    in deleting, unlocking, copying and even renaming files, also very easy.

    ReplyDelete
  9. Nice Blog, Thanks to share an amazing information with us, It helps me alot salesforce customer portal

    ReplyDelete
  10. Nice Blog, Thanks for sharing an amazing information for us, Keep Sharing salesforce customer portal

    ReplyDelete
  11. Good information for this blog
    We are the best piping design course in Hyderabad, India. Sanjary academy Offers Piping Design Course and Best Piping Design Training Institute in Hyderabad. Piping Design Institute in India Piping Design Engineering.
    Piping Design Course
    Piping Design Course in india
    Piping Design Course in hyderabad

    ReplyDelete
  12. Nice Information
    Sanjary kids is the best playschool, preschool in Hyderabad, India. Start your play school,preschool in Hyderabad with sanjary kids. Sanjary kids provides programs like Play group,Nursery,Junior KG,Serior KG,and Teacher Training Program.
    play school in hyderabad, India
    Preschool in hyderabad, India
    Preschool teacher training course in hyderabad, India
    pre and primary teacher training course in hyderabad,India
    early childhood teacher training course in hyderabad, India

    ReplyDelete
  13. I truly appreciate this post. I’ve been looking all over for this! Thank goodness I found it on Bing. You have made my day! Thanks again! Keep update more excellent posts..
    Document Management Software India
    Document Management Software Chennai
    Document Management Software

    ReplyDelete
  14. Thanks for your efforts in sharing this post with us. This was really awesome. kindly keep continuing the great work.
    SEO Service

    ReplyDelete
  15. The blog is very useful and informative which provides great information SD-WAN

    ReplyDelete
  16. Thanks for sharing this helpful & wonderful post. I really appreciate your hard work. this is very useful & informative for me.

    https://www.crmjetty.com/blog/dynamics-crm-sharepoint-integration/

    ReplyDelete
  17. Good Post! Thank you so much for sharing this pretty post, it was so good to read and useful to improve my knowledge as updated one, keep blogging…
    CRM Solutions

    ReplyDelete
  18. Hi,

    Could you please help me on the Upload Session Part. I have a requirement in which I need to upload large files from salesforce to SharePoint but I am getting the HTTP Callout Limit error.

    I have tried with your code but not able to divide it in chunks. Could you please send me the code at my email-arizkhansfdc@gmail.com for dividing the large files to chunks. It would be a great help.

    Thanks,
    Ariz

    ReplyDelete