Create or update list items
Usage
create_sp_list_items(
data,
list_name = NULL,
list_id = NULL,
sp_list = NULL,
...,
allow_display_nm = FALSE,
.id = "id",
site_url = NULL,
site = NULL,
drive_name = NULL,
drive_id = NULL,
drive = NULL,
unmatched_fields = c("drop", "error", "keep"),
coerce = c("basic", "asis", "geometry"),
validate = c("none", "warn", "error"),
create_list = FALSE,
.batch = getOption("sharepointr.batch", TRUE),
.progress = TRUE,
check_fields = deprecated(),
strict = deprecated()
)
update_sp_list_items(
data,
list_name = NULL,
list_id = NULL,
sp_list = NULL,
...,
.id = "id",
allow_display_nm = FALSE,
unmatched_fields = c("drop", "error", "keep"),
coerce = c("basic", "asis", "geometry"),
validate = c("none", "warn", "error"),
na_fields = c("drop", "replace"),
drop_fields = c("ContentType", "Attachments"),
.batch = getOption("sharepointr.batch", TRUE),
.progress = TRUE,
check_fields = deprecated(),
strict = deprecated()
)
update_sp_list_item(
...,
.data = NULL,
item_id = NULL,
sp_list_item = NULL,
.id = "id",
unmatched_fields = c("drop", "error", "keep"),
coerce = c("basic", "asis", "geometry"),
validate = c("none", "warn", "error"),
na_fields = c("drop", "replace"),
drop_fields = c("ContentType", "Attachments"),
list_name = NULL,
list_id = NULL,
sp_list = NULL,
site_url = NULL,
site = NULL,
drive_name = NULL,
drive_id = NULL,
drive = NULL,
.multi_fields = NULL,
.quiet = FALSE,
check_fields = deprecated()
)Arguments
- data
Required. A data frame or a list of named lists (one record per item) to import as items to the supplied or identified SharePoint list. If data is an sf object, the geometry column is coerced to text using
sf::st_as_text(). Forupdate_sp_list_items(), each record must include an.idelement anddatacan also be a single named list record for one item. Unlike a data frame, any field missing from a record is left unchanged, even whenna_fields = "replace". Forcreate_sp_list_items(), wrap a single record in a list (e.g.list(record)) anddatamust be a data frame ifcreate_list = TRUE.- list_name, list_id
SharePoint List name or ID string.
- sp_list
A
ms_listobject. If supplied,list_name,list_id,site_url,site,drive_name,drive_id,drive, and any additional parameters passed to...are all ignored.- ...
Additional parameters passed to
get_sp_site()orMicrosoft365R::get_sharepoint_site().- allow_display_nm
If
TRUE, allow data to use list field display names instead of standard names. Note this requires a separate API call so may result in a slower request. DefaultFALSE.- .id
Name of the column in
data(or the element in each record ifdatais a list) with item ID values. Defaults to"id". Item IDs must be whole numbers or non-empty strings and are checked before any items are updated. Forcreate_sp_list_items(),.idis only used to keep the ID column name from being replaced whenallow_display_nm = TRUE. Forupdate_sp_list_item(),.idis used to getitem_idfrom.dataifitem_idisn't supplied.- 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.
- site
A
ms_siteobject. Ifsiteis supplied,site_url,site_name, andsite_idare ignored.- drive_name, drive_id
SharePoint Drive name or ID passed to
get_drivemethod for SharePoint site object.- drive
A
ms_driveobject. Ifdriveis supplied,drive_nameanddrive_idare ignored.- unmatched_fields
What to do with column names in a data frame (or field names in a list of records) that don't match field names in the supplied SharePoint list. One of:
"drop"(default): drop unmatched names with a message. An error if no names match."error": an error if any name doesn't match."keep": names aren't checked and the Graph API errors for any name that isn't a list field.
- coerce
How to convert values in
datato match the list column types. One of:"basic"(default): convert numbers, logical values, and factors to text for text and choice columns; convert text to numbers for number and currency columns; and convert whole numbers (or text) to integers for theLookupIdfields of lookup and person columns. Values that can't be converted are left unchanged with a warning. Data withsfgeometry columns is an error."asis": send values unchanged."geometry": do everything in"basic"and convertsfgeometry columns to well-known text (WKT) withsf::st_as_text().
Coercion changes types but doesn't check values. Use
validateto check values. See the section on single item functions for the cost of coercion withupdate_sp_list_item().- validate
What to do with values that don't match the list column definitions, after values are coerced. One of:
"none"(default): don't check values."warn": warn and send the items."error": error before any items are sent.
Values are checked for text longer than the column
maxLength, numbers outside the column minimum and maximum, choices that aren't in a choice column (unless the column allows text entry), and missing values in required columns that have no default value. Missing required values are checked for new items and for updates that replace values withNA(na_fields = "replace"), since other fields are left unchanged. Values that don't match are reported together (up to 10). Missing required values are always a warning (even withvalidate = "error") since SharePoint creates items without them. Field names are checked withunmatched_fields.- create_list
If
TRUEandlist_nameis supplied, a new list is created usingdata_as_column_definition_list()to set the column definitions for the list.- .batch
If
TRUE(default), items are sent with Microsoft Graph$batchrequests (up to 20 items per request), which is much faster than a separate request for each item. Requests throttled by the Graph API or failing with a temporary error (e.g. "Database is read only") are retried up to 5 times after the requested delay (or an increasing delay of up to 60 seconds). If any items fail, the remaining items are still sent and the error lists the failed items with the HTTP status. IfFALSE, a separate request is sent for each item and the first failed item is an error. Useoptions(sharepointr.batch = FALSE)to change the default for the session. In either case, requests are sent in parallel ifmirai::daemons()are set (seepurrr::in_parallel()). Forcreate_sp_list_items(), items sent in a$batchrequest (or in parallel) may be created in a different order thandata, so new item IDs may not follow the order ofdata. Use.batch = FALSE(without daemons) if item IDs must follow the order ofdata.- .progress
Whether to show a progress bar. Use
TRUEto turn on a basic progress bar, use a string to give it a name, or see progress_bars for more details.- check_fields, strict
Use
unmatched_fieldsinstead:check_fields = FALSEis"keep"andstrict = TRUEis"error".- na_fields
How to handle
NAfields in input data. One of"drop"(removeNAand empty fields, e.g. a multi-select value ofcharacter(0), before updating list items, leaving existing values in place) or"replace"(overwrite existing list values with new replacement NA values or, for multi-value fields, an empty selection).- drop_fields
Column names to drop from
dataeven if they are listed as editable fields. Defaults toc("ContentType", "Attachments")- .data
A list or data frame with fields to update.
- item_id
A SharePoint list item id. Either
item_idorsp_list_itemmust be provided but not both.- sp_list_item
Optional. A SharePoint list item object to update. The column definitions for the list are requested to check field names and coerce values (see the section on single item functions).
- .multi_fields
Optional. Names of multi-value (Collection) fields, such as multi-select choice columns, that should always be sent as an array with an
"@odata.type"annotation. IfNULLandsp_listis available, field names are found from the list column definitions. Otherwise, only fields with a length other than 1 are treated as multi-value fields.- .quiet
If
TRUE, don't show a progress message for the update. Used byupdate_sp_list_items()when updating items one at a time.
Value
create_sp_list_items() and update_sp_list_items() invisibly
return the input data, unmodified (even if data is empty).
Details
Validation of data with with create_sp_list_items()
The handling of item creation when column names in data do not match the
fields names in the supplied list includes a few options:
If no names in data match fields in the list, the function errors and lists the field names.
If all names in data match fields in the list the records are created. Any fields that do not have corresponding names in data remain blank.
If any names in data do not match fields in the list, by default, those columns are dropped before adding items to the list.
If
unmatched_fields = "error"and any names in data do not match fields, the function errors.
Single item functions
create_sp_list_items() and update_sp_list_items() get the list column
definitions once and use them to check field names (unmatched_fields) and
coerce values (coerce) for every item. update_sp_list_item() and
create_sp_list_item() work on one item at a time so:
If you supply
sp_list(anditem_id), the column definitions are requested from the Graph API for every call. Looping over items adds one extra request per item. To avoid the request, setcoerce = "asis",unmatched_fields = "keep",validate = "none", and supply.multi_fields(forcreate_sp_list_item(), set.coerce = "asis"and.validate = "none").If you supply
sp_list_item(a list item object) toupdate_sp_list_item(), the column definitions are requested for the item's list (ams_listobject is created from the item's site and list IDs) so the same extra request is made for every call, and the same arguments avoid it.
Use the plural functions to update more than one item (they also send
requests in batches), or coerce and check the data first and send it with
coerce = "asis".
Examples
sp_list_url <- "<SharePoint List URL with a Name field>"
if (is_sp_url(sp_list_url)) {
create_sp_list_items(
data = data.frame(
Name = c("Jim", "Jane", "Jayden")
),
list_name = sp_list_url
)
}