/*
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
/*
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());
}
/*
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());
}
/*
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());
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
6 | |
4 | |
4 | |
4 | |
4 | |
3 | |
3 | |
3 | |
|