How to move a file within a SharePoint channel using Microsoft Graph Files API

Cyrus Majidy 25 Reputation points
2023-01-23T20:14:29.29+00:00

My company wants to move data in .xlsx files from a SharePoint channel to a database. A part of this process is being able to archive the files within the SharePoint channel. I have been working tirelessly to move a file within a SharePoint site/channel using the Microsoft Graph Files API, by using a PATCH request, exactly as it is described here in the Microsoft documentation:

https://learn.microsoft.com/en-us/graph/api/driveitem-move?view=graph-rest-1.0&tabs=http

My PowerShell code is formatted as follows:

#Create the JSON object
$Body = @{
    "parentReference" = @{
    		"id" = "{new-parent-folder-id}"
    }
    "name" = “{FileName.xslx}”       #New file name, same as before
}

$Body | ConvertTo-Json


##
$uri = "https://graph.microsoft.com/v1.0/sites/{site id}/drive/items/" + $File.id   #Current item id

$MessageParams = @{
    "URI"         = $uri
    "Headers"     = $Headers
    "Method"      = "PATCH"
    "Body"        = $Body
}

#Send message
$Response = Invoke-RestMethod @Messageparams -TimeoutSec 15

I have been trying to get this to work for days now, and I have gotten nowhere. I am still getting a (400) Bad Request even though I have followed the documentation to the letter, and tried literally everything I possibly could think of. There is not much documentation or solutions on the internet, and none that I have found to work. Is there something wrong with how I have formatted my PATCH request, or is this a bug in the Microsoft Graph Files API or SharePoint Online?

Please let me know, thank you!

Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
13,050 questions
SharePoint
SharePoint
A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.
11,208 questions
PowerShell
PowerShell
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,811 questions
0 comments No comments
{count} votes

5 answers

Sort by: Most helpful
  1. Gopinath Chennamadhavuni 2,436 Reputation points
    2023-01-24T05:01:26.38+00:00

    Hi Cyrus Majidy,

    Hope you are doing well.

    As per the documentation, I am able to move the file to other folder using the MS Graph API: PATCH /drives/{drive-id}/items/{item-id}.

    with request body:

    {
      "parentReference": {
    
        "id": "{new-parent-folder-id}"
      },
      "name": "new-item-name.txt"
    }
    

    Move_file

    After successful execution of above graph call, file has been moved to below new location.

    Move_file1

    And also, I am able to open this moved file from new location without any issue.

    Move_file2

    Drive is the top-level object that represents a user's OneDrive or a document library in SharePoint.

    driveId is the unique identifier of the drive.

    itemId is the unique identifier of the file or folder.

    There are two endpoints you can use.

    If you want to move file to another folder but between the same drive (document library) you can use

    PATCH /drives/{drive-id}/items/{item-id}

    with request body.

    {
      "parentReference": {
        "id": "{new-parent-folder-id}"
      },
      "name": "new-item-name.txt"
    }
    

    drive-id is the id of the source drive.

    item-id is the id of the file you want to move.

    new-parent-folder-id is the id of the destination folder.

    If you want to move file between two drive (document libraries) you can use

    POST /drives/{driveId}/items/{itemId}/copy

    with request body

    {
      "parentReference": {
        "driveId": "{new-drive-id}",
        "id": "{new-parent-folder-id}"
      },
      "name": "new-item-name.txt"
    }
    

    drive-id is the id of the source drive.

    item-id is the id of the file you want to move.

    new-drive-id is the id of the destination drive.

    new-parent-folder-id is the id of the destination folder.

    Reference links:

    Resources:

    Move an item

    Copy an item

    Hope this helps.

    If the answer is helpful, please click Accept Answer and kindly upvote. If you have any further questions about this answer, please click Comment.


  2. Limitless Technology 44,526 Reputation points
    2023-01-24T16:59:26.5633333+00:00
    Hi. Thank you for your question and reaching out. I’d be more than happy to help you with your query.
    
    I have linked an article below that may help solve your issue. Here is what the article has to say:
    
    According to user2250152, there are two endpoints are available for use.
    
    
    To move a file to another folder while staying on the same drive (document library), use
    
    PATCH /drives/{drive-id}/items/{item-id}
    
    
    with body of the request.
    
    {
      "parentReference": {
        "id": "{new-parent-folder-id}"
      },
      "name": "new-item-name.txt"
    }
    
    
    
    For more information/reference, please see https://stackoverflow.com/questions/70948757/how-can-i-move-a-file-from-one-folder-to-another-in-sharepoint-using-graph-api 
    
    If the reply was helpful, please don’t forget to upvote or accept as answer, thank you.
    
    
    
    
    0 comments No comments

  3. Cyrus Majidy 25 Reputation points
    2023-01-26T02:04:48.7166667+00:00

    Gopinath, Limitless Technology, thank you for your answers, I appreciate it. However, I am still struggling with the same issue, I have attempted using the information in your answers, and still failed.

    I think this is a bug between Microsoft Graph Files API and SharePoint Online, because I have done exactly as described in the documentation, as well as seeking many other online resources, and I am still am receiving a 400 Bad Request error.

    This is the full code of my simplest test to move a file in SharePoint using Graph:

    Screen Shot 2023-01-25 at 10.15.49 AM

    This is the error message in PowerShell ISE, which occurs on the last line of the above code:

    ErrorMsg_01

    If I add try/catch around the response like so,

    Screen Shot 2023-01-25 at 10.15.57 AM

    I get this error description, which is not very helpful because my Content-Type is "application\json", as seen in my code.

    ErrorMsg_02

    If anyone has any ideas, has seen any errors in my code, etc., I would love to hear them.

    0 comments No comments

  4. Cyrus Majidy 25 Reputation points
    2023-02-06T13:02:43.15+00:00

    I found the answer, it was actually very simple and stupid.

    I'm sure all of your other answers work, but this is what worked for me.

    #Create the JSON object
    $Body = @{
        "parentReference" = @{
        		"id" = "{new-parent-folder-id}"
        }
        "name" = “{FileName.xslx}”       #New file name, same as before
    }
    
    $Body | ConvertTo-Json
    
    
    ##
    $uri = "https://graph.microsoft.com/v1.0/sites/{site id}/drive/items/" + $File.id   #Current item id
    
    $MessageParams = @{
        "URI"         = $uri
        "Headers"     = $Headers
        "Method"      = "PATCH"
    }
    
    #Send message
    $Response = Invoke-RestMethod @Messageparams -Body $Body -TimeoutSec 15
    

    It was to remove $Body from $MessageParams and put it in $Response like shown above. Before, I had $Body in $MessageParams.

    Thank you all for your time, I appreciate it.

    0 comments No comments

  5. RaytheonXie_MSFT 38,956 Reputation points Microsoft Vendor
    2023-02-07T05:46:39.2733333+00:00

    Hi @Cyrus Majidy,

    I'm glad to hear you solve the problem ,if you have any issue about SharePoint, you are welcome to raise a ticket in this forum.

    By the way, since the Microsoft Q&A community has a policy that "The question author cannot accept their own answer. They can only accept answers by others." and according to the scenario introduced here: Answering your own questions on Microsoft Q&A, I would make a brief summary of this thread:

    How to move a file within a SharePoint channel using Microsoft Graph Files API

    Issue Symptom:

    Graph api returns 400 Bad Request when move a file within a SharePoint channel

    Current status:

    It was to remove $Body from $MessageParams and put it in $Response like shown below

    #Create the JSON object
    $Body = @{
        "parentReference" = @{
        		"id" = "{new-parent-folder-id}"
        }
        "name" = “{FileName.xslx}”       #New file name, same as before
    }
    
    $Body | ConvertTo-Json
    
    
    ##
    $uri = "https://graph.microsoft.com/v1.0/sites/{site id}/drive/items/" + $File.id   #Current item id
    
    $MessageParams = @{
        "URI"         = $uri
        "Headers"     = $Headers
        "Method"      = "PATCH"
    }
    
    #Send message
    $Response = Invoke-RestMethod @Messageparams -Body $Body -TimeoutSec 15
    

    You could click the "Accept Answer" button for this summary to close this thread, and this can make it easier for other community member's to see the useful information when reading this thread. Thanks for your understanding!

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.