0

I'm attempting to create an item in SharePoint on premise 2016 list using Java and SharePoint API. Forgive me if I missed it - I see a few related questions but they do not seem to really answer my question.

I'm able to do GET and fetch digest value. I am getting error code 400 when attempting to create a list item.

Questions:

  1. Do I still need to worry about authentication? As added complication, I'm attempting to do this using a chatbot attached to internal company messaging system. The sharepoint list is also internal and I never really had to worry about authentication when doing GET or fetching digest value. I'm hoping not because the code is 400, as opposed to 401 or 403
  2. If I do not - from what I've read error 400 could be caused because of incorrect data format to be written into the list or some Java peculiarities with respect to setting Content-Length for output stream. Hoping to find clarity on this.

This is my data:

data = {"__metadata":{"type":"SP.Data.MyListTitleListItem"},"Title ":"New Title"}

My code:

URL url = new URL("https://[SharePoint site]/_api/web/lists/GetByTitle('MyListTitle')/items?
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestProperty("Accept", "application/json;odata=verbose");
urlConnection.setRequestProperty("POST");
urlConnection.setRequestProperty("Content-Type", "application/json;odata=verbose");
urlConnection.setRequestProperty("X-RequestDigest", formDigestValue);

urlConnection.setDoOutput(true);
urlConnection.setDoInput(true);

OutputStream os = urlConnection.getOutputStream();
OutputStreamWriter osw = new OutputStreamWriter(os, "UTF-8");
osw.write(data);
urlConnection.connect();
2

The extra space after "Title" will cause the 400 error.

"Title ":"New Title"

vs.

"Title":"New Title"

If you can get the full error message, you will probably see something like this:

"value": "The property 'Title ' does not exist on type 
'SP.Data.AnnouncementsListItem'. Make sure to only use 
property names that are defined by the type."
  • wow...sharp eyes :). Fixed the extra space in the title and implemented suggestion to get the full error message. Still seeing the issue and this is the error message: value: "A node of type 'EndOfInput' was read from the JSON reader when trying to read the start of an entry. A 'StartObject' node was expected. – Jon C Apr 16 at 19:38
0

I don't know JAVA, so just guessing... is the OutputStreamWriter (ows.write(data);) expecting an object, or a string of JSON text.

I.e.

data = {"__metadata":{"type":"SP.Data.MyListTitleListItem"},"Title":"New Title"}

or

data = '{"__metadata":{"type":"SP.Data.MyListTitleListItem"},"Title":"New Title"}'
  • Yup...finally solved this. It was indeed what form the data should be in the Output stream. I had been sending string - it was the issue. Sending it as bytes solved it.Thank you! – Jon C Apr 17 at 17:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.