How to add all Users of a Group as Watchers of a Page or Blog
Platform Notice: Data Center Only - This article only applies to Atlassian products on the Data Center platform.
Note that this KB was created for the Data Center version of the product. Data Center KBs for non-Data-Center-specific features may also work for Server versions of the product, however they have not been tested. Support for Server* products ended on February 15th 2024. If you are running a Server product, you can visit the Atlassian Server end of support announcement to review your migration options.
*Except Fisheye and Crucible
Summary
The ability to add an entire group as a watcher of specific content is not available in Confluence at the moment. Please check the following feature requests for more details:
With that in mind, you need to add the users one by one, which is a task that can take hours to complete if the list of users is large. As we can see on the requests, customers would like to have the option of adding a group instead of individual users in those situations.
Even though there are some plugins that add this functionality, it may not be reasonable to acquire the add-on if you are only looking to this specific feature.
Solution
If your sole purpose is to add multiple users as watchers of a page or blog post, you can achieve that through the remote APIs. We will cover the options available depending on your Confluence version in the next sections:
The scripts below are provided as is and Atlassian do not offer support for them. Make sure to test them in your staging instance before running on production.
For Confluence 5.10.0 and above
On newer versions of Confluence, we can rely on the REST API to achieve that goal. To do so, please follow the steps below:
Copy the following code block and save it as rest-add-watchers-script.py
rest-add-watchers-script.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
import json import requests from time import sleep # This script is Python 2 and 3 compatible! #----------------------------------------------------------------------------- # Globals that need to be populated: # Replace with the Server Base URL of your instance, don't forget the protocol (HTTP or HTTPS) BASE_URL = "https://mycompany.com" # The username that will connect to the instance USER = "username" # The password of the username above that will be used for basic authentication PASSWD = "password" # If the group name has spaces, replace the space with %20 (e.g "this%20group") GROUP_NAME = "group-name" # The Content ID that the watchers will be added. To find it, visit the page/blog and go to '... > Page Information' # Once you are in that page, you should see the CONTENT ID in the URL on your browser as pageId= CONTENTID = "5636099" #----------------------------------------------------------------------------- # Globals that should not be changed: GROUP_MEMBERS = "/rest/api/group" ADD_WATCHER = "/rest/api/user/watch/content" def pprint(data): ''' Pretty prints json data. ''' print (json.dumps( data, sort_keys = True, indent = 4, separators = (', ', ' : '))) def get_group_members(auth): print ("\n****************************\n") print ("Group name is: "+ GROUP_NAME) url = '{base_url}{group_url}/{group}/member'.format( base_url = BASE_URL, group_url = GROUP_MEMBERS, group = GROUP_NAME) r = requests.get(url, auth = auth, verify=False) r.raise_for_status() return r.json() def main(): auth = (USER, PASSWD) info = get_group_members(auth) # info has the full JSON output, the keys are: # _links # limit # results (this is what we are looking for) # size (number of results) # start # grab only the results dict from the JSON output results = info['results'] # get the number of members total_users = info['size'] # iterate through the entries for x in range(0, total_users): url = '{base_url}{add_watcher_url}/{pageid}?username={name}'.format( base_url = BASE_URL, add_watcher_url = ADD_WATCHER, pageid = CONTENTID, name = results[x]['username']) r = requests.post( url, auth = auth, headers = { 'Content-Type' : 'application/json' } ) r.raise_for_status() print ("\n********************************************************\n") print ((results[x]['username']) +" successfully added as watcher of "+ CONTENTID) sleep(0.05) print ("\n**********************************\n") print ("Finished adding users as watchers!") if __name__ == "__main__" : main()
Edit the script you just downloaded and change the following lines only:
BASE_URL: This should be set to the server base URL of Confluence, as in the example (make sure to include the context path, if it exists):
1
BASE_URL = "https://mycompany.com"
USER: Set this variable to the username that will connect to Confluence through the REST API. It can be your normal user account.
PASSWD: Set this one to your password used to authenticate with the user above. Unfortunately, Confluence only supports basic auth for REST, so we need to explicitly pass the password.
GROUP_NAME: Set this variable to the group name that holds the users that will be added as watchers. If the group name has spaces, replace the space character with %20 (e.g "this group" should look like "this%20group").
CONTENTID: this variable must hold the content ID of the page or blog that we would like to add watchers. To find the ID, visit the page/blog and go to '... > Page Information'. Once you are in that page, you should see the CONTENT ID in the URL field of your browser as pageId=.
Execute the script as follows:
1
python rest-add-watchers-script.py
Once the message "Finished adding users as watchers!" is printed, check the Manage Watchers section of the page in Confluence to see if the users are indeed there.
ℹ️ This script is compatible with both Python 2 and 3 versions.
For Confluence 5.9.14 and below
On those versions, we need to use the deprecated XML RPC APIs as the /group endpoint was not yet available through REST API. Also, the XML RPC API does not include a similar method to list group members, so we need to fetch them from the database. Please follow the steps below to run the script correctly:
Run the following query on your database to fetch all members of a specific group:
1 2 3 4 5
SELECT user_name FROM cwd_user WHERE id IN (SELECT m.child_user_id FROM cwd_membership AS m JOIN cwd_group AS g ON m.parent_id = g.id WHERE g.group_name = '<GROUP-NAME-HERE>');
Copy the output, making sure to exclude the headers (column name), and save it on a file called userlist.txt . The file should look like this:
1 2 3 4 5
admin anotheradmin user usertwo jdoe
Copy the following code block and save it as xmlrpc-add-watchers-script.py
xmlrpc-add-watchers-script.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
# This script adds users as watchers of a specific content ID; # The list of users must be provided through a file called userlist.txt # This file must reside in the same directory where the script will be executed; # Populate the file with the output of the query below: # # SELECT user_name # FROM cwd_user # WHERE id IN (SELECT m.child_user_id FROM cwd_membership AS m # JOIN cwd_group AS g ON m.parent_id = g.id # WHERE g.group_name = '<GROUP_NAME>'); # # Make sure to remove any headers from the file, it should only contain the usernames; import xmlrpc.client # For python 2 replace the line above with: # import xmlrpclib from time import sleep #----------------------------------------------------------------------------- # Globals that need to be populated: # Replace with the Server Base URL of your instance, don't forget the protocol (HTTP or HTTPS) BASE_URL = "https://mycompany.com" # The username that will connect to the instance USER = "username" # The password of the username above that will be used for basic authentication PASSWD = "password" # The Content ID that the watchers will be added. To find it, visit the page/blog and go to '... > Page Information' # Once you are in that page, you should see the CONTENT ID in the URL on your browser CONTENTID = "5636099" #----------------------------------------------------------------------------- # Script logic start, should not be edited: s = xmlrpc.client.ServerProxy(BASE_URL + "/rpc/xmlrpc") # For python 2 replace the line above with: # s = xmlrpclib.ServerProxy(BASE_URL + "/rpc/xmlrpc") # Log in with the credentials provided through the global variables above print ("Logging in...") token = s.confluence2.login(USER, PASSWD) # List of usernames must be provided through a file since there is no method to # list members of a group in the old APIs with open("userlist.txt") as f: content = f.readlines() # Trimming whitespace characters like `\n` at the end of each line content = [x.strip() for x in content] # With the list in hands, add the users as watchers, one at a time for user in content: r = s.confluence2.watchPageForUser(token, CONTENTID, user) if r: print ("\n************************************************************\n") print (user +" successfully added as watcher of "+ CONTENTID) else: print ("\n**************************************************\n") print (user +" was already a watcher, skipping...") sleep(0.05) print ("\n**********************************\n") print ("Finished adding users as watchers!")
Put the script in the same directory as the file userlist.txt that we just created.
Edit the script you just downloaded and change the following lines only:
BASE_URL: This should be set to the server base URL of the server, as in the example (make sure to include the context path, if it exists):
1
BASE_URL = "https://mycompany.com"
USER: Set this variable to the username that will connect to Confluence through the REST API. It can be your normal user account.
PASSWD: Set this one to your password used to authenticate with the user above. Unfortunately, Confluence only supports basic auth for REST, so we need to explicitly pass the password.
CONTENTID: this variable must hold the content ID of the page or blog that we would like to add watchers. To find the ID, visit the page/blog and go to '... > Page Information'. Once you are in that page, you should see the CONTENT ID in the URL field of your browser as pageId=.
Execute the script as follows:
1
python xmlrpc-add-watchers-script.py
Once the message "Finished adding users as watchers!" is printed, check the Manage Watchers section of the page in Confluence to see if the users are indeed there.
ℹ️ This script is compatible with Python 3 only. Refer to the comments inside the code on how to make it Python 2 compatible if needed.
Was this helpful?