Adding users from groups to other groups in bulk

:bookmark: This guide explains how to add existing users from one or more groups to other groups using a combination of Data Explorer and bulk invites.

:person_raising_hand: Required user level: Administrator

Summary

This guide will show you how to:

  1. Create a Data Explorer query to select users from specific groups
  2. Download the query results as a CSV file
  3. Use the bulk invite feature to add these users to new groups

Requirements

  • Data Explorer plugin installed
  • Administrator access to your Discourse instance

:warning: Always create a backup before performing bulk operations on user groups.

Creating the Data Explorer query

  1. Go to Admin → Plugins → Data Explorer
  2. Create a new query
  3. Name your query (e.g., “Select users for group addition”)
  4. Paste the following SQL code into the query input:
--[params]
-- string_list :group_name
-- string_list :new_group_names
SELECT ue.email as "address@email", :new_group_names as "new group"
FROM users u
INNER JOIN group_users gu ON u.id = gu.user_id
INNER JOIN groups g on g.id = gu.group_id
INNER JOIN user_emails ue on u.id = ue.user_id
WHERE g.name IN (:group_name)
AND ue.primary = true
  1. Click “Save Changes”
  2. Reload the page if the group_name and new_group_names inputs don’t appear

Your Data Explorer screen should now look like this:

Running the query and downloading results

  1. Enter the source group name(s) in the group_name field
  2. Enter the target group name(s) in the new_group_names field
  3. Run the query to verify the output
    Query results
  4. Download the results as a CSV file by clicking the CSV button CSV button

:information_source: To select users from multiple groups, separate group names with a comma (,). To add users to multiple groups, separate group names with a semicolon (;).

Bulk inviting users to new groups

  1. Go to Admin → Send Invites → Bulk Invite
  2. Select the CSV file you downloaded
  3. Click “Bulk Invite” to process the file
    Bulk Invite confirmation

:information_source: You may receive an automated message about an invalid email address. This is normal and can be ignored, as it’s due to the CSV header being treated as data.

Verifying group additions

After the bulk invite process, check the target group(s) to ensure that the users have been added correctly.

For example, if you started with these users in the “grapes” group:

And the “fruits” group was initially empty:

After the bulk invite, the “fruits” group should now be populated:

:information_source: Users added to new groups using this method will not receive any email or notification about being added to the new group(s).

Selecting multiple groups

You can select users from multiple groups and add them to multiple groups:

  • To select users from multiple groups, separate group names with a comma (,): grapes,cherries
  • To add users to multiple groups, separate group names with a semicolon (;): fruits;vegetables

Additional resources

Last edited by @hugh 2024-07-16T03:08:28Z

Last checked by @hugh 2024-07-16T03:08:35Z

Check documentPerform check on document:
3 Likes

When using this process to bulk invite pre-existing users to a new group, do the users receive an email? Do they receive any notification?

1 Like

No, they will not receive an email or notification. I’ll add this information to the topic, thanks!

1 Like