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,
check_fields = TRUE,
create_list = FALSE,
strict = FALSE,
.batch = getOption("sharepointr.batch", TRUE),
.progress = TRUE,
call = caller_env()
)
update_sp_list_items(
data,
list_name = NULL,
list_id = NULL,
sp_list = NULL,
...,
.id = "id",
allow_display_nm = FALSE,
check_fields = TRUE,
strict = FALSE,
na_fields = c("drop", "replace"),
drop_fields = c("ContentType", "Attachments"),
.batch = getOption("sharepointr.batch", TRUE),
.progress = TRUE,
call = caller_env()
)
update_sp_list_item(
...,
.data = NULL,
item_id = NULL,
sp_list_item = NULL,
.id = "id",
check_fields = TRUE,
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,
call = caller_env()
)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.- check_fields
If
TRUE(default), column names (or record field names) for the input data are matched to the fields of the list object. IfFALSE, names aren't checked and the Graph API errors for any name that isn't a list field.- create_list
If
TRUEandlist_nameis supplied, a new list is created usingdata_as_column_definition_list()to set the column definitions for the list.- strict
If
TRUE, all column names in a data frame (or field names in a list of records) must match field names in the supplied SharePoint list. IfFALSE(default), unmatched names are dropped with a message. Only used ifcheck_fields = TRUE.- .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.- 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.- 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.
- .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
strict = TRUEand any names in data to not match fields, the function errors.
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
)
}