Graph API: SPO Move/ Copy Targets


Microsoft 365 has a large offering of services. To script and interact with many of them, the Microsoft Graph API utilizes a central endpoint, with dozens of REST APIs, to work with files, calendars, mail, etc. Although not currently as robust as some of the legacy on-prem PowerShell modules, the Graph resources and their APIs are continuously being improved upon. Additionally, the supporting Microsoft documentation is always being polished. Unfortunately, until the docs mature a bit more, newcomers can often be tripped up with some of the smaller things.

An example of this, working with [SharePoint Online] files and needing to shuffle them around. Working with the Microsoft Graph API, SPO document libraries are referred to as drives, and often enough, files need to transition between drives. But are these files being moved, or are they being copied? And is this happening within the same site, or across SPO sites?


Note that each API request requires a different HTTP method. If needing to move a file, then use PATCH. If needing to copy a file, then use POST. Pretty easy, but the target drive GUID is easy to forget. This parameter was optional when copying/ moving files within the same site, but is definitely required when working with one or more SPO site:

$listOf += @{
    "id" = "1"
    "method" = "POST"
    "url" = "/sites/$($guidOfSourceSite)/drives/$($guidOfSourceDrive)/items/$($guidOfTargetFile)/copy"
    "headers" = @{
        "Content-Type" = "application/json"
    }
    "body" = @{
        "name" = "$($nameOfTargetFile)"
        "parentReference"= @{
            "driveId" = "$guidOfTargetDrive"
            "id" = "$guidOfTargetFolder";
        }
        "@microsoft.graph.conflictBehavior" = "replace"
    }
}

Conclusion:
Don’t forget to include the target site drive GUID when working with more than one SharePoint Online site. For consistency, and best practice, it is probably easier to just always include it…

“Love is progress; hate is expensive.”

Esau Jenkins

#BlackLivesMatter

Leave a comment