Update title of page using event listeners
Asked by Dan27:

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]: CNTRLM2020-04-22 06:11:33,279 WARN [runner.ScriptBindingsManager]: with label SRS2020-04-22 06:11:33,280 WARN [runner.ScriptBindingsManager]: add 'SRS-' to the title2020-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


Answers:
author: Nishant Paraskar
body: 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

<br />

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!!!!


author: Lee Wonnacott
body: 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

comments: author: Dan27
body: Hi [@Lee Wonnacott](/t5/user/viewprofilepage/user-id/2958442)

I still not working...

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

Do you have any other ideas?

Daniel


author: Lee Wonnacott
body: 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


author: Dan27
body: Hi [@Lee Wonnacott](/t5/user/viewprofilepage/user-id/2958442)

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


Tags: confluence-server

server