Skip to Content

Overview:

Here we will see

  • Using SAP-PI UDF (Java), we consume SharePoint REST to write a File into SharePoint.
  • A business requirement, where employee’s pending item counts need to be updated in SharePoint in file format.
  • Employee’s Pending Count will be fetched from one system’s REST service and resulted string will be posted into a file format in SharePoint.
  • Fetching Employee’s Pending Count via respective REST service will be handled in separate UDF-1 and its result will be supplied to this SharePoint UDF-2.
  • Here we focus on java coding of “SharePoint UDF-2”
  • To access SharePoint, we need to fetch AccessToken first which can be get by following link help:

 

Java-Code-Logic:

  • Here we try to write a constant string into SharePoint in FileFormat
  • This constant string can be replaced by desired strings like employee’s pendingCount data as described in Overview section
  • AccessToken required for SharePoint authentication which can be found from above mentioned link.
/*
  url: http://<siteUrl>/_api/web/GetFolderByServerRelativeUrl('/<FolderName>')/Files/add(url='<fileName>',overwrite=true)
  method: POST
  body: "Contents of file"
  Headers: 
  Authorization  : "Bearer " + accessToken
  X-RequestDigest: form digest value
  content-length :length of post body				
*/
				
  String siteUrl = "https://<hostName>.sharepoint.com/teams/SPdev/AlertsCount"; 
  String wsUrl = siteUrl + "/_api/web/GetFolderByServerRelativeUrl('Shared%20Documents')/Files/add(url='File1.txt',overwrite=true)";				
  String request = "Create a File with raw string !!!";
				
  //Create HttpURLConnection
  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);						
				
  //Send Request
  DataOutputStream wr = new DataOutputStream(httpConn.getOutputStream ());				
  wr.writeBytes(request);
  wr.flush();
  wr.close();
				
  //Read the response.
  responseStr = "HTTP ResponseCode: " + httpConn.getResponseCode() + " "+  httpConn.getResponseMessage();	
  //System.out.println(wsUrl);
  //System.out.println(responseStr);

 

Testing Status in SharePoint:

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