Batch disabling of user accounts | XM Community
Skip to main content
Solved

Batch disabling of user accounts

  • July 31, 2023
  • 5 replies
  • 82 views

h_mckim
Level 2 ●●
Forum|alt.badge.img+3

Does anyone have a good solution for batch disabling of user accounts, for example, students who had Qualtrics accounts but have graduated or are no longer enrolled?  Going forward we will be implementing auto-expiration dates for student accounts, but for now we have a huge backlog of accounts  and I really don’t want to change their status one by one.

Best answer by erooney

A while back we had the same need. To do this I navigated to Legacy Reports and then exported stats out to CSV. From this list I identified the users I wanted to disable and kept those rows and, importantly, the ‘User ID’ column. I renamed the file to users.csv and used that in a Python script.

With that CSV file in hand, I had a Python script that would go through and disable each account in the CSV file. You can also query Qualtrics via the API and get at all accounts in the brand if you wanted to. 

Before you start you will need the following: your API key and your data center. Both are listed under My Account > Qualtrics IDs.

Here’s the Python script I used. You will need to modify it and drop in your API key, data center ID and CSV file name with the User IDs from the report you downloaded. This will disable accounts whose User IDs you have in the CSV file.

I’ve also included some commented out code (at the bottom of the script) you could use to query your brand and access ALL users and use that info to determine which accounts to disable. You would still need to issue a request to the API to do that, but this gets you started if you go down that route.

All this being said, this is provided as-is. Please review any code before running in your environment.

import csv
import json
import requests

# Setting user Parameters
apiToken = "YOUR_TOKEN_HERE" # 1 See My Account > Qualtrics IDs
dataCenter = "sjc1" # 2 See My Account > Qualtrics IDs
csvFile = "mycsv.csv" # 3 Your CSV file downloaded from Legacy reports...

headers = {
"x-api-token": apiToken,
"Content-Type": "application/json"
}

# We want to SET the user account as disabled
data = {
"status": "disabled"
}
# Loop through CSV file and change user account status to inactive/disabled.
with open(csvFile, newline='') as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
userId = row['User ID'] # 3
baseUrl = "https://{0}.qualtrics.com/API/v3/users/{1}".format(dataCenter, userId)
response = requests.put(baseUrl, json=data, headers=headers)
#print(response.text)
print(row['User ID'], ' account disabled')

#
# Get list of all users in the brand.
# Use this code if you want to to go against API directly and figure out
# which accounts to disable...
#users = []
#while baseUrl is not None:
# response = requests.get(baseUrl, headers=headers)
# baseUrl = response.json()['result']['nextPage']
# users += response.json()['result']['elements']
#
# Print out list of users
#
#for user in users:
# print(user["id"],",",user["email"],",",user["username"],",",user["accountStatus"])
# # Issue request here to disable...

 

5 replies

ashleigh_quaill
Level 3 ●●●
Forum|alt.badge.img+19

I haven’t done it specifically but there is an API to update users in bulk - this would allow you to update their status to disabled. Looks like you can do up to 3,000 at a time so should get you moving through a heap of them in one go!

https://api.qualtrics.com/b1ab31823cf9e-managing-users


h_mckim
Level 2 ●●
Forum|alt.badge.img+3
  • Author
  • Level 2 ●●
  • July 31, 2023

Part of my problem is that I have not used and don’t really understand APIs.  When I look at the example for updating users at the link you shared, I don’t see how to go from that example which updates a field for 1 user, to updating a field for a long list of users.

 


Forum|alt.badge.img
  • Level 1 ●
  • Answer
  • August 1, 2023

A while back we had the same need. To do this I navigated to Legacy Reports and then exported stats out to CSV. From this list I identified the users I wanted to disable and kept those rows and, importantly, the ‘User ID’ column. I renamed the file to users.csv and used that in a Python script.

With that CSV file in hand, I had a Python script that would go through and disable each account in the CSV file. You can also query Qualtrics via the API and get at all accounts in the brand if you wanted to. 

Before you start you will need the following: your API key and your data center. Both are listed under My Account > Qualtrics IDs.

Here’s the Python script I used. You will need to modify it and drop in your API key, data center ID and CSV file name with the User IDs from the report you downloaded. This will disable accounts whose User IDs you have in the CSV file.

I’ve also included some commented out code (at the bottom of the script) you could use to query your brand and access ALL users and use that info to determine which accounts to disable. You would still need to issue a request to the API to do that, but this gets you started if you go down that route.

All this being said, this is provided as-is. Please review any code before running in your environment.

import csv
import json
import requests

# Setting user Parameters
apiToken = "YOUR_TOKEN_HERE" # 1 See My Account > Qualtrics IDs
dataCenter = "sjc1" # 2 See My Account > Qualtrics IDs
csvFile = "mycsv.csv" # 3 Your CSV file downloaded from Legacy reports...

headers = {
"x-api-token": apiToken,
"Content-Type": "application/json"
}

# We want to SET the user account as disabled
data = {
"status": "disabled"
}
# Loop through CSV file and change user account status to inactive/disabled.
with open(csvFile, newline='') as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
userId = row['User ID'] # 3
baseUrl = "https://{0}.qualtrics.com/API/v3/users/{1}".format(dataCenter, userId)
response = requests.put(baseUrl, json=data, headers=headers)
#print(response.text)
print(row['User ID'], ' account disabled')

#
# Get list of all users in the brand.
# Use this code if you want to to go against API directly and figure out
# which accounts to disable...
#users = []
#while baseUrl is not None:
# response = requests.get(baseUrl, headers=headers)
# baseUrl = response.json()['result']['nextPage']
# users += response.json()['result']['elements']
#
# Print out list of users
#
#for user in users:
# print(user["id"],",",user["email"],",",user["username"],",",user["accountStatus"])
# # Issue request here to disable...

 


h_mckim
Level 2 ●●
Forum|alt.badge.img+3
  • Author
  • Level 2 ●●
  • August 1, 2023

Thank you! That is very helpful!


brookel
Groups Administrator
Forum|alt.badge.img+10
  • Groups Administrator
  • August 1, 2023

@erooney thank you for helping answer this question! @h_mckim Please reach out if you have any other questions.