0

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:

https://organisation.sharepoint.com/sites/Dynamics365Sandbox/_api/web/GetFileByServerRelativeUrl('/sites/Dynamics365Sandbox/account/Test%20Account_231A283D5B33E811811D5065F38A4A41/some%20long%20filename.doc')/moveto(newurl='/sites/Dynamics365Sandbox/accountt/Test%20Account_231A283D5B33E811811D5065F38A4A41/some%20long%20filename.doc',flags=1)

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)";

1 Answer 1

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}'";
5
  • 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? Commented Jun 15, 2018 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
    Commented Jun 15, 2018 at 13:42
  • I am getting BAD Request Error when moving file to different site-subsite. Any idea? Commented Jun 26, 2018 at 11:28
  • What language is this? How to do it on python?
    – avantdev
    Commented Oct 14, 2020 at 2:10
  • No worries, it worked well with python! Thank you~
    – avantdev
    Commented Oct 14, 2020 at 2:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.