Skip to Content
Author's profile photo Dilip Kumar Krishnadev Pandey

Create, Read file in SharePoint using Java (SAP PI UDF)

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:

 

JavaCode to Create a file in SharePoint:

  • 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.
/*
  Sharepoint REST details to create a file:
  url: http://<siteUrl>/_api/web/GetFolderByServerRelativeUrl('/<FolderName>')/Files/add(url='<fileName>',overwrite=true)
  method: POST
  headers:
      Authorization: "Bearer " + accessToken			
  body: "Contents of file"
*/

  //Frame Sharepoint REST URL to create a File
  String siteURL = "https://<host>/<siteURL_path>";	//here <siteURL_path> is '/teams/SPdev/AlertsCount'	
  String wsUrl = siteUrl + "/_api/web/GetFolderByServerRelativeUrl('Shared%20Documents')/Files/add(url='File1.txt',overwrite=true)";	

  //Data to be written in File			
  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.
  String responseStr = "";
  if (httpConn.getResponseCode() == 200) {
    responseStr = "File has been written successfully. ResponseCode: "+ httpConn.getResponseCode();
  }else{
    responseStr += "Error while writing file, ResponseCode: " + httpConn.getResponseCode() + " " + httpConn.getResponseMessage();
  }	
  
  //System.out.println(httpResponseStr);	//Print response
	

 

Testing Status in SharePoint:

 

 

Blog updated on 24-Jul2018 with following contents:

Lets see how we can read files present in some specific folder in SharePoint.

Here, in following examples, we will read files from default folder ‘Documents’ which while accessing referred as ‘Shared Documents’.

[1] JavaCode to retrieve all files of a folder from SharePoint:

/*
Sharepoint REST details to retrieve all of the files in a folder
url: http://site url/_api/web/GetFolderByServerRelativeUrl('/Folder Name')/Files
method: GET
headers:
    Authorization: "Bearer " + accessToken
    accept: "application/json;odata=verbose" or "application/atom+xml"
*/
try {		
   //Frame SharePoint siteURL			
   String siteURL = "https://<host>/<siteURL_path>";
		
   //Frame SharePoint URL to retrieve all of the files in a folder
   String wsUrl = siteURL + "/_api/web/GetFolderByServerRelativeUrl('Shared%20Documents')/Files";
	
   //Create HttpURLConnection
   URL url = new URL(wsUrl);
   URLConnection connection = url.openConnection();
   HttpURLConnection httpConn = (HttpURLConnection) connection;
						
   //Set Header		
   httpConn.setRequestMethod("GET");				
   httpConn.setRequestProperty("Authorization", "Bearer " + accessToken);
   httpConn.setRequestProperty("accept", "application/json;odata=verbose"); //To get response in JSON
   //httpConn.setRequestProperty("accept", "application/atom+xml");         //To get response in XML
   
   //Read the response
   String httpResponseStr = "";
   InputStreamReader isr = null;
   if (httpConn.getResponseCode() == 200) {
     isr = new InputStreamReader(httpConn.getInputStream());
   } else {
     isr = new InputStreamReader(httpConn.getErrorStream());
   }		
   BufferedReader in = new BufferedReader(isr); 	 		
   String strLine = "";
   while ((strLine = in.readLine()) != null) {
     httpResponseStr = httpResponseStr + strLine;
   }
   //System.out.println(httpResponseStr);	//Print response
} catch (Exception e) {
   //System.out.println("Error while reading file: " + e.getMessage());
}		

 

[2] JavaCode to retrieve a specific file from SharePoint:

/*
Sharepoint REST details to retrieve a specific file
url: http://site url/_api/web/GetFolderByServerRelativeUrl('/Folder Name')/Files('file name')/$value
method: GET
headers:
    Authorization: "Bearer " + accessToken
*/

try {		
   //Frame SharePoint siteURL			
   String siteURL = "https://<host>/<path>";
		
   //Frame SharePoint URL to retrieve all of the files in a folder
   String wsUrl = siteURL + "/_api/web/GetFolderByServerRelativeUrl('Shared%20Documents')/Files('XYZ.txt')/$value";
	
   //Create HttpURLConnection
   URL url = new URL(wsUrl);
   URLConnection connection = url.openConnection();
   HttpURLConnection httpConn = (HttpURLConnection) connection;
						
   //Set Header		
   httpConn.setRequestMethod("GET");				
   httpConn.setRequestProperty("Authorization", "Bearer " + accessToken);
  
   //Read the response
   String httpResponseStr = "";
   InputStreamReader isr = null;
   if (httpConn.getResponseCode() == 200) {
     isr = new InputStreamReader(httpConn.getInputStream());
   } else {
     isr = new InputStreamReader(httpConn.getErrorStream());
   }		
   BufferedReader in = new BufferedReader(isr); 	 		
   String strLine = "";
   while ((strLine = in.readLine()) != null) {
     httpResponseStr = httpResponseStr + strLine;
   }
   //System.out.println(httpResponseStr);	//Print response
} catch (Exception e) {
   //System.out.println("Error while reading file: " + e.getMessage());
}		

 

[3] JavaCode to retrieve a specific file from SharePoint when you know file’s URL:

/*
Sharepoint REST details to retrieve a file with its URL
url: http://site url/_api/web/GetFileByServerRelativeUrl('/Folder Name/file name')/$value
method: GET
headers:
    Authorization: "Bearer " + accessToken
*/

try {		
   //Frame SharePoint siteURL			
   String siteURL = "https://<host>/<siteURL_path>";
		
   //Frame SharePoint URL to retrieve all of the files in a folder
   //URL-Type-1
   String wsUrl = siteURL + "/_api/web/GetFileByServerRelativeUrl('/<siteURL_path>/Shared%20Documents/XYZ.txt')/$value";

   //URL-Type-2
   //String wsUrl = siteURL + "/_api/Web/GetFileByServerRelativePath(decodedurl='/<siteURL_path>/Shared%20Documents/XYZ.txt')/$value";
	
   //Create HttpURLConnection
   URL url = new URL(wsUrl);
   URLConnection connection = url.openConnection();
   HttpURLConnection httpConn = (HttpURLConnection) connection;
						
   //Set Header		
   httpConn.setRequestMethod("GET");				
   httpConn.setRequestProperty("Authorization", "Bearer " + accessToken);
  
   //Read the response
   String httpResponseStr = "";
   InputStreamReader isr = null;
   if (httpConn.getResponseCode() == 200) {
     isr = new InputStreamReader(httpConn.getInputStream());
   } else {
     isr = new InputStreamReader(httpConn.getErrorStream());
   }		
   BufferedReader in = new BufferedReader(isr); 	 		
   String strLine = "";
   while ((strLine = in.readLine()) != null) {
     httpResponseStr = httpResponseStr + strLine;
   }
   //System.out.println(httpResponseStr);	//Print response
} catch (Exception e) {
   //System.out.println("Error while reading file: " + e.getMessage());
}		

 


<<<<< Parent blog reference…..  Integrate SharePoint using SAP PI

Assigned tags

      44 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Former Member
      Former Member

      Hello Dilip,

       

      does this work in  very old version  XI?

       

      Regards

       

      Ely.

      Author's profile photo Dilip Kumar KrishnaDev Pandey
      Dilip Kumar KrishnaDev Pandey
      Blog Post Author

      Dear Former Member

      Sorry for delayed response, I was too much busy in my current project....

      This is working in PI 7.1 and PO 7.5.

      But I've not tried this in XI version, it should work because this is UDF logic to call REST service with POST method, jre1.4 too is supported with this piece of code.

       

      Thanks & Regards

      Dilip

       

      Author's profile photo sivakrishna sathu
      sivakrishna sathu

      Hi Dilip,

      Thanks for your wonderful blog. Now, I have the similar requirement, pull the .xml file from SFTP and place as it is in SharePoint folder.

      We are in PO 7.5 and we have REST adapter. Can we handle this without writing any Java code/udf?

      Or should we write Java code to fetch token I'd first and then place the file along with that token?

      Appericiate if you could help me on this please.

      Thanks,

      Siva.

      Author's profile photo Antony FERMINUS
      Antony FERMINUS

      Hi Dilip,

      Thanks for the blog.

      I get error HTTP ResponseCode: 404 Not Found.

      I have the token. The url is also correct. I use PO 7.5.

      Is it a kind of permission issue to upload the file?

       

      regards,

      Antony.

      Author's profile photo Antony FERMINUS
      Antony FERMINUS

      It is ok.

      Some kind on issue with the url.

      Thanks.

       

      Regards,

      Antony.

      Author's profile photo vinay mittal
      vinay mittal

      Hi Guys ,

       

      I am stuck!, First of all thanks Dilip for such a wonderful blog. I am able to get the access token  by the client id/secret etc.

      I am calling this API 

      https://xxxx.sharepoint.com/_api/web/GetFileByServerRelativeUrl('/sites/hub/dept/COTraining/Wisam/Shared%20Documents/BushfireAreas.txt')/$value to read the file but it says File doesnt exist!

       

      am I doing some mistake in the url creation? Authentication works fine as i was getting a 200 OK on this dummy API request

      https://<sitename>.sharepoint.com/_api/web?$select=Title

       

      Do i need to add some special permissions to the target sharepoint team site?

       

       

      Author's profile photo Dilip Kumar KrishnaDeo Pandey
      Dilip Kumar KrishnaDeo Pandey
      Blog Post Author

      Hi,

      • Please cross verify if folder 'Documents' has same file in SharePoint.
      • As per your error, seems to be file not present at same location.
      • And no extra permission required, as your code is able to view the Folder.
      • And I am updating this blog too with File Read info for others benefit.......

       

      Thanks & Regards,

      Dilip

      Author's profile photo anurag kumar
      anurag kumar

      Hi Dilip,

      1st of all thanks a lot !! for such a wonderful post.

      But I am getting error as -  Access denied. You do not have permission to perform this action or access this resource

      when I am trying to download a file .
      I am able passing the generated access token correctly(using client Id + secret ) . still I am getting access denied every time.

      -------------------
      but If I am trying to directly hit this url in browser (Google chrome)-

      siteURL+/_api/Web/GetFileByServerRelativePath(decodedurl='/<siteURL_path>/Shared%20Documents/XYZ.txt')/$value"

      file is getting downloaded . as browser has my share point username and password.
      But in your approach where we can pass our share point credential.
       

      Author's profile photo Dilip Kumar KrishnaDev Pandey
      Dilip Kumar KrishnaDev Pandey
      Blog Post Author

      Dear Anurag,

      You should ask your SharePoint team to provide "Write" permission to your SharePoint client-id/app-id.

      For more info of this type of permission issue please have a look in below blog link:

       

      Thanks & Regards,

      Dilip

       

      Author's profile photo anurag kumar
      anurag kumar

      Thanks Dilip After getting Access it works like Charm .Thanks a lot !

      Author's profile photo anurag kumar
      anurag kumar

      Hi Dilip,

      If I trying upload using above post . I am getting response 200 (success) but I am not able to see the files in sharePoint .

      AS official doc says - https://docs.microsoft.com/en-us/sharepoint/dev/sp-add-ins/upload-a-file-by-using-the-rest-api-and-jquery

      do we need to use  jquery here ?
      or After calling upload method in java suggested by you in blog do we have to update File Metadata?
      do we have any way to set up-loader name (user name) or file id to share point ?

       

      Author's profile photo Dilip Kumar KrishnaDev Pandey
      Dilip Kumar KrishnaDev Pandey
      Blog Post Author

      Hi Anurag,

      If you are getting Response code 200 (success), you should see the files in SharePoint, please check correct directory which was given in URL.

      About other queries, please find following comments:

      1. No mandatory concept of using JQuery only, SharePoint REST-Services can be called from any Platform/Language which supports REST-API calls.
      2. No need to update any File Metadata.
      3. And about set up-loader name (user name) or file id to SharePoint
        • This you have to discuss with your SharePoint team.
        • Max you can provide your userName or FileID in FileNames only, next how SharePoint utilizes how it reads, its all dependent on SharePoint’s custom logic.
        • or if any std. mechanism is present, that you have to follow.
        • In my info, when file gets created in to SharePoint, SharePoint too assigns unique FileID to each FileName.

      Parallel if you are interested in sending 'pdf' files too in SharePoint, you should have a look on below blog:

       

      Thanks & Regards,

      Dilip

      Author's profile photo anurag kumar
      anurag kumar

      I am checking it in correct directory only
      even If i am trying to list all the files in the directory where i uploaded the file ( named asFile1.txt same as above example) by a rest get  query -

      siteURL/_api/web/GetFolderByServerRelativeUrl('folder name')/files	

      in response i am able to see the uploaded file
      but there in response JSON two properties are null or empty -  "LinkingUri": null, && "LinkingUrl": ""
      I suspect this may be a reason why my uploaded file is not visible in share point website . because other file what I see in share point those have "LinkingUri": 'correct_url', && "LinkingUrl": "correct_url"   they are not empty .

      "CheckInComment": "",
      "CheckOutType": 0,
      "ContentTag": "{#someText},1,1",
      "CustomizedPageStatus": 0,
      "ETag": "\"{#someText},1\"",
      "Exists": true,
      "IrmEnabled": false,
      "Length": "42",
      "Level": 255,
      "LinkingUri": null,
      "LinkingUrl": "",
      "MajorVersion": 0,
      "MinorVersion": 1,
      "Name": "File1.txt",
      "ServerRelativeUrl": "/abc/xyz/folder name/File1.txt",
      "TimeCreated": "2018-11-01T08:19:17Z",
      "TimeLastModified": "2018-11-01T08:19:17Z",
      "Title": null,
      "UIVersion": 1,
      "UIVersionLabel": "0.1",
      "UniqueId": "#someText"

      do we have to post these two field properties : -

      "LinkingUri" && "LinkingUrl"

      from java code  in out post request?

      Author's profile photo Dilip Kumar KrishnaDev Pandey
      Dilip Kumar KrishnaDev Pandey
      Blog Post Author

      Hi Anurag,

      No, you don’t have to provide “LinkingUri” && “LinkingUrl” in REST-File-POST code, only File content (String) and respective REST-URL required.

      Can you provide your code here, which you are trying for File POST in SharePoint ?

       

      Thanks & Regard,

      Dilip

      Author's profile photo anurag kumar
      anurag kumar

      it's exactly same as code posted by you...

      	public static void upload(String source){
      		//here I am passing source as 
      		//https://myOrganizationUrl/sites/abc/_api/web/GetFolderByServerRelativeUrl('xyz')/Files/add(url='testFile.txt',overwrite=true)
      		//while calling this method
      		URL url = new URL(source);
      		URLConnection connection = url.openConnection();
      		HttpURLConnection httpConn = (HttpURLConnection) connection;
      
      		// Set Header
      		httpConn.setDoOutput(true);
      		httpConn.setDoInput(true);
      		httpConn.setRequestMethod("POST");
      		String accessToken = getSharePointAccessToken();// method to get token
      		httpConn.setRequestProperty("Authorization", "Bearer " + accessToken);
      
      		// Send Request
      		DataOutputStream wr = new DataOutputStream(httpConn.getOutputStream());
      		// Data to be written in File
      		String testString = "Create a File with raw string !!! Test";
      		wr.writeBytes(testString);
      		wr.flush();
      		wr.close();
      
      		// Read the response.
      		String responseStr = "";
      		if (httpConn.getResponseCode() == 200) {
      			responseStr = "File has been written successfully. ResponseCode: " + httpConn.getResponseCode();
      		} else {
      			responseStr += "Error while writing file, ResponseCode: " + httpConn.getResponseCode() + " "
      					+ httpConn.getResponseMessage();
      		}
      		
      		System.out.println("upload Response :  "+ responseStr);
      	}
      Author's profile photo Dilip Kumar KrishnaDev Pandey
      Dilip Kumar KrishnaDev Pandey
      Blog Post Author

      Hi Anurag,

      • Code is fine.
      • Can you just try to 1st write a file in default directory 'Documents' using //*///_api/web/GetFolderByServerRelativeUrl('Shared%20Documents')//*
      • If you can see file in this directory, then there may be some display/read permission issue with your directory.

       

      Regards,

      Dilip

      Author's profile photo anurag kumar
      anurag kumar

      Hi Dilip,

      Once again Thanks a lot !!
      for the detailed explanation in blog and your quick responses.
      For my case file was getting stuck into check out stage , so after file upload I have to check in the file using rest api .
      and URL i have to hit for check in files is (post request with access token)-

      https://myOrganizationUrl/sites/abc/_api/web/GetFileByServerRelativeUrl('/sites/abc/folderWhereWeUploadedFile/fileName.extension')/CheckIn(comment='Test',checkintype=0)

      and that's all .

      ______________________________________________________________

      for some user (my case) there will be limited permission to view the files inside share point library . but after check in (or direct upload) success response .we can try downloading the newly uploaded file.

      ______________________________________________________________

      Well , always request access as "FullControl" for the generated token from share point team.

      Dilip Kumar KrishnaDev Pandey  - I see [https://sharepoint.stackexchange.com] and stackoverFlow there are so many questions related to share point upload and download from Rest not answered .

      Can I put your blog link in answers of https://sharepoint.stackexchange.com && stackoverflow ?

      Author's profile photo Dilip Kumar KrishnaDev Pandey
      Dilip Kumar KrishnaDev Pandey
      Blog Post Author

      Hi Anurag,

      Sorry, I was outside and couldn't see blog's query...

      Nice to hear, post 'FullControl', issue was shorted out at your side. It may have helped you to view the files/folders. For writing the File, only 'WRITE' permission is required.

      And yes, you can put my blog link to the other audience's links. I'll be happy to see, if it helps to more people.

       

      Thanks & Regards,

      Dilip

       

      Author's profile photo Nitin Prakash
      Nitin Prakash

      Hi Dilip,

      I have a requirment to read a file/ downlaod from sharepoint location. I am able to do same using postman but not through code which i am using. For below code to work i need fedAuth and rtfa cookie value which will help to get DigestValue but i am not able to obtain both cookies value from code but using same api in postman i am able to do. Can you have a look and help with this?

       

      package sharepointTest;

      import java.io.BufferedReader;
      import java.io.IOException;
      import java.io.InputStream;
      import java.io.InputStreamReader;
      import java.io.OutputStream;
      import java.io.OutputStreamWriter;
      import java.io.StringReader;
      import java.io.Writer;
      import java.net.CookieHandler;
      import java.net.CookieManager;
      import java.net.HttpCookie;
      import java.net.HttpURLConnection;
      import java.net.MalformedURLException;
      import java.net.URL;
      import java.net.URLConnection;
      import java.util.List;

      import javax.xml.parsers.DocumentBuilder;
      import javax.xml.parsers.DocumentBuilderFactory;
      import javax.xml.parsers.ParserConfigurationException;
      import javax.xml.xpath.XPath;
      import javax.xml.xpath.XPathExpressionException;
      import javax.xml.xpath.XPathFactory;

      import org.w3c.dom.Document;
      import org.xml.sax.InputSource;
      import org.xml.sax.SAXException;

      public class SharePointNewClass {

      public static void main(String [] args) throws XPathExpressionException, SAXException, ParserConfigurationException, IOException {
      System.out.println(new SharePointNewClass().login());
      }

      private final String sts = "https://login.microsoftonline.com/extSTS.srf";
      private final String login = "/_forms/default.aspx?wa=wsignin1.0";
      // SAML.xml from https://github.com/lstak/node-sharepoint
      private final String reqXML = "<?xml version=\"1.0\" encoding=\"utf-8\" ?><s:Envelope xmlns:s=\"http://www.w3.org/2003/05/soap-envelope\" xmlns:a=\"http://www.w3.org/2005/08/addressing\" xmlns:u=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd\"><s:Header><a:Action s:mustUnderstand=\"1\">http://schemas.xmlsoap.org/ws/2005/02/trust/RST/Issue</a:Action><a:ReplyTo><a:Address>http://www.w3.org/2005/08/addressing/anonymous</a:Address></a:ReplyTo><a:To s:mustUnderstand=\"1\">https://login.microsoftonline.com/extSTS.srf</a:To><o:Security s:mustUnderstand=\"1\" xmlns:o=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd\"><o:UsernameToken><o:Username>[username]</o:Username><o:Password>[password]</o:Password></o:UsernameToken></o:Security></s:Header><s:Body><t:RequestSecurityToken xmlns:t=\"http://schemas.xmlsoap.org/ws/2005/02/trust\"><wsp:AppliesTo xmlns:wsp=\"http://schemas.xmlsoap.org/ws/2004/09/policy\"><a:EndpointReference><a:Address>[endpoint]</a:Address></a:EndpointReference></wsp:AppliesTo><t:KeyType>http://schemas.xmlsoap.org/ws/2005/05/identity/NoProofKey</t:KeyType><t:RequestType>http://schemas.xmlsoap.org/ws/2005/02/trust/Issue</t:RequestType><t:TokenType>urn:oasis:names:tc:SAML:1.0:assertion</t:TokenType></t:RequestSecurityToken></s:Body></s:Envelope>";

      private String generateSAML() {
      String saml = reqXML
      .replace("[username]", "******************");
      saml = saml.replace("[password]", "*******");
      saml = saml.replace("[endpoint]", "https://<tenant>.sharepoint.com");
      return saml;
      }

      public String login() {
      String token = "";
      //String file;
      try {
      token = requestToken();

      String cookie = submitToken(token);
      }
      catch (Exception e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
      }
      return token;

      }

      private String requestToken() throws XPathExpressionException, SAXException,
      ParserConfigurationException, IOException {

      String saml = generateSAML();

      URL u = new URL(sts);
      URLConnection uc = u.openConnection();
      HttpURLConnection connection = (HttpURLConnection) uc;

      //connection.connect();
      connection.setDoOutput(true);
      connection.setRequestMethod("POST");
      connection.addRequestProperty("Content-Type","text/xml; charset=utf-8");

      OutputStream out = connection.getOutputStream();
      Writer wout = new OutputStreamWriter(out);
      wout.write(saml);

      wout.flush();
      wout.close();

      InputStream in = connection.getInputStream();
      int c;
      StringBuilder sb = new StringBuilder("");

      while ((c = in.read()) != -1)
      sb.append((char) (c));
      in.close();
      String result = sb.toString();

      System.out.println("Result :" + result);
      String token = extractToken(result);
      connection.disconnect();

      return token;
      }

      private String extractToken(String result) throws ParserConfigurationException, SAXException, IOException, XPathExpressionException {
      // TODO Auto-generated method stub
      DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
      DocumentBuilder db = dbf.newDocumentBuilder();

      Document document = db.parse(new InputSource(new StringReader(result)));

      XPathFactory xpf = XPathFactory.newInstance();
      XPath xp = xpf.newXPath();
      String token = xp.evaluate("//BinarySecurityToken/text()", document.getDocumentElement());

      return token;
      }

      private String submitToken(String token) throws IOException {
      // TODO Auto-generated method stub
      String url = "https://<tenant>.sharepoint.com/" + login;
      // CookieManager cookieManager = new CookieManager();
      // CookieHandler.setDefault(cookieManager);

      URL u = new URL(url);
      URLConnection uc = u.openConnection();
      HttpURLConnection connection = (HttpURLConnection) uc;
      connection.setDoOutput(true);

      connection.setRequestMethod("POST");
      //connection.addRequestProperty("Content-Type", "application/x-www-form-urlencoded");
      connection.addRequestProperty("Accept", "application/json; odata=verbose");
      connection.addRequestProperty("User-Agent", "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Win64; x64; Trident/5.0)");
      connection.addRequestProperty("Content-Type","text/xml; charset=utf-8");
      connection.addRequestProperty("BinarySecurityToken", token);
      connection.setRequestProperty("Content-Length", "calcualte");

      OutputStream out = connection.getOutputStream();

      Writer wout = new OutputStreamWriter(out);

      wout.write(token);

      InputStream in = connection.getInputStream();
      //String headerName="";
      //String headerValue="";
      List<String> cookies = connection.getHeaderFields().get("Set-Cookie");
      System.out.println("cookies:"+ cookies);

      //readFile(token);

      return "";
      }

      private void readFile(String token) {
      // TODO Auto-generated method stub
      String siteURL = "https://<tenant>.sharepoint.com/<folder>"  ;
      String wsUrl = siteURL + "/_api/web/GetFileByServerRelativeUrl(/Shared%20Documents//Files('Sample.txt')/$value" + login;

      try
      {
      URL u1 = new URL(wsUrl);

      URLConnection uc1 = u1.openConnection();
      HttpURLConnection connection1 = (HttpURLConnection) uc1;
      connection1.setRequestMethod("GET");

      connection1.setRequestProperty("Authorization", "Bearer " + token);
      System.out.println("response :"+connection1.getResponseCode());

      String httpResponseStr = "";
      InputStreamReader isr = null;
      if (connection1.getResponseCode() == 200) {
      isr = new InputStreamReader(connection1.getInputStream());
      System.out.println("in"+isr.toString());
      } else {
      isr = new InputStreamReader(connection1.getErrorStream());

      System.out.println("out"+isr.toString());
      }
      BufferedReader in1 = new BufferedReader(isr);
      String strLine = "";
      while ((strLine = in1.readLine()) != null) {
      httpResponseStr = httpResponseStr + strLine;
      }
      System.out.println("Response is"+httpResponseStr);

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

      }
      }

      O/p:
      Result :<?xml version="1.0" encoding="utf-8"?><S:Envelope xmlns:wsa="http://www.w3.org/2005/08/addressing" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:wsp="http://schemas.xmlsoap.org/ws/2004/09/policy" xmlns:wst="http://schemas.xmlsoap.org/ws/2005/02/trust" xmlns:S="http://www.w3.org/2003/05/soap-envelope"><S:Header><wsa:Action S:mustUnderstand="1" wsu:Id="Action">http://schemas.xmlsoap.org/ws/2005/02/trust/RSTR/Issue</wsa:Action><wsa:To S:mustUnderstand="1" wsu:Id="To">http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous</wsa:To><wsse:Security S:mustUnderstand="1"><wsu:Timestamp wsu:Id="TS" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"><wsu:Created>2018-11-05T06:15:42.9392629Z</wsu:Created><wsu:Expires>2018-11-05T06:20:42.9392629Z</wsu:Expires></wsu:Timestamp></wsse:Security></S:Header><S:Body xmlns:S="http://www.w3.org/2003/05/soap-envelope"><wst:RequestSecurityTokenResponse xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:wsp="http://schemas.xmlsoap.org/ws/2004/09/policy" xmlns:wst="http://schemas.xmlsoap.org/ws/2005/02/trust"><wst:TokenType>urn:passport:compact</wst:TokenType><wsp:AppliesTo><wsa:EndpointReference xmlns:wsa="http://www.w3.org/2005/08/addressing"><wsa:Address>https://tenant.sharepoint.com</wsa:Address></wsa:EndpointReference></wsp:AppliesTo><wst:Lifetime><wsu:Created>2018-11-05T06:15:42Z</wsu:Created><wsu:Expires>2018-11-06T06:15:42Z</wsu:Expires></wst:Lifetime><wst:RequestedSecurityToken><wsse:BinarySecurityToken Id="Compact0" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">t=TOKEN VALUE"></wsse:Reference></wsse:SecurityTokenReference></wst:RequestedUnattachedReference></wst:RequestSecurityTokenResponse></S:Body></S:Envelope>
      cookies:[stsservicecookie=ests; path=/; secure; HttpOnly, x-ms-gateway-slice=001; path=/; secure; HttpOnly, esctx=*******************************************-; domain=.login.microsoftonline.com; path=/; secure; HttpOnly, ExternalIdpStateHash=u2-TAnvuEVMlfA_1q9EZxCc8exkxh22SiKAPq8QqWgg; path=/; secure; HttpOnly, buid=******************-; expires=Wed, 05-Dec-2018 06:15:43 GMT; path=/; secure; HttpOnly]
      response :401
      outjava.io.InputStreamReader@3c756e4d
      Response is{"error_description":"Unsupported security token."}
      t=TOKEN ValUE

      Author's profile photo anurag kumar
      anurag kumar

      Hi Nitin,

      for your query [ For below code to work i need fedAuth and rtfa cookie value which will help to get DigestValue but i am not able to obtain both cookies value from code but using same api in postman ]

      can you check with your share point team what is access for the client id and client secret they shared with you.

      if we have access as "FullControl" in app permission then we don't need any of these cookies . in get request for downloading a file .

      Author's profile photo Nitin Prakash
      Nitin Prakash

      Hi Anurag,

       

      Thanks for response. I am yet to check that. If we have FullControl then my readFile code is correct? As i run that and get response as Unsupported security Token:

      response :401
      outjava.io.InputStreamReader@51b7e5df
      Response is{"error_description":"Unsupported security token."}

      Author's profile photo anurag kumar
      anurag kumar

      Hi Nitin,
      please read this entire blog it have all your answers . as i see in your read please check your rest api url (which you are trying to send as get request too) – https://sharepoint.stackexchange.com/a/250371/79002

      Author's profile photo Nitin Prakash
      Nitin Prakash

      Hi Anurag,

       

      Yeah i went through Dilip blog and written his code to read a File and i have FullControl over it.

      Thing is every time i am getting unauthorized error or secrity token not valid depending how below line is written.

      if no space after Bearer in
      connection1.setRequestProperty("Authorization", "Bearer" + token);
      then response is:
      Response is<?xml version="1.0" encoding="utf-8"?><m:error xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata"><m:code>-2147024891, System.UnauthorizedAccessException</m:code><m:message xml:lang="en-US">Access denied. You do not have permission to perform this action or access this resource.</m:message></m:error>

      if  space after Bearer :
      Response is{“error_description”:”Unsupported security token.”}

      ,and written url as explained in above blog or the link which you had shared.

       

      Author's profile photo Dilip Kumar KrishnaDev Pandey
      Dilip Kumar KrishnaDev Pandey
      Blog Post Author

      Hi Nitin,

      • Sorry for delayed response, was outside with limited access to Blog……..
      • You are getting ‘permission’ issue while fetching access token itself.
      • To resolve it, your Sharepoint-Client-ID need to have ‘Write’ permission.
      • Please have a look to below blog and ask your SharePoint team to do so:
      • Integrate SharePoint using SAP PI

      Thanks & Regards,

      Dilip

      Author's profile photo Nitin Prakash
      Nitin Prakash

      Hi Dilip,

       

      Thanks for response!!

      Do we need to deploy application also?  or the just demo?

      Author's profile photo Dilip Kumar KrishnaDev Pandey
      Dilip Kumar KrishnaDev Pandey
      Blog Post Author

      Hi Nitin,

      • Given blog's examples are live business scenarios at our side.
      • At our side, we were having two instances of SharePoint, TEST and PRD, respective client-ID were given 'WRITE' permissions.
      • Respective SharePoint credentials were used with SAP-PI's TEST(Quality)/PRD environments.

      Thanks & Regards,

      Dilip

       

      Author's profile photo Nitin Prakash
      Nitin Prakash

      Thanks Dilip, I will try to get it deployed and check whether it works or not

      Author's profile photo Nitin Prakash
      Nitin Prakash

      Hi Dilip,

      Small help. what ype of file you provided to sharepoint team to deploy? .jar or .app? any deployement steps for .jar to deploy in Sharepoint?

       

      Regards,

      Nitin

      Author's profile photo Dilip Kumar KrishnaDev Pandey
      Dilip Kumar KrishnaDev Pandey
      Blog Post Author

      Nitin,

      We don’t have to share any file (.jar or .app) to SharePoint-Team for deployment.

      We are using SharePoint-REST details to send data/file in SharePoint from SAP-PI.

      In SAP-PI, you can use Java UDFs to consume required SharePoint-RESTs.

      If you want to write JavaMap, you can refer below blog and create .jar file which need to be deployed/used in sapi-pi interface.

       

      thanks & regards,

      Dilip

       

      Author's profile photo Nitin Prakash
      Nitin Prakash

      Thanks Dilip.

       

      But we were not using SAP-PI tool for it. Can we have chat over DM?

      Author's profile photo Dilip Kumar KrishnaDev Pandey
      Dilip Kumar KrishnaDev Pandey
      Blog Post Author

      Dear Nitin,

      Then in that case, you should be having some executable platform (some application) in which you can in-corporate SharePoint-REST APIs.

      For e.g.

      • a web-based application where you can use respective language syn-taxes to consume SharePoint-REST APIs to read/Write files in SharePoint.
      • or a JavaApplet application where you can use above blog's method to work with SharePoint

       

      Thanks & Regards,

      Dilip

      Author's profile photo Gayetri Mansingh
      Gayetri Mansingh

      hi Dilip,

      Glad to see your post about sharepoint API. Its really helpful.

      My requirement is download all the files from the document library to the local system.

      I followed ur code of. download files. But I can only able to get the file name and its contents on the eclipse console.

       

      Following is the runnable code where i can see all the file names and its content in the console.

      ==================================================

      package com.ericsson.searg.sharepointaccess;

      import java.io.BufferedReader;
      import java.io.ByteArrayInputStream;
      import java.io.IOException;
      import java.io.InputStreamReader;
      import java.net.HttpURLConnection;
      import java.net.URL;
      import java.net.URLConnection;

      import com.google.gson.JsonArray;
      import com.google.gson.JsonElement;
      import com.google.gson.JsonObject;
      import com.google.gson.JsonParser;

      public class test {
      public static void getFileList() throws IOException {
      String accessToken = GenerateSharepointToken.getSharepointToken();

      try {
      // Frame SharePoint siteURL
      String siteURL = “https://ericsson.sharepoint.com/sites/NRONICIntegration”;

      // Frame SharePoint URL to retrieve all of the files in a folder
      String wsUrl = siteURL
      + “/_api/web/GetFolderByServerRelativeUrl(‘Shared%20Documents/NIC%20Integration%20Logs/T-MOBILE/TMO%20Integration%20Migration/GayetriNode’)/Files”;

      // Create HttpURLConnection
      URL url = new URL(wsUrl);
      // System.out.println(“url—>”+url);
      URLConnection connection = url.openConnection();

      HttpURLConnection httpConn = (HttpURLConnection) connection;

      // Set Header
      httpConn.setDoOutput(true);
      httpConn.setDoInput(true);
      httpConn.setRequestMethod(“GET”);
      httpConn.setRequestProperty(“Authorization”, “Bearer ” + accessToken);
      httpConn.setRequestProperty(“accept”, “application/json;odata=verbose”); // To
      // System.out.println(“accessToken———->” + accessToken); //
      // response

      // httpConn.setRequestProperty(“accept”, “application/atom+xml”);
      // //To get response in XML

      // Read the response

      String httpResponseStr = “”;
      InputStreamReader isr = null;
      System.out.println(“httpConn.getResponseCode()>>>” + httpConn.getResponseCode());

      if (httpConn.getResponseCode() == 200) {
      isr = new InputStreamReader(httpConn.getInputStream());

      } else {
      isr = new InputStreamReader(httpConn.getErrorStream());
      }

      BufferedReader in = new BufferedReader(isr);
      String strLine = “”;
      while ((strLine = in.readLine()) != null) {
      httpResponseStr = httpResponseStr + strLine;
      }
      System.out.println(httpResponseStr); // Print response

      } catch (Exception e) {
      // System.out.println(“Error while reading file: ” +
      // e.getMessage());

      }
      }

      @SuppressWarnings(“resource”)
      public static void main(String s[]) throws IOException {
      String accessToken = GenerateSharepointToken.getSharepointToken();

      try {

      String siteURL = “https://ericsson.sharepoint.com/sites/NRONICIntegration”;

      // Frame SharePoint URL to retrieve all of the files in a folder
      String wsUrl = siteURL
      + “/_api/web/GetFolderByServerRelativeUrl(‘Shared%20Documents/NIC%20Integration%20Logs/T-MOBILE/TMO%20Integration%20Migration/GayetriNode’)/Files”;

      // Create HttpURLConnection
      URL url = new URL(wsUrl);
      // System.out.println(“url—>”+url);
      URLConnection connection = url.openConnection();

      HttpURLConnection httpConn = (HttpURLConnection) connection;

      // Set Header
      // httpConn.setDoOutput(true);
      // httpConn.setDoInput(true);
      httpConn.setRequestMethod(“GET”);
      httpConn.setRequestProperty(“Authorization”, “Bearer ” + accessToken);
      httpConn.setRequestProperty(“accept”, “application/json;odata=verbose”);
      // Read the response

      String httpResponseStr = “”;
      InputStreamReader isr = null;

      if (httpConn.getResponseCode() == 200) {
      isr = new InputStreamReader(httpConn.getInputStream());

      } else {
      isr = new InputStreamReader(httpConn.getErrorStream());
      }

      BufferedReader in = new BufferedReader(isr);
      // File file = new File(“data.txt”);
      // FileWriter fileWriter = new FileWriter(file);
      //
      // fileWriter.write(httpResponseStr);
      String strLine = “”;
      while ((strLine = in.readLine()) != null) {
      httpResponseStr = httpResponseStr + strLine;
      System.out.println(“strLine==” + strLine);
      // fileWriter.write(httpResponseStr);

      }
      System.out.println(httpResponseStr); // Print response
      String fileName=””;
      JsonParser parser = new JsonParser();
      JsonObject rootObj = parser.parse(httpResponseStr).getAsJsonObject();
      JsonArray nameArray = rootObj.get(“d”).getAsJsonObject().get(“results”).getAsJsonArray();
      for (JsonElement pa : nameArray) {
      JsonObject jsonNameObj = pa.getAsJsonObject();
      fileName = jsonNameObj.get(“Name”).getAsString();
      System.out.println(“fileName :” + fileName);
      String fileUrl = siteURL+ “/_api/web/GetFolderByServerRelativeUrl(‘Shared%20Documents/NIC%20Integration%20Logs/T-MOBILE/TMO%20Integration%20Migration/GayetriNode’)/Files(‘”+fileName+”‘)/$value”;

      // Create HttpURLConnection
      URL urlf = new URL(fileUrl);
      System.out.println(“urlf—>”+urlf);
      URLConnection connection1 = urlf.openConnection();

      HttpURLConnection httpConnf = (HttpURLConnection) connection1;

      // Set Header
      // httpConn.setDoOutput(true);
      // httpConn.setDoInput(true);
      httpConnf.setRequestMethod(“GET”);
      httpConnf.setRequestProperty(“Authorization”, “Bearer ” + accessToken);
      httpConnf.setRequestProperty(“accept”, “application/json;odata=verbose”);

      String httpResponseFileStr = “”;
      InputStreamReader isrf = null;

      if (httpConnf.getResponseCode() == 200) {
      isrf = new InputStreamReader(httpConnf.getInputStream());

      } else {
      isrf = new InputStreamReader(httpConnf.getErrorStream());
      }
      BufferedReader inf = new BufferedReader(isrf);
      String strLinef = “”;
      String targetFolder = “c:/temp/”;
      while ((strLinef = inf.readLine()) != null) {
      httpResponseFileStr = httpResponseFileStr + strLinef;
      // System.out.println(“strLinef :”+strLinef);

      }
      System.out.println(“httpResponseFileStr :”+httpResponseFileStr);

      }

      // fileWriter.close();

      } catch (Exception e) {
      // System.out.println(“Error while reading file: ” +
      // e.getMessage());

      }
      }
      }

      ==========================================================

       

      However, I could able to achieve this download functionality using Pthyon with the follwoing code snippet:

      ============

      print(‘Downloading\t’+filename,’—‘,fn)
      requestFileGetUrl1 = ‘https://ericsson.sharepoint.com/sites/NRONICIntegration/_api/web/GetFolderByServerRelativeUrl(\” + fileUrl + ‘\’)/Files(\”+ filename + ‘\’)/$value’

      getFile = requests.get(requestFileGetUrl1, headers=update_headers)
      with open(filename, ‘wb+’) as f:
      for chunk in getFile.iter_content(chunk_size=1024):
      if chunk:
      f.write(chunk)
      f.flush()
      print(“Files Downloaded…”+str(getFile))

      =====================================

      and the same I need in JAva as well.

       

      Your help is much appreciated.

       

      thanks in advance

      Gayetri

      Author's profile photo Dilip Kumar Krishnadev Pandey
      Dilip Kumar Krishnadev Pandey
      Blog Post Author

      Dear Gayetri,

      You are getting the fileName and its content (httpResponseFileStr) in console, because your program is written so.

      To download file, you just update your code with logic "String to File Conversion". You can use below java code snippet for same:

      try {
        File file = new File("D:/test1.txt");
        FileWriter fileWriter = new FileWriter(file);
        fileWriter.write(httpResponseFileStr); 			
        fileWriter.flush();
        fileWriter.close();
      } catch (IOException e) {
        e.printStackTrace();
      }

       

      Thanks & Regards,

      Dilip

      Author's profile photo Gayetri Mansingh
      Gayetri Mansingh

      Thanks a lot Dilip for you response.

      I got the file dowloaded, and I used the following code to download the file.

       

      FileOutputStream outputStream = new FileOutputStream(saveFilePath);
      int bytesRead = -1;
      byte[] buffer = new byte[1024];
      while ((bytesRead = inputStream.read(buffer)) != -1) {
      outputStream.write(buffer, 0, bytesRead);
      }

       

      Thank you again

      Best Regards

      Gayetri

      Author's profile photo Gayetri Mansingh
      Gayetri Mansingh

      Hi Dilip,

      Gayetri here again.

      I have a requirement to download latest 5 files from sharepoint directory.

      Please help.

       

      thanks and best regards

      Gayetri

      Author's profile photo Dilip Kumar Krishnadev Pandey
      Dilip Kumar Krishnadev Pandey
      Blog Post Author

      Dear Gayetri,

      Sorry for very late interaction, till now, you may have already addressed the requirement. If so, can you please share tips.

      For your case, I would have been seeking info from Sharpoint-Team about respective REST parameters to get latest 5 files of directory.

      Thanks & Regards,

      Dilip

      Author's profile photo Gayetri Mansingh
      Gayetri Mansingh

      Hi Dilip,

      Thanks for your response.

      I got that resolved.

      I used

      _api/Web/GetFolderByServerRelativeUrl('folderRelativeUrl')?$top=5

       

      Best Regards

      Gayetri

      Author's profile photo Sam Sammy
      Sam Sammy

      Hi Dilip ,

       

      I tried to create a test.txt file as per the UDF and it worked well . How can I create a Base64 pdf or xls files to share point ? When I try to pass the base64 format in the string request , the files is getting created but corrupted and unable to open . Please advise . 

       

      Thank you 

      Author's profile photo Dilip Kumar Krishnadev Pandey
      Dilip Kumar Krishnadev Pandey
      Blog Post Author

      Dear Sam,

      I had successfully posted XLSX, TXT, PDF files using either Java-UDFs or Java-Map-Program, but not yet tried with BASE64 string. If I get a chance, will try and let you know. However, if you need any help related to pdf file posting, you can refer below link:

      https://blogs.sap.com/2018/08/26/post-pdf-file-in-sharepoint-using-sap-pi-java-map/

      Thanks & Regards,

      Dilip

       

      Author's profile photo Sam Sammy
      Sam Sammy

      Thank you Dilip for you quick response. Could you direct me how you have handled .xlsx documents ?  Im getting a binary format of a tab delimited file from ECC and when I convert to .xlsx it is throwing error but if I open the same in .txt it works . ECC is not able to convert directly a .xls to binary . So any inputs on how you handled from ECC . 

       

      Appreciate your help !! 

      Author's profile photo Dilip Kumar Krishnadev Pandey
      Dilip Kumar Krishnadev Pandey
      Blog Post Author

      Hi Sam,

      In my case, I had XLSX file on source location, which I had picked up and send to SharePoint using JavaProgram.

      You have TAB separated TXT file which you want to convert it into XLSX,

      please check JavaCode techniques for same on other online sources. Below link references may help you:

      https://www.easyxls.com/manual/tutorials/java/export-data-to-xlsx-file.html

      https://www.easyxls.com/manual/tutorials/java/convert-csv-to-excel.html

       

      And once, you are good with the conversion, go ahead for posting it into SharePoint.

       

      Thanks & Regards,

      Dilip Pandey

      Author's profile photo chinmaya pradhan
      chinmaya pradhan

      HI Dilip,

      Thanks for this wonderful post.

      I am able to upload to SP using the Java REST API. However I am facing issue with the download program. The content being uploaded is different after download.

       

       

      uploaded text file content : Hello  Test.

      downloaded content is : AAAAAAAA=

       

      code :

      String wsUrl = get_rootdtls_URI + "/_api/web/GetFolderByServerRelativeUrl('/sites/vemt_rpt/Shared%20Documents/')/files('temp.txt')/$value";
      URL obj = new URL(wsUrl);
      //url += param;
      HttpURLConnection con = (HttpURLConnection) obj.openConnection();
      con.setRequestMethod("GET");
      con.setRequestProperty("Authorization", "Bearer " + tok);
      con.setRequestProperty("accept", "application/json;odata=verbose");
      //con.setRequestProperty("Accept", "application/octet-stream;UTF-8");
      con.setDoOutput(true);
      con.setDoInput(true);

      String httpResponseStr = "";
      InputStreamReader isr = null;
      if (con.getResponseCode() == 200) {
      isr = new InputStreamReader(con.getInputStream());

      } else {
      isr = new InputStreamReader(con.getErrorStream());
      }

      BufferedReader in = new BufferedReader(isr);
      String strLine = "";
      while ((strLine = in.readLine()) != null) {
      httpResponseStr = httpResponseStr + strLine;
      }
      System.out.println(httpResponseStr); // Print response

       

      Could you please provide some guidance.

      Appreciate your help.

       

      Author's profile photo Ana Neto
      Ana Neto

      It is funny how this integration is not as straightforward as one would expect. I have just written an article on Codementor on it and although it does not mention the integration to SAP, that is also something you can do

      https://www.codementor.io/@anananet/getting-appointments-and-business-documents-into-sharepoint-using-java-1781hsoqks

      Author's profile photo Dilip Kumar Krishnadev Pandey
      Dilip Kumar Krishnadev Pandey
      Blog Post Author

      Hi Ana,

      Thanks for sharing the information.

      Regards, Dilip P.