Skip to Content

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

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 = "";

//AccessToken url
String wsURL = "<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");

//Prepare RequestData
String jsonParam =
"grant_type=client_credentials" +
"&client_id=<client-Id>@<tenant-Id>" +
"&client_secret=<client-secret>" +

//Send Request
DataOutputStream wr = new DataOutputStream(httpConn.getOutputStream ());

// 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