如何使用共享点其余API在JAVA中为共享点文档设置自定义字段

时间:2018-11-16 16:56:43

标签: java rest sharepoint-2013

我需要将文档上传到共享点,并使用Java代码为文档设置一些字段。我可以使用以下代码成功完成第一部分:

获取请求摘要:

String digestqueryURL = "<websiteURL>" +"_api/contextinfo";
URL obj = new URL(digestqueryURL);
HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();

writing to ouput steam
con.setDoOutput(true);
con.setRequestMethod("POST");

con.setRequestProperty("User-Agent", "Java Client");
con.setRequestProperty("Content-Type", "application/json;odata=verbose");
con.setRequestProperty("Accept", "application/json; odata=nometadata");
con.setRequestProperty("Content-Length", "2");
OutputStream os = con.getOutputStream();
OutputStreamWriter osw = new OutputStreamWriter(os, "UTF-8");
osw.write("{}");
osw.flush();
osw.close();
os.close();  //don't forget to close the OutputStream
con.connect();

int responseCode = con.getResponseCode();

BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
       response.append(inputLine);
}
in.close();
con.disconnect();
System.out.println(response.toString());

JSONParser parser = new JSONParser();
JSONObject json = (JSONObject) parser.parse(response.toString());
String formDigest = (String)json.get("FormDigestValue");

上传文件:

 String uploadURL = "<websiteURL>" + "/_api/web/getfolderbyserverrelativeurl('<folderName>')/files/add(overwrite='true',url='101_16112018_1.txt')";
File file = new File("<filePath>");
if (file.length() > Integer.MAX_VALUE) {
       System.out.println("File too large");
}
byte[] bytes = new byte[(int) file.length()];
FileInputStream ins = new FileInputStream(file);
ins.read(bytes);

URL uploadURLObj = new URL(uploadURL);
HttpsURLConnection connection=(HttpsURLConnection)uploadURLObj.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("X-RequestDigest",formDigest);
connection.setRequestProperty("Content-Type", "application/json;odata=verbose");
connection.setRequestProperty("Accept", "application/json; odata=nometadata");
connection.setDoOutput(true);
connection.setRequestProperty("Content-Length",
Integer.valueOf(bytes.length).toString());
OutputStream uploadOs = connection.getOutputStream();
uploadOs.write(bytes);
uploadOs.close();
connection.connect();
Integer uploadResponseCode = connection.getResponseCode();
System.out.println(Integer.valueOf(uploadResponseCode));
BufferedReader uploadIn = new BufferedReader(
new InputStreamReader(connection.getInputStream()));
String uploadInputLine;
StringBuffer uploadResponse = new StringBuffer();
while ((uploadInputLine = uploadIn.readLine()) != null) {
           uploadResponse.append(uploadInputLine);
}
System.out.println("upload res" + uploadResponse.toString());
uploadIn.close();
connection.disconnect();

我不确定如何获取上传文件的项目ID。我可以找到一个API,该API允许您更改某个itemId的字段,因此我尝试获取文件夹中的所有项目,并为其中一个选择了itemId,并尝试更改该项目的字段值。

下面的代码不起作用。它会抛出400个错误响应代码

设置itemId的字段:

  String fieldInfo = "{ \"__metadata\": { \"type\":
\"SP.Data.ControlroomdatabaseItem\" }, \"Title\": \"TestUpdated\"}";
byte[] b = fieldInfo.getBytes();
String uploadURL2 = "https://sharepoint.deshaw.com/compliance/_api/web/lists/GetByTitle('<folderName>')/items(<itemId>)";
       URL uploadURLObj2 = new URL(uploadURL2);
       HttpsURLConnection connection2 = (HttpsURLConnection) uploadURLObj2.openConnection();
       connection2.setRequestMethod("POST");
       connection2.setRequestProperty("X-RequestDigest",formDigest);

       connection2.setRequestProperty("Accept", "application/json; odata=verbose");

       connection2.setDoOutput(true);
       connection2.setRequestProperty("Content-Length", Integer.valueOf(b.length).toString());
       OutputStream uploadOs2 = connection2.getOutputStream();
       uploadOs2.write(b);
       uploadOs2.close();

       connection2.connect();

       Integer uploadResponseCode2 = connection2.getResponseCode();


       System.out.println(Integer.valueOf(uploadResponseCode2));

       BufferedReader uploadIn2 = new BufferedReader(
               new InputStreamReader(connection2.getInputStream()));
       String uploadInputLine2;
       StringBuffer uploadResponse2 = new StringBuffer();

       while ((uploadInputLine2 = uploadIn2.readLine()) != null) {
           uploadResponse2.append(uploadInputLine2);
       }
       System.out.println(uploadResponse2.toString());
       uploadIn2.close();
       connection2.disconnect();

设置上传文件字段的正确方法是什么?

0 个答案:

没有答案