In SharePoint Online, I'm trying to move files that are in a folder in a library to another folder in another library. I'm running a console app to do this so using the API.

I'm mostly there but getting URL Length 400 error "The length of the URL for this request exceeds the configured maxUrlLength value." for some files using this query:

odataQuery = $"_api/web/GetFileByServerRelativeUrl('/{subsite}/{sourceLibrary}/{Folder}/{r.Name}')/moveto(newurl='/{subsite}/{destLibrary}/{Folder}/{r.Name}',flags=1)";

Which gives this URL:


I'm trying to find a way to do this using the UniqueId of the file but I'm unsure which part of the URL is causing the problem and how to resolve it.

I've tried this but get a malformed URL error "Bad Request":

odataQuery = $"_api/web/lists/getbytitle('{subsite}/{sourceLibrary}')/items?$select=id&$filter=Id eq '{r.UniqueId}'/moveto(newurl='/{subsite}/{destLibrary}/{Folder}/{r.Name}',flags=1)";

One way to get around this limitation is to pass both the source and destination as aliases in the query string.

var source = $"/{subsite}/{sourceLibrary}/{Folder}/{r.Name}";
var destination = $"/{subsite}/{destLibrary}/{Folder}/{r.Name}";

odataQuery = $"_api/web/GetFileByServerRelativeUrl(@source)/moveto(newurl=@destination,flags=1)?@source='{source}'&@destination='{destination}'";
  • Nicely done - it works on my first test with an erroneous file name. Have you doubled up the single quotes in the appended query string? Jun 15 '18 at 13:21
  • I just made a change to the source/destination variables as I hadn't updated the values with the $ and double quotes.
    – horinedev
    Jun 15 '18 at 13:42
  • I am getting BAD Request Error when moving file to different site-subsite. Any idea? Jun 26 '18 at 11:28
  • What language is this? How to do it on python? Oct 14 '20 at 2:10
  • No worries, it worked well with python! Thank you~ Oct 14 '20 at 2:29

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.