Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

How to get all drafts for user using scriptrunner

Elena Oleksenko
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
October 6, 2022

Hello, 

I'm trying to get the list of drafts for given user, using groovy script in scriptrunner console.

Here is the code:

 

import com.atlassian.confluence.pages.DraftManager

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

import com.atlassian.user.User

import com.atlassian.confluence.user.UserAccessor



def userManager = ComponentLocator.getComponent(UserAccessor)

def user = userManager.getUserByName("test_user")

def draftManager =ComponentLocator.getComponent(DraftManager)

def allDrafts = draftManager.findDraftsForUser(user)


return allDrafts

The problem is the result of this script is empty list, though this user has one draft. The result is the same for other users. 

I also tried to use getDraft(long draftId) method, using the correct ID for existing draft, but I keep receiving null as the result. Seems like I can't access any draft in Confluence using DraftManager. 

Any ideas why this happens and how can I access list of  user`s drafts?

 

1 answer

1 vote
Ram Kumar Aravindakshan _Adaptavist_
Community Champion
July 5, 2023

Hi @Elena Oleksenko

I'm not sure if your approach will work as expected. However, you can try the sample code below:-

import com.atlassian.confluence.pages.PageManager
import com.atlassian.confluence.spaces.SpaceManager
import com.atlassian.confluence.user.UserAccessor
import com.atlassian.sal.api.component.ComponentLocator

def spaceManager = ComponentLocator.getComponent(SpaceManager)
def pageManager = ComponentLocator.getComponent(PageManager)
def userAccessor = ComponentLocator.getComponent(UserAccessor)

def spaces = spaceManager.allSpaces
def list = []
def currentUser = userAccessor.getUserByName('admin')

spaces.each{ space ->
def pages = pageManager.getPages(space, true)

pages.each{ page ->
if(page.DRAFT == 'draft' && page.creatorName == currentUser.name) {
def type = page.type
def date = page.lastModificationDate
list << [space.name, page.creatorName, page.title, type, date]
}
}
}
list

Please note that the sample code above is not 100% exact to your environment. Hence, you will need to make the required modifications.

I hope this helps to solve your question. :-)

Thank you and Kind regards,
Ram 

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events