Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

User macro accessing content properties?

Carsten Hilber Community Leader Dec 29, 2021

I wrote a user macro to show values in a page that have been written to the content properties of the page. I have seen that the properties I can access within the template are not the same that I have been writing with the REST API, why is that?

writing the properties with REST:

https://test.confluence.localdomain/rest/api/content/75333645/property and the key/value in post (key: t-version, value: 1.2.3-SNAPSHOT)

check the properties with a get:

https://test.confluence.localdomain/rest/api/content/75333645/property/ returns:

{"results":[{"id":"75333718","key":"t-version","value":"1.2.3-SNAPSHOT",...

So my rest call was successfull and my data is stored.

If I now access the content properties in the velocity template of the macro:

ID: $content.getIdAsString()

#foreach ( $item in $content.getProperties().asList() )
$item.getName() : $item.getStringValue() ,
#end

I get the following values in the page ID 75333645:

ID: 75333645 legacy.draft.parent.id : $item.getStringValue() , macro-count.note : 48-1 , macro-create-events-published-for-version : $item.getStringValue() , macro-count.user-macro : 48-2 , sync-rev : dummy-sync-rev , macroNames : note,content-properties-makro ,

Q1: What is up with that?

Q2: How do I get to the content properties that I set via REST?

 

1 answer

1 accepted

1 vote
Answer accepted
Bill Bailey Community Leader Dec 29, 2021

Hello @Carsten Hilber , maybe a stupid question, but if the data is stored as key value pairs (per your REST call), are you using the right method to retrieve that data? When I have used a key-value map, I had to use the put and get methods.

Take a look in the example in the article I wrote that uses these methods: Getting-Started-with-User-Macros-Writing-a-Wrapper-Macro 

Carsten Hilber Community Leader Dec 30, 2021

Hi Bill, thanks for the Answer. I tried to look over your macro, good stuff that you did there! But what you're writing about are page properties, not content properties. Maybe I misunderstood the API on that and though I use stuff that don't belong together, as you say. 

The doc I used was this: https://developer.atlassian.com/server/confluence/content-properties-in-the-rest-api/

Anyway, I can always use jquery to access the rest endpoint to get the property I want, it's just not so elegant. for those who ask themselfes how you access the confluence REST API from a macro, here's some sample code:

## This makro renders content that jenkins put into content properties of the page
## @param property-key:title=property key|type=string|desc=Der Schlüssel unter dem Jenkins den Wert übermittelt

## will be used in ajax call
#set($context='$')

<div style="display:block;">
<div style="display:inline-block;">$body</div>
<div style="display:inline-block;">&nbsp;<div>
<div style="display:inline-block;" id="$paramproperty-key" >No information posted yet for property $paramproperty-key</div>
</div>

<script type="text/javascript">
var url = "/rest/api/content/"+$content.getIdAsString()+"/property/$paramproperty-key"
AJS.${context}.ajax({
url: url,
type: "GET",
contentType: "application/json",
success: function(json){
document.getElementById('$paramproperty-key').textContent = json.value
}
});
</script>

it would just be nice to know how to get to these content-properties just by navigating over the injected velocity objects...

I will accept your answer without to know that because I really like your macro blog :-) 

Bill Bailey Community Leader Dec 30, 2021

Thanks for the kind words.

The point I was trying to make is that you may need to use get and put methods as it appears that the content properies are stored as a key map (key-value pairs). So you would need to do something like

$content.properties.get($key)

For example

$content.properties.get("t-version")

Carsten Hilber Community Leader Dec 30, 2021

Ah, I see. I tried it, but had no luck. 

The ContentProperties returned by $context do not have any methods for get 

https://docs.atlassian.com/ConfluenceServer/javadoc/7.15.0/com/atlassian/confluence/content/ContentProperties.html

I'm pretty sure that the 'Rest' Content Properties and the properties returned by $content are different things.

Bill Bailey Community Leader Dec 30, 2021

Yeah, it can be frustrating at times. Not all objects are exposed to Velocity. And I have had to do a lot of research to figure out how to retrieve certain objects at times.

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
SERVER
VERSION
7.13.0
TAGS
Community showcase
Published in Confluence Cloud

🏠 Say hello to the new Confluence Home!

Hi Atlassian Community, My name is DJ Chung, and I’m a Product Manager on the Confluence Cloud team. Today, I’m excited to share a new and improved version of Home. The new Home helps you ...

1,014 views 7 42
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you