Skip to Content

Now let’s start with consumption of SharePoint REST service using POST Method

Here we need input as SharePoint accessToken String which can be found from previous blog “SAP-PI UDF: [Part-1] Consume SharePoint REST- Authentication

In blow Java UDF example, SharePoint AlertCount Item list gets updated by consuming respective SharePoint REST service.

//—————————————————————————————

public String callSharepoint_POST(String accessToken, Container container) 
throws StreamTransformationException{

String outputString = "";
try{

//SharePoint REST url to update AlertCount Item List
String wsURL = "https://<client-Domain>.sharepoint.com/teams/SPdev/AlertsCount/_api/web/Lists/GetByTitle('AlertCount')/Items";

//Create Http Connection
URL url = new URL(wsURL);
URLConnection connection = url.openConnection();
HttpURLConnection httpConn = (HttpURLConnection) connection;

//Set Header
httpConn.setDoOutput(true);
httpConn.setDoInput(true);
httpConn.setRequestMethod("POST");

//get accessToken from UDF-1 blog "Call SharePoint REST Service in SAP-PI: [Part-1] UDF to Get Access Token"
String basicAuth = "Bearer " + accessToken;
httpConn.setRequestProperty("Authorization", basicAuth);

httpConn.setRequestProperty("accept", "application/json;odata=verbose");
httpConn.setRequestProperty("X-RequestDigest", "$(\"#__REQUESTDIGEST\").val()");
httpConn.setRequestProperty("content-Type", "application/json;odata=verbose");


//Prepare RequestData
String jsonParam = "{__metadata: { 'type': 'SP.Data.AlertCountListItem' }, Title: 'Title_01', SAPDATA: 'SAPData_01' }";

//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) {
  outputString = "Http Response Code: "  + httpConn.getResponseCode();
} else if (httpConn.getResponseCode() == 201){
  //Here when HttpResponseCode is 201, this insures successful posting of data on SharePoint site
  outputString = "Http Response Code: "  + httpConn.getResponseCode() + "\n Item gets created on sharepoint";
}else{
 outputString = "Error with Http Response Code: "  + httpConn.getResponseCode() ;
}

}catch (Exception e) {
outputString = "Error: "  + e.getMessage() ;
}

return outputString;
}

//—————————————————————————————

To report this post you need to login first.

Be the first to leave a comment

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

Leave a Reply