0

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();
}
  • 1
    You have invalid format of `updateJson`. It should be defined like `String updateJson = "{\"__metadata\": {\"type\": \"SP.ListItem\"},\"TestColumn\": \"TestingValue\"}";` – AshwinK May 09 '19 at 16:08
  • 1
    Have you tried executing this request via `curl` or `postman` just to check whether you are sending requesting properly as well as getting expected response. – AshwinK May 09 '19 at 16:14
  • I tried json with double quote. still getting Internal Server Error 500 – Dinesh Chandra May 09 '19 at 16:19
  • Below is the error description "errorcode: -2146232832, The object specified does not belong to a list." – Dinesh Chandra May 10 '19 at 08:33

0 Answers0