1

I am trying to move a file/folder from one location to another using a REST call, but I am getting the following error:

{
    error: {
        code: "-1, Microsoft.SharePoint.Client.InvalidClientQueryException"
        message: "An unexpected 'EndOfInput' node was found when reading from the JSON reader. A 'StartObject' node was expected."
    }-
}

I can successfully run a GET request to get a file/folder, but PATCH is not working.

Here's my headers:

Authorization: Bearer eyJ0eXAiOiJKV1Q...g94tP3hkv9g
If-Match: "{8DFE1DAB-D51B-45F9-B181-E9DE3952B407},1"
Body: {"parentReference": {"id": "01J5374RUCQ5Z4U6XLMND3Q4U7GS6JURNJ"}}

And here's my endpoint:

https://xxx-my.sharepoint.com/_api/v1.0/me/files/02KEHFO3NKD7XIYG6V7FC3DAPJ3Y4VFNAH

My guess is that the Body header is wrong but I have no idea how it should look.

Any ideas?

  • OK, so I've established that the JSON needs to be in the payload rather than a body header.! Hopefully I can work the rest out from there – Tom Aug 11 '15 at 15:17
2

You can use the next code:

$(function() {
            SP.SOD.executeFunc('sp.js', 'SP.ClientContext', CopyFile);
    });

    function CopyFile(){
        var oUrl =  _spPageContextInfo.webAbsoluteUrl +"/_api/web/getfilebyserverrelativeurl('/Library/acento.txt')/copyto(strnewurl='/Lybrary_2/acento.txt',boverwrite=true)";
        $.ajax({
            url: oUrl,
            type: "POST",
            headers: {
                "Accept": "application/json; odata=verbose",
                "X-RequestDigest":  $("#__REQUESTDIGEST").val()
            },
            success: function(data){
                console.log(data);
            },
            error: function(data){
                console.log(data);
            }
        });
    }

This code will copy the selected file the new Document Library, overwriting the files if it's exist. There is another method called MoveTo, the syntax is very similar:

/getfilebyserverrelativeurl('/Shared Documents/filename.docx')
    /moveto(newurl='/Other Folder/filename.docx',flags=1)

I hope to be helpful

  • Is it possible to move the folder in this way? My folder is having multiple folders and multiple documents inside it. – Mihir Oct 18 '18 at 18:48

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.