Set/Get Page Properties from java plugin macro

Hello,

I’m trying to both set and get page properties.

In the developer documentation under “Persistence in Confluence” it does discuss using PageManager/ContentPropertyManager (which must of course be injected with P2 plugins via the constructor).

Seems straight forward. However, when I look up ContentPropertyManager in the java API reference, it says it is almost deprecated, use ContentProperties instead. The API reference for ContentProperties indicates ContentPropertyService must be injected and invoked with a JsonContentProperty object. This has been available since 5.6. I’d like to develop my solution on the most up-to-date API. Does anyone have any good examples on how to use this to set/get page properties?

I tried the following:

Per “Powerful new search and property storage APIs in Confluence 5.7” I added:

public class setProperty implements Macro {

@ComponentImport
private ContentPropertyService contentPropertyService;
@ComponentImport
private ContentService contentService;

@Autowired
public setProperty(ContentPropertyService contentPropertyService, ContentService contentService) {
	this.contentPropertyService = contentPropertyService;
	this.contentService = contentService;
}

public String execute(Map<String, String> parameters, String body, ConversionContext conversionContext) throws MacroExecutionException {
	
	Content content = contentService.find().withId(ContentId.valueOf("background")).fetchOneOrNull();

	if (content != null ) {
		JsonContentProperty property = JsonContentProperty.builder()
				.content(content)
				.key("background")
				.value(new JsonString("{\"background\":\"This is my test background property\"}")).build();
		contentPropertyService.create(property);
	}

I’m trying to create a new page property called “background”. This is just throwing the following exception:

[INFO] [talledLocalContainer] com.atlassian.confluence.api.service.exceptions.BadRequestException: Can’t parse as a ContentId: background

Any help on how to use this API would be much appreciated!

Thanks!

I think the issue is that

Content content = contentService.find().withId(ContentId.valueOf("background")).fetchOneOrNull();

should reference the page that has the property on it. You can use https://docs.atlassian.com/confluence/5.10.5/com/atlassian/confluence/content/render/xhtml/ConversionContext.html#getEntity-- on the ConversionContext that is passed in. From that you can get the contentId: https://docs.atlassian.com/confluence/5.10.5/com/atlassian/confluence/core/ContentEntityObject.html#getContentId--

Thanks Daniel! I was able to suppress the exceptions with:

		ContentEntityObject contentEntityObject = conversionContext.getEntity();
		Content content = contentService.find().withId(contentEntityObject.getContentId()).fetchOneOrNull();

Any thoughts on how to read the property from the Content object?

I haven’t’ used it but on the contentEntityObject there is https://docs.atlassian.com/confluence/5.10.5/com/atlassian/confluence/core/ContentEntityObject.html#getProperties--

/Daniel

Thanks! I had used that API in other applications and it works here too.

My final working solution based on the ConversionContext:

public String execute(Map<String, String> parameters, String body, ConversionContext conversionContext) throws MacroExecutionException {
		String name = parameters.get("Name");
		ContentProperties contentProperties = conversionContext.getEntity().getProperties();
		contentProperties.setStringProperty(name, body);
        contentProperties.getStringProperty(name);
}