[SAP-CPI] – Integrated with SHAREPOINT BY SAP CPI

Hi guys, in this article I want to share step by step how to integrated with SHAREPOINT from SAP CPI. Before go to explore how to design iFlow for this, we will take a look about SHAREPOINT and REST API of it. To practice this article, we need one account office 365 with SHAREPOINT in there.

Working with SHAREPOINT and SHAREPOINT REST API

To call REST API of SHAREPOINT we have to go 4 steps:

  • Register Add-In ( Client ID, Client Secret)
  • Grant Permissions to Add-In ( Grant permission for Client ID)
  • Generate the Access Token
  • Access the SharePoint resource

Step 01 : Create Client ID and Client Secret

  • After done, we will have Client ID, Client Secret. Next, we will grant permission for this Client ID.

Step 02 : Grant permission for Client ID

SHAREPOINT have 4 scope URI:

SHAREPOINT have 4 authorize

  • Read
  • Write
  • Manage
  • Full Control

We can grant permission at level tenant or level site.

  • (1) : Input Client ID
  • (2): Click button Lookup. SHAREPOINT will fill information of this Client ID
  • (3) : Input permission XML with syntax
  • (1) : Scope
  • (2) : Right (Read, Write, Manage, Full Control)

Step 03 : Get access token

Step 3.1 Get TENANT ID, Resource

Use POSTMAN, call one request with method GET to endpoint : https://<domain-name>/sites/<site-name>/_vti_bin/client.svc/

At header of response, take a look in field

  • WWW-Authenticate
    • Bearer realm : Value of this field is Tenant ID
    • client_id : Value of this field is resource

Step 3.2 Get Access Token

To get access token, we need informations

  • grant_type : client_credentials
  • client_id : ClientID@TenantID (Client ID get in step 01, TenantID get in Step 3.1)
  • client_secret : Client Secret in step 01
  • resource : resource/<domain-name>@TenantID
  • Endpoint : https://accounts.accesscontrol.windows.net/<TenantID>/tokens/OAuth/2
  • Method : POST

Step 04 : Access resource of SHAREPOINT

All operation on SHAREPOINT, we will call endpoint at level SITE. This is endpoint at level SITE

https://<domain-name>/sites/<site-name>/_api/web/<xxxxxxx&gt;

NOTES : After config Client ID, Client Secret and grant permission. When We call resource We receiver error: Authorize – Token. To fix this Issue, We need run PowerShell

4.1 Create folder in documents

HOST: htts://<domain-name>

RESOURCE : /sites/<site-name>/_api/web/folders

METHOD : POST

PAYLOAD :

  • Key : ServerRelativeUrl
  • Value : Shared Documents/<Folder-Name>

Content-type : JSON

ACCEPT : application/json;odata=verbose

4.2 Get all folder in Shared Documents

HOST: htts://<domain-name>

RESOURCE : /sites/<site-name>/_api/web/GetFolderByServerRelativeUrl(‘/sites/<site-name>/Shared Documents’)/Folders

METHOD : GET

ACCEPT : application/json;odata=verbose

4.3 DELETE folder

HOST: htts://<domain-name>

RESOURCE : /sites/<site-name>/_api/web/GetFolderByServerRelativeUrl(‘/sites/<site-name>/Shared Documents/<folder-name>’)

METHOD : DELETE

4.4 Get all files in folder

HOST: htts://<domain-name>

RESOURCE : /sites/<site-name>/_api/web/GetFolderByServerRelativeUrl(‘/sites/<site-name>/Shared Documents/<folder-name>’)/Files

METHOD : GET

ACCEPT : application/json;odata=verbose

4.5 Get file with file name

HOST: htts://<domain-name>

RESOURCE : /sites/<site-name>/_api/web/GetFolderByServerRelativeUrl(‘/sites/<site-name>/Shared Documents/<folder-name>’)/Files(‘<file-name-and-extension>’)/$value

METHOD : GET

ACCEPT : application/json;odata=verbose

4.6 Upload File to Folder

HOST: htts://<domain-name>

RESOURCE : /sites/<site-name>/_api/web/GetFolderByServerRelativeUrl(‘/sites/<site-name>/Shared Documents/<folder-name>’)/files/Add(url='<file-name>’,overwrite=<true/false>)

METHOD : POST

ACCEPT : application/json;odata=verbose

INTEGRATION WITH SHAREPOINT BY SAP CPI

OK, we explored how to get access token and take some request REST API of SHAREPOINT in POSTMAN tool. In this section, we will use SAP CPI to call REST APIs of SHAREPOINT. This scenario used for in cases we want upload file from SAP backend by ABAP code to SHAREPOINT. This scenario, we have to do:

  • SAP backend will send file to SFTP
  • SAP CPI collect file from SFTP and send it to SHAREPOINT.

OK, for simple in this article, I will copy/paste file XML into SFTP server and check on SHAREPOINT. In fact, file will sent by ABAP Proxy from backend.

Further more, in integration flow I will hardcode value of access token. In fact, You will call to REST API of SHAREPOINT, as step by step in section above, to get access token.

  • Text to Array Byte : This is a groovy Script to transfer data in body to steam by with code
  • Header & Access Token : This is a content modifier, hard code access token
  • Request Reply : Call REST API upload file to SHAREPOINT
  • Address : REST API upload file to SHAREPOINT. We get file name by syntax : ${file.onlyname}

TEST CASE

01. Wrong Access Token

02. Access token is expired

03. Happy case

SUMMARY

In this article, I shared step by step how to integration with SHAREPOINT through SAP CPI. I also shared how to working with REST API of SHAREPOINT. More details for REST API of SHAREPOINT, kindly visit link : https://docs.microsoft.com/en-us/sharepoint/dev/sp-add-ins/working-with-folders-and-files-with-rest

Thanks for your reading and any advise, kindly leave your comment below.

Thanks.

JOSEPH.

4 comments

  1. This is really helpful blog!
    I’m trying the same thing as you did. At the mean time, I’m also trying to get the access token via CPI instead of Postman. However, I did not get the token successfully and wondering if you have any experience on it?

    Like

  2. I am following the same steps, stuck at powershell script – could you please help with the same.

    What is this powershell script used for?

    Like

  3. Hi Joseph, I’m trying to upload the excel file to SharePoint point. I have given file type as XLS and file creating. But, when I open the file it’s contains binary string(like how data looks if we open excel file in notepad or notepad++) not an actual exel data. Can you help on how to handle this case?

    Regards,
    Ven

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

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