I am trying to update metadata for Share Point library document via rest API using JAVA Apache HTTP client.
I am able to get Form Digest Value from SharePoint API context. However receiving error 500(Microsoft.SharePoint.SPException) on submitting post request to update metadata.
"errorcode: -2146232832, The object specified does not belong to a list."
Could you please review below code snippet and advise how I can resolve this issue? Please note that 'ABC%20API%20POC' is Sharepoint library (ABC API POC).
HttpPost post = new HttpPost("/_api/contextinfo");
post.setHeader("Accept", "application/json; odata=verbose");
post.setHeader("content-type", "application/json; odata=verbose");
CloseableHttpResponse responseDigest = httpclient.execute(target, post,context);
byte[] content = EntityUtils.toByteArray(responseDigest.getEntity());
String jsonString = new String(content, "UTF-8");
JSONObject json = new JSONObject(jsonString);
String formDigestValue = json.getJSONObject("d").getJSONObject("GetContextWebInformation").getString("FormDigestValue");
//post request to update the metadata
String updateJson = "{'__metadata': { 'type': 'SP.ListItem'}, 'TestColumn':'TestingValue'}";
String filename="/DocumentRepository/ABC%20API%20POC/upload2.txt";
// ABC%20API%20POC- is a library
HttpPost updateMetaDateRequest = new HttpPost("/_api/web/GetFileByServerRelativeUrl('"+ filename + "')/ListItemAllFields");
updateMetaDateRequest.setHeader("X-RequestDigest", formDigestValue);
updateMetaDateRequest.setHeader("X-HTTP-Method", "MERGE");
updateMetaDateRequest.setHeader("If-Match", "*");
updateMetaDateRequest.setHeader("Accept", "application/json; odata=verbose");
updateMetaDateRequest.setHeader("content-type", "application/json; odata=verbose");
updateMetaDateRequest.setEntity(new StringEntity(updateJson));
try {
CloseableHttpResponse updateMetaDateResponse = httpclient.execute(target,updateMetaDateRequest,context);
int rc = updateMetaDateResponse.getStatusLine().getStatusCode();
String reason = updateMetaDateResponse.getStatusLine().getReasonPhrase();
if (rc != 200) {
System.out.println("updateMetaDateResponse" + " failed " +reason +rc );
}
else {
System.out.println("updateMetaDateResponse:" + rc + reason);
}
}
finally {
// if (updateMetaDateResponse != null) updateMetaDateResponse.close();
}