Script - add-watcher (groovy)

Updated for Confluence 4.x/5.x 

This groovy script, implemented as a user macro, is used to add users as a watcher to a page.This is really handy in combination with scaffolding. I use it in a form where a manager can assign resources to a project request (which is a page in Confluence) and those users are automatically subscribed to the page.

Macro Name

add-watcher

DescriptionAdd a user as a watcher to the current page

Macro body processing

No macro body

## param0 is the user name (id) to add
 
#if ($param0 && $param0.length() > 0)
#set ($wiki = "{groovy:userName=${param0}}
import com.atlassian.spring.container.ContainerManager;
import com.atlassian.confluence.mail.notification.NotificationManager;

// Get the default Notification Manager
def notificationManager = (NotificationManager) ContainerManager.getComponent('notificationManager');
 
// Get the current page object
def page = context.getEntity();
 
// Get a handle on the user
def user = userAccessor.getUser(userName);
 
// Is the user already watching this page or space?
boolean isWatching = notificationManager.isUserWatchingPageOrSpace(user, page.getSpace(), page)
if (user && page && !isWatching) {
    notificationManager.addPageNotification(user, page);
    out.println('Subscribed User: <b>' + user.getFullName() + '</b><br />');
} else if (user == null) {
    out.println('User not found: <b>' + userName + '</b><br />');
} else if (isWatching) {
    out.println('User already watching: <b>' + userName + '</b><br />');
}
{groovy}")

$action.getHelper().renderConfluenceMacro($wiki)
#end

I have a live-template based form with a select list of users to assign:

{list-data:AssignedManager|width=200px|type=select|multiple=true|height=10em}
  {user-options:users=stepper,user1,user2,user3,user4}
{list-data}

I then have a report that iterates on the selections in the list to subscribe the selected users:

{report-block:injected=true}
  {local-reporter:data:AssignedManager}
  {report-body}{add-watcher:%reference:value > user:name%}{report-body}
{report-block}

Log a request with our support team.

Confluence®, Jira®, Atlassian Bamboo®, Bitbucket®, Fisheye®, and Atlassian Crucible® are registered trademarks of Atlassian®
Copyright © 2005 - 2024 Appfire | All rights reserved. Appfire™, the 'Apps for makers™' slogan and Bob Swift Atlassian Apps™ are all trademarks of Appfire Technologies, LLC.