3

I am trying to create a new folder inside a document library using JSOM, below is my code

var requestUri = _spPageContextInfo.webAbsoluteUrl + "/_api/web/folders";
function getFormDigest(webUrl) {
    return $.ajax({
        url: webUrl + "/_api/contextinfo",
        method: "POST",
        headers: { "Accept": "application/json; odata=verbose" }
    });
}

$("#btnClick").click(function(){
return getFormDigest(_spPageContextInfo.webAbsoluteUrl).then(function (data) {

            return  $.ajax({
                url: requestUri,
                type: "POST",
                data:JSON.stringify({'__metadata': { 'type': 'SP.Folder' }, 'ServerRelativeUrl': '/Documents/NewFoldername'}),
                headers: {
                "accept":"application/json;odata=verbose",
                "content-type": "application/json;odata=verbose",
                "X-RequestDigest": data.d.GetContextWebInformation.FormDigestValue
                },
                success: onSuccess,
                error: onError
                });

                function onSuccess(data) {
                alert('Folder created successfully');
                }

                function onError(error) {
                alert(JSON.stringify(error));
                }

                });


    });

});

I am getting the below error

"{"readyState":4,"responseText":"{\"error\":{\"code\":\"-2147024809, System.ArgumentException\",\"message\":{\"lang\":\"en-US\",\"value\":\"Value does not fall within the expected range.\"}}}","responseJSON":{"error":{"code":"-2147024809, System.ArgumentException","message":{"lang":"en-US","value":"Value does not fall within the expected range."}}},"status":400,"statusText":"Bad Request"}"

  • 1
    Can you confirm /Documents/NewFoldername is valid? – Atish Dipongkor - MVP Jul 11 '16 at 12:22
  • 2
    Remove backslash (/) from data relative URL .It should be 'ServerRelativeUrl': 'Documents/NewFoldername' .... data:JSON.stringify({'__metadata': { 'type': 'SP.Folder' }, 'ServerRelativeUrl': 'Documents/NewFoldername'}), – Sabitha S Jul 11 '16 at 12:30
  • Thanks there were two mistakes as u both pointed out 1. it was /Shared Documents/NewFolderName 2. removed backslash before /Shared Documents – Vignesh Subramanian Jul 11 '16 at 12:34
2

As per the comment, It was URL issue. Valid URL is

Shared Documents/NewFolderName

Sabita S added another fix:

Remove backslash (/) from data relative URL .It should be 'ServerRelativeUrl': 'Documents/NewFoldername' .... data:JSON.stringify({'__metadata': { 'type': 'SP.Folder' }, 'ServerRelativeUrl': 'Documents/NewFoldername'})

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.