I am trying to create a folder from Java rest API on sharepoint

Viewed 146

I am trying to create a folder using the access token I am getting the access but in the end, getting the response code 404. I have tried to develop a JsonInputString and then state the connection I have set values in httpconn and then tried to call the connection what am I doing wrong, It is working if I am hardcoding the values but not when I am dynamically getting it.

here is my code

    String siteURL, String folder) throws IOException {
        String wsURL = siteURL + "/_api/web/folders";
        URL url = new URL(wsURL);
        URLConnection connection = url.openConnection();
        String[] arr = siteURL.split("com");
        String siteNameURL = arr[arr.length-1];
        String jsonInputString = "{'__metadata': { 'type': 'SP.Folder' }, 'ServerRelativeUrl': '/sites"+ siteNameURL +"/Shared%20Documents/"
                + folder + "'}";
        HttpURLConnection httpConn = (HttpURLConnection) connection;
        try {
            clientID = sharePointclientID;
            clientSecret = secretKey;

            // AccessToken url : app.properties

            accessTokenScr = getSharepointTokenScripting();
            // Set header
            httpConn.setRequestProperty("Content-Type", "application/json;odata=verbose");
            httpConn.setRequestProperty("Accept", "application/json;odata=verbose");
            httpConn.setRequestProperty("Authorization", "Bearer " + accessTokenScr);
            httpConn.setDoOutput(true);
            httpConn.setDoInput(true);
            httpConn.setRequestMethod("POST");
            httpConn.setRequestProperty("X-RequestDigest", "SHAREPOINT_FORM_DIGEST");
            httpConn.setRequestProperty("Content-Length", Integer.toString(jsonInputString.getBytes("utf-8").length));
            DataOutputStream wr = new DataOutputStream(httpConn.getOutputStream());

            byte[] input = jsonInputString.getBytes("utf-8");
            wr.write(input, 0, input.length);

            String responseStr = "";
            if (httpConn.getResponseCode() == 201) {
                responseStr = "Folder has been created successfully. ResponseCode: " + httpConn.getResponseCode();
            } else {
                responseStr += "Error while creating folder, ResponseCode: " + httpConn.getResponseCode() + " "
                        + httpConn.getResponseMessage();
            }
            LOG.info(responseStr);
3 Answers

You need to issue a WebDAV MKCOL command, and HttpURLConnection won't do it. You can do it with the Apache HTTP client if your web server supports WebDAV. Normally PUT is used to create content at given location and not a folder.

You need to check what commands your server supports for creating folder (i.e. in the past WebDAV servers would use MKCOL - Creating Folders (WebDAV) ). Just changing client side code alone will not enable such functionality.

I'm interested by your method accessTokenScr = getSharepointTokenScripting(); to get the FormDigestValue of the X-RequestDigest if could can share it!

the only thing I was doing wrong is not adding /sites in the JSON

just changed

String jsonInputString = "{'__metadata': { 'type': 'SP.Folder' }, 'ServerRelativeUrl': '/sites"+ siteNameURL +"/Shared%20Documents/"
                + folder + "'}";

to

String jsonInputString = "{'__metadata': { 'type': 'SP.Folder' }, 'ServerRelativeUrl': '"+ siteNameURL +"/Shared%20Documents/"
        + folder + "'}";

and it worked

Related