Insert items in SharePoint using SAP-PI UDF
Overview:
Here, we will see, that, how SharePoint’s AlertCount list can be updated from SAP-PI UDF.
To update AlertCount list inside SharePoint, following SharePoint-REST protocols can be used:
- URL: https://<org_Shp_Host>/teams/SPdev/AlertsCount/_api/web/Lists/GetByTitle(‘AlertCount’)/Items
- Method: POST
- Headers:
- Authorization : “Bearer ” + <SharePointAccessToken>
accept : application/json;odata=verbose
Content-Type : “application/json;odata=verbose”
- Authorization : “Bearer ” + <SharePointAccessToken>
- Body: (Request Parameter format)
- {__metadata: { ‘type’: ‘SP.Data.AlertCountListItem’ },
Col1 : ”,
Col2 : ”
}
- {__metadata: { ‘type’: ‘SP.Data.AlertCountListItem’ },
- where,
- <org_Shp_Host> is Organisation’s SharePoint host name
- Col1 is 1st column of AlertCountList
- Col2 is 2nd column of AlertCountList
- <SharePointAccessToken>: it is security token required to authenticate SharePoint REST request while inserting items, this token can be fetched from following blog’s udf:
SAP PI Java UDF example to insert AlertCount list items in SharePoint:
public String writeAlertCount(String accessToken, String InputCol1, String InputCol2, Container container) throws StreamTransformationException{
/**
* Using this function, we can update AlertCountList items of SharePoint.
* Suppose, if we want to insert data into two columnItems ('Col1' & 'Col2') of AlertCountList in SharePoint, then following SharePoint-REST will be consumed inside this UDF
* REST URL: https://<org_Shp_Host>/teams/SPdev/AlertsCount/_api/web/Lists/GetByTitle('AlertCount')/Items
* Request Format:
* {__metadata: { 'type': 'SP.Data.AlertCountListItem' }, Col1: 'E01', Col1: '1562' } *
* Where,
* <org_Shp_Host> is Organisation's SharePoint host name
*/
String response = "";
try {
//SharePoint REST url to update AlertCount Item List
String wsURL = "https://<org_Shp_Host>/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");
httpConn.setRequestProperty("Authorization", "Bearer " + accessToken);
httpConn.setRequestProperty("accept","application/json;odata=verbose");
httpConn.setRequestProperty("content-Type", "application/json;odata=verbose");
// Set Request parameters
String requestP = "{__metadata: { 'type': 'SP.Data.AlertCountListItem' }, "
+ "Col1: '" + InputCol1 +"', "
+ "Col2: '" + InputCol2+"' }";
// Send Request
DataOutputStream wr = new DataOutputStream(httpConn.getOutputStream());
wr.writeBytes(requestP);
wr.flush();
wr.close();
//Read the response
if (httpConn.getResponseCode() == 200) {
response = "Http Response Code: " + httpConn.getResponseCode();
}else if (httpConn.getResponseCode() == 201) {
// when HttpResponseCode is 201, this insert is successful in SharePoint.
response = "Http Response Code: " + httpConn.getResponseCode() + "\n Item inserted successfully in sharepoint";
}else{
response = "Error with Http Response Code: " + httpConn.getResponseCode();
}
} catch (Exception e) {
response = "Error: " + e.getMessage();
}
return response;
}
Testing:
- UDF Run inside Graphical Map:
- Results in SharePoint
<<<<< Parent blog reference….. Integrate SharePoint using SAP PI
Be the first to leave a comment
You must be Logged on to comment or reply to a post.