Sharepoint
Lib
Rest
Example:
- get a list: List and list items
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", "https://hostname", "domain"));
CloseableHttpClient httpclient = HttpClients.custom()
.setDefaultCredentialsProvider(credsProvider)
.build();
try {
HttpGet httpget = new HttpGet("http://hostname/_api/web/lists");
System.out.println("Executing request " + httpget.getRequestLine());
CloseableHttpResponse response = httpclient.execute(httpget);
try {
System.out.println("----------------------------------------");
System.out.println(response.getStatusLine());
EntityUtils.consume(response.getEntity());
} finally {
response.close();
}
} finally {
httpclient.close();
}
}
}
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.4.1</version>
</dependency>
HttpGet request = new HttpGet("/_api/web/GetFileByServerRelativeUrl('" + file + "')/$value");
CloseableHttpResponse response2 = httpclient.execute(request);
HttpEntity entity = response2.getEntity();
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(entity));
}