22 July, 2016

Sharepoint REST API call



Guys, I am sure this snippet will help you a lot when you do sharepoint REST integration to read files

import java.io.File;
import java.io.InputStream;
import java.io.StringWriter;

import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHeaders;
import org.apache.http.HttpStatus;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.NTCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicHeader;
import org.apache.http.util.EntityUtils;

public class SharePointClientAuthentication {

 public static void main(String[] args) throws Exception {
  CredentialsProvider credsProvider = new BasicCredentialsProvider();
  credsProvider.setCredentials(new AuthScope(AuthScope.ANY),
    new NTCredentials("username", "password",
      "domain", "DNS"));
  CloseableHttpClient httpclient = HttpClients.custom()
    .setDefaultCredentialsProvider(credsProvider).build();
  try {
   /*****To get all the items from folder****/
   HttpGet httpget = 
new HttpGet("http://yourdomain/salesmarketing/_api/web/Lists/GetbyTitle('Marketing%20Information')/items");
   // Set request header parameters
   Header header = new BasicHeader(HttpHeaders.ACCEPT,
     "application/json;odata=verbose");
   httpget.setHeader(header);
   System.out.println("Executing request URI " + httpget.getRequestLine());
   // Execute Client Request
   long startTime = System.currentTimeMillis();
   CloseableHttpResponse response = httpclient.execute(httpget);
   long endTime = System.currentTimeMillis();
   long duration = (endTime - startTime);
   System.out.println("duration in ms >>>>>>>>>>>>>" + duration);
   System.out.println("Status  : -" + response.getStatusLine());
   // Printing Response JSON
   HttpEntity entity = response.getEntity();
   InputStream st = entity.getContent();
   StringWriter writer = new StringWriter();
   IOUtils.copy(st, writer);
   String content = writer.toString();
   // JSON Response
   System.out.println("JSON Response " + content);
   // The real request, reuse authentication
   String file = "/salesmarketing/Marketing Information/ABC.docx"; // source
   String targetFolder = "C:/TEMP";
   HttpGet request2 = new HttpGet("http://yourdomain/salesmarketing/_api/Web
/GetFileByServerRelativeUrl('/salesmarketing/Marketing%20Information/ABC.docx')/$value");
   CloseableHttpResponse response2 = null;
   response2 = httpclient.execute(request2);
   int rc = response2.getStatusLine().getStatusCode();
   String reason = response2.getStatusLine().getReasonPhrase();
   if (rc == HttpStatus.SC_OK) {
    System.out.println("Writing " + file + " to " + targetFolder);
    File f = new File(file);
    File ff = new File(targetFolder, f.getName()); // target
    // writing the byte array into a file using Apache Commons IO
    FileUtils.writeByteArrayToFile(ff,
      EntityUtils.toByteArray(response2.getEntity()));
   } else {
    throw new Exception("Problem while receiving " + file
      + "  reason : " + reason + " httpcode : " + rc);
   }
   // Release all resources held by the httpEntity
   EntityUtils.consume(response.getEntity());
   EntityUtils.consume(response2.getEntity());
  } finally {
   httpclient.close();
  }
 }
}

Popular Posts

Featured Post

Liferay 7.3 compatibility matrix

Compatibility Matrix Liferay's general policy is to test Liferay Portal CE against newer major releases of operating systems, open s...