MoveToUsingPath throwing -2147024891, System.UnauthorizedAccessException

Harshit Shah 1 Reputation point
2022-02-01T06:34:11.103+00:00

Hi previously I am using the following rest API to move the file or rename:

POST: {{baseUrl}}/{{siteName}}/_api/web/GetFileByServerRelativePath(decodedUrl=@v)/moveTo(newurl=@s,flags=0)?@v='oldpath'&@s='newpath'

This is working fine but causing an issue for the file name containing % or # value.

So to solve the issue I found on the following post.

So I have tried following rest API:

POST: {{baseUrl}}/{{siteName}}/_api/web/GetFileByServerRelativePath(decodedUrl=@v)/moveToUsingPath(newPath=@s,moveOperations=0)?@v='oldpath'&@s='newpath'

But it is throwing the following error:

{
"error": {
"code": "-1, Microsoft.Data.OData.ODataException",
"message": {
"lang": "en-US",
"value": "An unexpected 'PrimitiveValue' node was found when reading from the JSON reader. A 'StartObject' node was expected."
}
}
}

if I tried to call rest API this way:

POST: {{baseUrl}}/{{siteName}}/_api/web/GetFileByServerRelativePath(decodedUrl='oldpath')/moveToUsingPath(newPath='newpath',moveOperations=0)

then it throws following error:

{
"error": {
"code": "-2147024891, System.UnauthorizedAccessException",
"message": {
"lang": "en-US",
"value": "Access denied."
}
}
}

Not sure what wrong I am doing.

SharePoint
SharePoint
A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.
9,694 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. RaytheonXie_MSFT 31,606 Reputation points Microsoft Vendor
    2022-02-01T09:38:08.987+00:00

    Hi @HarshitShah-3176 ,
    You can refer to following code to move files.

    /_api/web/getfilebyserverrelativeurl(decodedUrl='folder name/file name')/MoveToUsingPath(newPath='/Lybrary_2',moveOperations=1)  
    

    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


    1 person found this answer helpful.