How to get macro id in dynamic content macro custom editor?

We have a sequence diagram macro for Confluence with a growing user base. Recently we are working on Export to PDF support.

One of the strength of our app is it doesn’t send user data outside of Confluence. We want to maintain that. To support PDF rendering, we are thinking of generating image attachment when user saves macro content. The attachment needs to be named with macro id, so that exporting to PDF can use it for static content.

After exploring the documentation we can’t find how to get macro id in a dynamic content macro. Can someone advise on this?

1 Like

I know this is old, but I just found this as a first result when googling “confluence cloud get macro id” so I thought I’d just leave this here:

AP.context.getContext(function(context) {
    console.log(context.confluence.macro.id);
});

An alternative would be to pass the macro.id into your URL in your descriptor:

"dynamicContentMacros": [
      {
          "url": "/your-macro?macroId={macro.id}",
          ...
      }
]
3 Likes

The connect API returns undefined for context.confluence in the editor. Looks like getContext() only returns {license: {active: true}}.

The url parameter is also empty on the editor page.

"dynamicContentMacros": [
      {
          "url": "...",
          "editor": {
              "url": "/your-editor?macroId={macro.id}",
          },
      },
]

Am I missing something?

@danielkleinert
That indeed is the case. Custom editor seems pretty useless without an option to get macro id.

Has there been any update on this. In terms of getting the macro id?

@dmorrow would you be able to help here? I’m new to Confluence Cloud development, and I’m getting blocked by this issue as well.

I’m not able to get macro.id neither through Javascript API, or any other means. For example I was trying to use context parameters in editor url, but with no luck.

Thanks!

Hi @danielkleinert,

You are right. I’ve created CONFCLOUD-70815 to address the issue of the context object not being populated sufficiently.

Regards,
Dugald

1 Like

Regarding the issue about macro ID not being available to the editor, I thought about creating an issue to make it available, but I decided to create an issue for the semantics of the macro ID to be documented - see CONFCLOUD-70816. The reason I went this way is because it seems the macro ID can mutate so I’m not even sure how useful it is.

In the absence of clear macro ID semantics and availability in enough contexts to be useful, perhaps you can allocate your own immutable ID an embed it in your macro data.

Overall, the gaps in the APIs makes it difficult to pass data between the view and edit modes. Here’s a summary of the issues I am aware of (not all state related, but they add to the confusion):

To pass data between the view and edit modes, you may be able to use the following hacky technique:

  • When the dynamic content macro view resource opens in preview mode (pass the {output.type} context parameter), call AP.context.getContext(callback) and within the callback extract the content ID by referencing context.confluence.content.id and set a content property with the value using AP.confluence.setContentProperty().
  • Later, when in the macro’s custom editor, you won’t be able to use AP.context.getContext(callback) to determine the current content ID due to CONFCLOUD-70815, but you will be able to call AP.confluence.getContentProperty() to retrieve the content ID. Once you have the content ID, you can make further calls to set and retrieve properties to/from the content.
6 Likes

Thanks for putting all this together Dugald, very helpful!

@dmorrow awesome stuff, thanks a lot for this, I was not aware of such macro.id behaviour, so in fact it does not make sense to rely on it.

No problems @Grzegorz.Tanczyk. I was a bit surprised by this also and have doubts in the back of my mind about my observations and interpretation of macro.id behaviour so it will be interesting to see what comes out of CONFCLOUD-70816.

2 Likes