3
votes

I am trying to post a SharePoint list item to a SharePoint 2010 list using Java client. I am able to read the SP list items using below code but I am not sure how to write to the SharePoint. For the post operation, I am getting http 401 error. Any guidance will be greatly appreciated.

GET Operation:

String url = "https://organization.company.com/sites/Ateam/_vti_bin/ListData.svc/TableLoadInfo";        DefaultHttpClient client = new DefaultHttpClient();

        NTCredentials credentials = new NTCredentials(username, password, hostname, domain);
        AuthScope scope = new AuthScope(hostname, 443);

        SSLContext sslcontext = SSLContext.getInstance("TLS");
        sslcontext.init(null, new TrustManager[] { trustmanager }, null);

        SSLSocketFactory sf = new SSLSocketFactory(sslcontext,              SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
        Scheme https = new Scheme("https", 443, sf);

        client.getConnectionManager().getSchemeRegistry().register(https);
        client.getCredentialsProvider().setCredentials(scope, credentials);

        HttpGet request = new HttpGet(url);
        //request.addHeader("Accept", "application/xml");
        request.addHeader("Accept", "application/json;odata=verbose");

        HttpResponse response = client.execute(request);

POST Operation:

String url = "https://organization.company.com/sites/Ateam/_vti_bin/ListData.svc/TableLoadInfo";

DefaultHttpClient client = new DefaultHttpClient();

        NTCredentials credentials = new NTCredentials(username, password, hostname, domain);
        AuthScope scope = new AuthScope(hostname, 443);

        SSLContext sslcontext = SSLContext.getInstance("TLS");
        sslcontext.init(null, new TrustManager[] { trustmanager }, null);

        SSLSocketFactory sf = new SSLSocketFactory(sslcontext, SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
        Scheme https = new Scheme("https", 443, sf);

        client.getConnectionManager().getSchemeRegistry().register(https);
        client.getCredentialsProvider().setCredentials(scope, credentials);

        // POST a data
        HttpPost request = new HttpPost(url);

                    request.addHeader("Accept", "application/json;odata=verbose");
        request.addHeader("Content-type", "application/json;odata=verbose");
        // request.addHeader("X-RequestDigest", FormDigestValue);
        request.addHeader("X-HTTP-Method", "POST");
        request.addHeader("If-Match", "*");

        JSONStringer json = (JSONStringer) new JSONStringer().object().key("Table_Name").value("TableName 1")
                .key("Load_Frequency").value("Weekly").key("Cycle").value("CURRENT").endObject();


        StringEntity se = new StringEntity(json.toString());
        request.setEntity(se);

                    HttpResponse response = client.execute(request);
1

1 Answers

0
votes

When you post data to SharePoint you have to add "FormDigestValue" inside the header.

Please Note you have commented the line "request.addHeader("X-RequestDigest", FormDigestValue);".

That is the reason you get 401 Error.

First you have to get the "FormDigestValue" value form your current site. Then add that value inside the header. Following Code shows the way to get the FormDigestValue.

            String digestqueryURL = serverurl + subsite + "/" + "_api/contextinfo";
            HttpPost httpPost = new HttpPost(digestquery);
            httpPost.addHeader("Accept", "application/json;odata=verbose");
            httpPost.addHeader("X-ClientService-ClientTag", "SDK-JAVA");
            HttpResponse response = httpClient.execute(httpPost);

            byte[] content = EntityUtils.toByteArray(response.getEntity());

            String jsonString = new String(content, "UTF-8");
            JSONObject json = new JSONObject(jsonString);

            String FormDigestValue = json.getJSONObject("d")
                    .getJSONObject("GetContextWebInformation")
                    .getString("FormDigestValue");

Then add the FormDigestValue to the header when you do the post operations.

request.addHeader("X-RequestDigest", FormDigestValue);