Is it possible to upload a large number of users in ODK Central using a csv file?
We don't have anything user-facing yet, but if you are comfortable in Python, you can use the app user provisioning script at https://getodk.github.io/pyodk/examples.
The script reads names from a CSV and creates an App User for each one that isn't currently used by an active App User on the server. It also creates customized QR codes for each new App User.
You can do something similar for Web Users, but I suppose we can also provide an example if there's interest.
May you provide an example for web user? is it possible to do this using R?
Hey @dmpm !
Yep, you can definitely create web users in bulk using R!
R Code:
# required libraries
library(httr)
library(readr)
library(jsonlite)
# [edit these] odk central server url + admin credentials + path to web-users csv
odk_admin_email <- "admin-email-here"
odk_admin_pass <- "admin-password-here"
base_url <- "https://your-odk-url.com"
path_csv <- "path/to/web_users.csv"
col_name <- "column-name-for-emails-in-web_users.csv"
# reading the .csv
user_emails <- read_csv(path_csv,
show_col_types = FALSE,
progress = FALSE
)[[col_name]]
# creating the web users one by one
for(email in user_emails){
# request body, can add initial "password" or "displayName" as well if necessary.
req_body <- list(email = email)
# make a post call to odk central's rest api
response <- POST(
url = paste0(base_url, "/v1/users"),
authenticate(odk_admin_email, odk_admin_pass, type = "basic"),
content_type_json(),
body = toJSON(req_body, auto_unbox = TRUE)
)
# checking the response
if(status_code(response) == 200){
cat("Successfully created a web user:", email, "\n")
} else {
cat("Error (", status_code(response), "), could not create web user:", email, "\n")
print(fromJSON(content(response, "text"))$message)
}
Sys.sleep(3) # add 3 seconds delay between adding each user, who knows if a mail server can address the requests that rapidly?!
}
How To:
[Must] Just update the variables under "edit these", make sure the required packages are available.
[Optional] I’ve set up a 3-second delay between creating each web user - you can adjust it if needed.
[Optional] You can also provide an initial password or displayName in the request body during account creation if required, check this.
Also, we have a great R library for ODK Central, [ruODK] - definitely worth checking out! Not sure if it handles user management yet though… (@Florian_May , does it?)
Hope this helps!
Thanks for the kind shout-out
It's on the Todo list. I've started to implement the writing functions (create/update/delete) with the entity related API endpoints.
Will dedicate the next available ruODK time for User endpoints!