Skip to Content

Here we will come to know, how SharePoint REST services can be consumed in SAP-PI using java UDFs.

  • In [Part-1], SharePoint authentication technique is been explained.
  • In [Part-2], One SharePoint REST service [GET Method] will be consumed using Java UDF (User Defined Function)
  • In [Part-3], One SharePoint REST service [POST Method] will be consumed using Java UDF (User Defined Function)

Now let’s start with SharePoint Authentication……..

Access tokens (i.e. security tokens) are required to authenticate SharePoint REST services.

To get access tokens, below details are required from SharePoint server, which can be found with help of SharePoint Admin:

  1. <client-ID>
  2. <client-Secret>
  3. <tenant-Id>
  4. <client-Id> should have Read/Write permissions

Below Java UDF code will help to get access token of SharePoint site:

//=================================================================

public String getAccessToken(Container container) 
throws StreamTransformationException{

String accessToken = "";
try{

//AccessToken url
String wsURL = "https://accounts.accesscontrol.windows.net/<tenant-Id>/tokens/OAuth/2";

URL url = new URL(wsURL);
URLConnection connection = url.openConnection();
HttpURLConnection httpConn = (HttpURLConnection) connection;

//Set header
httpConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
httpConn.setDoOutput(true);
httpConn.setDoInput(true);
httpConn.setRequestMethod("POST");

//Prepare RequestData
String jsonParam =
"grant_type=client_credentials" +
"&client_id=<client-Id>@<tenant-Id>" +
"&client_secret=<client-secret>" +
"&resource=00000003-0000-0ff1-ce00-000000000000/<client-Domain>.sharepoint.com@<tenant-Id>";

//Send Request
DataOutputStream wr = new DataOutputStream(httpConn.getOutputStream ());
wr.writeBytes(jsonParam);
wr.flush();
wr.close();

// Read the response.
InputStreamReader isr = null;
if (httpConn.getResponseCode() == 200) {
isr = new InputStreamReader(httpConn.getInputStream());
} else {
isr = new InputStreamReader(httpConn.getErrorStream());
}

BufferedReader in = new BufferedReader(isr);
String responseString = "";
String outputString = "";

// Write response to a String.
while ((responseString = in.readLine()) != null) {
outputString = outputString + responseString;
}

//Extracting accessToken from string, here response (outputString) is a Json format string
if(outputString.indexOf("access_token\":\"") > -1){
int i1 = outputString.indexOf("access_token\":\"");
String str1 = outputString.substring(i1 + 15);
int i2 = str1.indexOf("\"}");
String str2 = str1.substring(0, i2);
accessToken = str2;
}

}catch (Exception e) { e.printStackTrace(); }
return accessToken;
}

 

//=================================================================

To report this post you need to login first.

1 Comment

You must be Logged on to comment or reply to a post.

Leave a Reply