How do you update/change Parent ID of an item in ShareFile using Java APIs? Or how do you move an item in ShareFile using Java APIs?

  • 1
  • Question
  • Updated 8 months ago
Here's my code

public static void updateParent(OAuth2Token token,
String id, String parentId) 
throws IOException, ParseException
{
URL url = new URL(String.format("https://%s/sf/v3/Items(%s)";, ShareFileV3SampleSE.getHostname(token), id));
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
connection.addRequestProperty("Content-Type", "application/json");
ShareFileV3SampleSE.addAuthorizationHeader(connection, token);
connection.setDoOutput(true);
// HttpUrlConnection does not support the PATCH method by default
connection.addRequestProperty("X-HTTP-Method-Override", "PATCH");
System.out.println(connection.getRequestMethod() + " " + connection.getURL());
connection.connect();

Map<String, Object> folder = new HashMap<String, Object>();
folder.put("parentid", parentId);

Gson gson = new Gson();
String body = gson.toJson(folder);

connection.getOutputStream().write(body.getBytes());

BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
StringBuilder response = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
response.append(line);
}

System.out.println(connection.getResponseCode() + " " + connection.getResponseMessage());
System.out.println(response);      
if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
System.out.println("Updated folder");
}
connection.disconnect();
}
Photo of Eiks Lector

Eiks Lector

  • 1 Post
  • 0 Reply Likes

Posted 9 months ago

  • 1
Photo of Dale Smith

Dale Smith, Software Engineer

  • 186 Posts
  • 26 Reply Likes
Hi Eiks,

While we don't directly expose the Move API currently, it is under consideration to move it from internal to public. In the mean time, you can always perform a Copy operation for the file to the destination parent folder, and then delete the file from the source folder. You can take a look at the Copy Item section of
http://api.sharefile.com/rest/docs/resource.aspx?name=Items

Let us know if that helps,
Dale