11

SharePoint Integration using SAP CPI (Without SAP Open Connector)

 3 years ago
source link: https://blogs.sap.com/2021/04/15/sharepoint-integration-using-sap-cpi-without-sap-open-connector/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Introduction

Sometimes documents need to be copied to the SharePoint team site so that it can be made accessible to broader recipients within an organization.

This integration requires configuration changes in SharePoint as well as SAP CPI.

SharePoint App Configuration

Register Add-In

On the initial stage, we have to register the Add-In in SharePoint, where we want to access the information. Follow the steps below to register the Add-In in SharePoint site.

  • Navigate and login to SharePoint online site.
  • Then navigate to the Register Add-In page by entering the url as
  • https://<siteURL>/_layouts/15/appregnew.aspx
  • On the App Information section, click the Generate button next to the Client Id and Client Secret textboxes to generate the respective values.
  • Enter Add-In Title in Title textbox
  • Enter AppDomian as a localhost
  • Enter RedirectUri as a https://localhost
  • Click Create button, which registers the add-in and returns the success message with created information.

Register%20SharePoint%20Add-ins

Register SharePoint Add-ins

Grant Permissions to Add-In

Once the Add-In is registered, we have to set the permissions for that add-in to access the SharePoint data. We will set the Read permission level to the web scope, so that we will be able to read the web information.

  • Navigate to the SharePoint site
  • Then enter the URL https://<siteURL>/_layouts/15/appinv.aspx in the browser. This will redirect to the Grant permission page.
  • Enter the Client ID(which we have generated earlier), in the AppId textbox and click the Lookup button. That will populate the value to other textboxes in Title, App Domain and Redirect Url.

Grant%20Permission%20to%20Add-In

Grant Permissions to Add-In

  • Now enter the below permission request in XML format.
<AppPermissionRequests AllowAppOnlyPolicy="true">
    <AppPermissionRequest Scope="http://sharepoint/content/sitecollection/web" Right="Write" />
</AppPermissionRequests>
  • Then click the Create button. This will redirect to your page, where we have to trust the add-in to read items from the website.

Note: If we want to access site collection or tenant level, we have add the xml accordingly.

Retrieve the Tenant ID

Once we register the Client Id and Secret with the permissions, we are ready to access the SharePoint information from external systems or tools.

First, we have to know the Tenant ID. Follow the below steps to obtain that information from the postman. Postman helps to get the tenant Id by requesting the below url with Authorization header.

  • Launch Postman.
  • Select Get Method
  • Enter the below URL in the “Request URL” textbox
  • https://<siteURL>/_vti_bin/client.svc/
  • Configure the below information in the header section to send along with the url requestMethod = Get

Get%20Call%20for%20Tenant%20ID

Get Call for Tenant ID

  • After applying the configuration, click the Send button. The response returns a lot of headers but ends with unauthorized access.

Response%20Headers

Response Headers

In response header, we will get WWW-Authenticate as one of the headers and that contains the necessary information required for the next step. The realm value contains the tenant id for the SharePoint Online site and clientid value contains the resource information (we’ll use it later in our SAP CPI iFlow).

SAP CPI iFlow Configuration

iFlow

iFlow

This iFlow is creating one webservice which can be triggered by any HTTP Client and upload files to SharePoint using REST api V1 of SharePoint.

Here we have used parallel multicast to get the OAuth access token from Microsoft Access Control Services.

OAuth Call Branch

OAuth Request

First of all we are using a content modifier step to create the request. 

  • Request Header: We have added one header with key and value pair as “Content-Type” and “application/x-www-form-urlencoded”.
  • Request Body: We have to add the below string to the message body section with appropriate replacement.
grant_type=client_credentials&client_id=ClientID@TenantID&client_secret=ClientSecret&resource=resource/SiteDomain@TenantID
  • ClientID: Created in the Add In section
  • ClientSecret: Created in the Add In section
  • TenantID: Fetch from “WWW-Authenticate” header in the last section
  • resource: Fetch from “WWW-Authenticate” header in the last section
  • SiteDomain: Domain name

Call to MACS and HTTP Receiver

Then we have a request reply step which will call the Microsoft Access Control Services and fetch the response. HTTP receiver channel should be updated with the below address and “POST” Method.

Address: https://accounts.accesscontrol.windows.net/<TenantID>/tokens/OAuth/2

JSON to XML Converter and Fetch Access Token

As we are getting one JSON response we need to convert it to XML format and then we have used another content modifier to fetch the access token and assign it to a property called “access_token”.

Access_Token

Access_Token

Remove Body

We have used one simple Groovy script to remove the body of the OAuth call branch.

import com.sap.gateway.ip.core.customdev.util.Message
import groovy.json.JsonOutput

Message processData(Message message) {
    def body = "";
    message.setBody(body) 
    return message;
}

Final Call to SharePoint

Join and Gather

We have used Join and Gather to merge both the branches. So that we can get an OAuth access token as well as the original message body from the request.

Set Header

We have used one content modifier to create 2 headers with a key value pair as below. We have also deleted the unused headers.

Key :: Value

Content-Type :: application/xml (You can change the value as per your requirement)

Authorization :: Bearer ${property.access_token}

Headers

Headers

We have also created one exchange property to transmit the filename.

Property

Property

Convert Body to outputStream

As per our understanding the request body should be passed to the HTTP adapter but as per our testing the content modifier or ${body} is not giving required results when the request body is passed directly (SharePoint REST API is giving 500 error with I/O error). So we have used one simple groovy script to convert the payload to ByteArrayOutputStream and again set it to the message body. (Not sure why it is behaving like this as we are still investigating this issue.)

import com.sap.gateway.ip.core.customdev.util.Message;
import java.util.HashMap;
def Message processData(Message message) {
    //Body 
       def body = message.getBody(String.class);
       ByteArrayOutputStream outputStream = new ByteArrayOutputStream()
       outputStream.write(body.getBytes())
       message.setBody(outputStream.toString())
       return message;
}

Call to SharePoint

We are making a POST call to SharePoint REST api using request reply and HTTP receiver channel.

HTTP%20Receiver

HTTP Receiver

Test 

We have called the HTTP endpoint of the iFlow from a Postman client.

Call%20from%20Postman

Call from Postman

Result

We can see the files being sent to the SharePoint.

Result

Result

Conclusion

With this you have successfully created and tested an integration flow that is uploading documents  to SharePoint, created a team site in SharePoint, leveraged the SAP CPI to connect seamlessly to SharePoint site. Basically we need to take care of the HTTP request payload while calling the rest api of SharePoint. Please go through the Microsoft documentation for API overview.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK