Update title of page using event listeners

Dan27 April 22, 2020

Hi,

 

I wrote a script in event listeners that add to the title some text if a spesific label was added.

In the log of this script it look like the title was changed, but in the page itself the title stayed the same..

This is my script:

import com.atlassian.confluence.event.events.content.page.PageEvent
import com.atlassian.confluence.pages.Page
import com.atlassian.event.Event
import com.atlassian.sal.api.component.ComponentLocator
import com.atlassian.confluence.pages.Page
import com.atlassian.confluence.event.events.label.LabelAddEvent
import com.atlassian.confluence.core.DefaultSaveContext
import com.atlassian.confluence.event.events.user.UserCreateEvent
import com.atlassian.confluence.pages.Page
import com.atlassian.confluence.pages.PageManager
import com.atlassian.confluence.spaces.SpaceManager
import com.atlassian.confluence.user.ConfluenceUser
import com.atlassian.sal.api.component.ComponentLocator
import groovy.xml.MarkupBuilder

def labelEvent = event as LabelAddEvent

def page = labelEvent.getLabelled() as Page

log.warn page.getContentId()

def pageManager = ComponentLocator.getComponent(PageManager)

def space = page.getSpace();
log.warn space.getKey().toString();
if(space.getKey().toString()=='SPACE_NAME')
{
def pageLabels = page.getLabels()
def srs_lbl = 'srs'

if(pageLabels.toString().contains(srs_lbl)){
log.warn "with label SRS"

if(page.getTitle().startsWith('SRS-'))
{
log.warn "already has 'SRS-' in title"
return;
}
else
{
// add a prefix to the page's title
page.setTitle('SRS- ' + page.getTitle())
log.warn "add 'SRS-' to the title"
}

}
}

log.warn page.getTitle()
return

 

 

This is the log:

2020-04-22 06:11:33,276 WARN [runner.ScriptBindingsManager]: ContentId{id=343587177}
2020-04-22 06:11:33,278 WARN [runner.ScriptBindingsManager]: CNTRLM
2020-04-22 06:11:33,279 WARN [runner.ScriptBindingsManager]: with label SRS
2020-04-22 06:11:33,280 WARN [runner.ScriptBindingsManager]: add 'SRS-' to the title
2020-04-22 06:11:33,287 WARN [runner.ScriptBindingsManager]: SRS- Test please ignore

 

What's happening? Can someone help me with it? Maybe I need to reindex this page\Update it's values?

 

Daniel

2 answers

0 votes
Nishant Paraskar August 10, 2021

Tried to use this 

pageManager.saveContentEntity(page, null)

 

Whole code -->

import com.atlassian.confluence.pages.Page
import com.atlassian.confluence.pages.PageManager

import com.atlassian.sal.api.component.ComponentLocator

 

import com.atlassian.confluence.spaces.SpaceManager

long temp = 65613 as Long
def spaceManager = ComponentLocator.getComponent(SpaceManager)
def myspace = spaceManager.getSpace("FS") // spave key
def pageManager = ComponentLocator.getComponent(PageManager)
def page = pageManager.getPage(65613) // get page from id


log.error(page.getTitle())
page.setTitle(page.getTitle().toString() + " Updated title") // updated tile
log.error(page.getTitle())

pageManager.saveContentEntity(page, null) // final update step

 

It will work for you .Keep learning!!!!

0 votes
Lee Wonnacott
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
April 29, 2020

Hi Daniel! 

If you add the following line to your script after executing the page.setTitle() method do you see the page's title updated successfully? 

pageManager.saveContentEntity(page, DefaultSaveContext.SUPPRESS_NOTIFICATIONS)

Let me know if that works! 

Thanks,
Lee

Dan27 April 30, 2020

Hi @Lee Wonnacott 

I still not working...

The logs are looking the same, I added this line after the setTitle.

 

Do you have any other ideas?

Daniel

Lee Wonnacott
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
May 4, 2020

Hi Daniel, 

I've been testing this further and can confirm that I was able to successfully update the title of the page, through an Add Lable event, without making any changes to your script. 

One thing to note was that the page needed to be refreshed before I could see any changes applied, presumably, after adding the 'srs' label and refreshing the page you see no immediate change? 

For reference, I have tested this using the latest version of ScripRunner for Confluence as well as a couple of older versions and within each version, the script worked as expected. 

Thanks, 
Lee

Dan27 May 4, 2020

Hi @Lee Wonnacott 

I refreshed the page a lot of times, and some pages I tried to update weeks ago are still stay the same (without the new title).

Maybe something in my script aren't okay? (I added your line after :

page.setTitle('SRS- ' + page.getTitle())

in the old script).

 

The logs are the same..

 

Thank you,

Daniel

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events