List, get, and download versions of a SharePoint item
Source:R/sp_item_versions.R
list_sp_item_versions.Rdlist_sp_item_versions() lists the versions of a SharePoint file (a
driveItem) and when and by whom they were saved, which makes it possible to
check the recent edit history of a file such as an Excel workbook.
get_sp_item_version() returns a single version, and
download_sp_item_version() saves the content of a past version of the file.
Usage
list_sp_item_versions(
...,
sp_item = NULL,
n = NULL,
modified_between = NULL,
item_cols = c("id", "none", "summary"),
version_cols = c("properties", "all"),
as_data_frame = TRUE
)
get_sp_item_version(
version_id,
...,
sp_item = NULL,
item_cols = c("id", "none", "summary"),
version_cols = c("properties", "all"),
as_data_frame = TRUE
)
download_sp_item_version(
version_id,
...,
sp_item = NULL,
dest = NULL,
overwrite = FALSE
)Arguments
- ...
Arguments passed on to
get_sp_itempathA SharePoint file URL or the relative path to a file located in a SharePoint drive. If input is a relative path, the string should not include the drive name. If input is a shared file URL, the text "Shared " is removed from the start of the SharePoint drive name by default. If file is a document URL, the
default_drive_nameargument is used as thedrive_nameand theitem_idis extracted from the URL.item_idA SharePoint item ID passed to the
itemidparameter of theget_itemmethod forms_driveobjects.item_urlA SharePoint item URL used to parse the item ID, drive name, and site URL.
drive_name,drive_idSharePoint drive name or ID.
driveA
ms_driveobject. If drive is supplied,drive_name,site_url, and any additional parameters passed to...are ignored.propertiesIf
TRUE, use theget_item_propertiesmethod and return item properties instead of the item.site_urlA 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.
- sp_item
A
ms_drive_itemobject for a SharePoint file. Additional parameters passed to...are ignored ifsp_itemis supplied.- n
Maximum number of versions to return, starting with the newest (latest). Defaults to
NULL(all versions). Applied aftermodified_between.- modified_between
A length 2 vector of dates or date-times with the start and end of the period to return versions for (inclusive, as in
dplyr::between()). UseNAfor an open start or end. ADateend includes that whole day. Character values are parsed as UTC. If the lubridate package is installed, alubridate::interval()is also accepted.- item_cols
Item columns to add to the returned data frame:
"id"(default) addsitem_idanddrive_id,"summary"also addsitem_nameanditem_web_url, and"none"adds no item columns.- version_cols
Version columns to return:
"properties"(default) returnsversion_id,version,last_modified,last_modified_by,last_modified_by_email,last_modified_by_id, andsizeand"all"also returns everything else the API provides, includingdownload_url. The download URL includes a short-lived access token so is not returned by default. Column names use snake case (unlike the Graph API property names thatsp_dir_info()returns).- as_data_frame
If
TRUE(default), return a data frame. IfFALSE, return the response from the API as a list.- version_id
Version ID, e.g.
"3.0", as returned bylist_sp_item_versions().- dest
Path to save the file to. Defaults to the name of the item with the version ID added before the file extension (e.g.
"book_v3.0.xlsx") in the working directory.- overwrite
If
TRUE, overwrite an existing file atdest.
Value
For list_sp_item_versions(), a data frame with one row for each
version (newest first) if as_data_frame = TRUE or a list with the
versions in value otherwise. For get_sp_item_version(), a one row data
frame or a list. For download_sp_item_version(), the path to the
downloaded file (invisibly).
Details
These functions use the do_operation method for a ms_drive_item object
directly. The intent is to add comparable methods back to the Microsoft365R
package which may include changes to the functionality and output of these
functions.
Versions are only available if versioning is enabled for the document
library (the default for SharePoint). The Graph API doesn't document the order
it returns versions in. In testing with 60 files, it always returned
versions from newest to oldest, so n is sent to the API as $top to limit
the size of the response for files with many versions and returns the
newest versions. The versions that are returned are sorted by last modified
time. The API does not support filtering versions by date, so if
modified_between is used, all versions are requested and n is applied
afterward. SharePoint may combine edits made close together into a single
version.
Version IDs
Version IDs (e.g. "3.0") are only unique within a single item. Use
item_cols = "id" (the default) to keep the item ID alongside the version
ID if you combine the results for more than one item.
See the Microsoft Graph documentation for listing versions and the driveItemVersion resource.
Examples
if (FALSE) { # \dontrun{
# List the five most recent versions of a file
list_sp_item_versions(item_url = "<SharePoint file URL>", n = 5)
# Versions saved in October 2026
list_sp_item_versions(
item_url = "<SharePoint file URL>",
modified_between = c("2026-10-01", "2026-10-31")
)
} # }