Forums

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

Help with the API Metadata for Confluence (update field value)

Roman Kersky
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.
December 17, 2019

Hi, I am writing a script on Scriptrunner that returns a value from a metadata field.

Please tell me how can I update the value of this metadata field using the saveContentMetadataValue method ?

I can not find an example ;(

import com.atlassian.sal.api.component.ComponentLocator
import com.atlassian.confluence.pages.templates.PageTemplateManager
import com.atlassian.confluence.pages.PageManager
import com.atlassian.confluence.spaces.SpaceManager
import com.atlassian.confluence.setup.settings.SettingsManager
import com.onresolve.scriptrunner.runner.customisers.WithPlugin
import com.onresolve.scriptrunner.runner.ScriptRunnerImpl
import com.communardo.confluence.metadata.service.MetadataManager
import com.communardo.confluence.metadata.*

@WithPlugin("com.communardo.confluence.metadata")

def pageTemplateManager = ComponentLocator.getComponent(PageTemplateManager)
def spaceManager = ComponentLocator.getComponent(SpaceManager)
def pageManager = ComponentLocator.getComponent(PageManager)
def settingsManager = ComponentLocator.getComponent(SettingsManager)

def meta = ScriptRunnerImpl.getOsgiService(MetadataManager)

def sampleSpaceKey = "DRAFT"
def sampleSpace = spaceManager.getSpace(sampleSpaceKey)
def samplePage = pageManager.getPage(561906838)

def ceo = samplePage.getContentEntityObject()

MetadataField sets = meta.loadMetadataFieldByKey("metadatafield.itdoctechwriter",sampleSpace)
DataObject q = meta.loadContentMetadataValue(ceo, sets)

return q

image.png

1 answer

1 vote
Sandro Herrmann [Communardo]
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.
March 16, 2020

Hi Roman,

so the API tells us to use: 

public void saveContentMetadataValue(ContentEntityObject ceo, MetadataField metadataField,
DataObject metadataValue)

 

You already used the correct method to retrieve the Dataobject from a page. The output shows us that it is an array of Confluence users. What if you try to get a user from confluence and add it to this array and then write it back: 

 

meta.saveContentMetadataValue(ceo, metadataField, yourModifiedUserArray)

 

This should work.

Roman Kersky
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.
March 17, 2020

Thank you.

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events