These functions copy or move items in a SharePoint document library. Their
interface is based on fs::file_copy(), fs::file_move(), and
fs::dir_copy():
sp_item_copy()copies files. Usesp_dir_copy()to copy folders.sp_item_move()moves (or renames) files or folders.sp_dir_copy()andsp_dir_move()copy or move folders and their contents.
As with {fs}, if new_path is an existing folder, each item is placed
inside that folder and keeps its name. Otherwise, new_path is the full
path for the new item and its parent folder must already exist. A new_path
that ends in "/" must be an existing folder.
new_path can be a path relative to the drive that holds the item, or a
SharePoint folder or document library URL. Items can be copied to another
drive or site, but they can only be moved within the same drive.
Usage
sp_item_copy(
path,
new_path,
...,
overwrite = FALSE,
wait = TRUE,
drive_name = NULL,
drive_id = NULL,
drive = NULL,
site_url = NULL,
call = caller_env()
)
sp_item_move(
path,
new_path,
...,
overwrite = FALSE,
drive_name = NULL,
drive_id = NULL,
drive = NULL,
site_url = NULL,
call = caller_env()
)
sp_dir_copy(
path,
new_path,
...,
overwrite = FALSE,
wait = TRUE,
drive_name = NULL,
drive_id = NULL,
drive = NULL,
site_url = NULL,
call = caller_env()
)
sp_dir_move(
path,
new_path,
...,
drive_name = NULL,
drive_id = NULL,
drive = NULL,
site_url = NULL,
call = caller_env()
)Arguments
- path
A character vector of file or folder paths, or SharePoint file or folder URLs. A relative path must not include the drive name.
pathcan also be ams_drive_itemobject or a list ofms_drive_itemobjects.- new_path
A character vector of new paths, or SharePoint folder or document library URLs.
new_pathmust be length 1 or the same length aspath. A relativenew_pathis in the same drive as the item it is paired with.- ...
Arguments passed on to
get_sp_drivedrive_urlA SharePoint Drive URL to parse for a Drive name and other information. If
drive_nameis a URL, it is used asdrive_url.default_drive_nameDrive name string used only if input is a document URL and drive name is not part of the URL. Defaults to
getOption("sharepointr.default_drive_name", "Documents")cacheIf
TRUE, cache drive to a file usingcache_sp_drive().refreshIf
TRUE, get a new drive even if the existing drive is cached as a local option. IfFALSE, use the cachedms_driveobject if it exists.cache_fileFile name for cached drive or site. Default
NULL.siteA
ms_siteobject. Ifsiteis supplied,site_url,site_name, andsite_idare ignored.
- overwrite
If
FALSE(default), an error is returned if an item already exists at the new path. IfTRUE, an existing file is replaced. SharePoint can't replace a folder, sosp_dir_copy()works likefs::dir_copy()instead: ifnew_pathis an existing folder andoverwrite = TRUE, the contents ofpathare copied intonew_path(replacing any files with the same name).sp_dir_move()has nooverwriteargument because a folder can't be moved to an existing folder.- wait
If
TRUE(default), wait until each copy is complete. See details.- drive_name, drive_id
SharePoint drive name or ID.
- drive
A
ms_driveobject. If drive is supplied,drive_name,site_url, and any additional parameters passed to...are ignored.- site_url
A SharePoint site URL in the format "https://[tenant name].sharepoint.com/sites/[site name]". Any SharePoint item or document URL can also be parsed to build a site URL using the tenant and site name included in the URL.
- call
The execution environment of a currently running function, e.g.
caller_env(). The function will be mentioned in error messages as the source of the error. See thecallargument ofabort()for more information.
Value
Invisibly returns a character vector with the new path of each item, relative to the drive it was copied or moved to.
Details
Copying is asynchronous
Microsoft Graph copies items in the background. If wait = TRUE (the
default), sp_item_copy() and sp_dir_copy() check the status of each copy
until it is complete, so they can report an error if a copy fails. Large
folders can take a while to copy. If wait = FALSE, the copy may still be
running when the function returns, and a failed copy isn't reported.
Examples
dir_url <- "<link to SharePoint directory>"
if (is_sp_url(dir_url)) {
drive <- get_sp_drive(drive_name = dir_url)
# Copy a file into an existing folder
sp_item_copy("Reports/report.docx", "Archive", drive = drive)
# Rename a file
sp_item_move(
"Reports/report.docx",
"Reports/report-2026.docx",
drive = drive
)
# Copy a folder to a new folder named "Reports-backup"
sp_dir_copy("Reports", "Reports-backup", drive = drive)
# Move a folder into the folder at a SharePoint URL
sp_dir_move("Reports-backup", dir_url, drive = drive)
}