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

Add Macros To Page Body Programmatically (C#)

Bart Kerfeld Jul 28, 2015

Hello, I have been developing an app in C# to quickly write a bunch of html pages to our confluence site. I have found that the html macro works well for this the problem is that I do not know how to generate a proper macro id for it. 

<ac:structured-macro ac:macro-id="945a5a80-e4b6-4092-89f1-03db8a5bbe3e" ac:name="html" ac:schema-version="1"><ac:plain-text-body><![CDATA[ HTML GOES HERE]]></ac:plain-text-body></ac:structured-macro>

The method I use to update the pages is through the REST API (I use sql to get content id and version). It works for me. Any ideas on how to generate a macro-id?

public void UpdatePage(string pageName, string spaceKey, string bodyContent, bool verbose = false)
        {
            this.verbose = verbose;
            string contentId = GetContentId(pageName, spaceKey);
            int nextVersion;
            if (Int32.TryParse(GetPageVersion(pageName, spaceKey), out nextVersion))
                nextVersion++;
            else
                Console.WriteLine("String could not be parsed.");
            string version = nextVersion.ToString();
            string query = "{\"id\":\"" + contentId + "\",\"type\":\"page\",\"title\":\"" + pageName + "\",\"space\":{\"key\":\"" + spaceKey + "\"},\"body\":{\"storage\":{\"value\":\"" + bodyContent + "\",\"representation\":\"storage\"}},\"version\":{\"number\":" + version + "}}";
            program.RunQuery(ConfluenceResource.content, contentId, query, "PUT", verbose);
        }
public void RunQuery(string resource, string argument = null, string data = null, string method = "GET", bool verbose = false, bool retry = false)
        {
            string url = string.Format("{0}{1}/", BASE_URL, resource.ToString());
            if (argument != null)
            {
                url = string.Format("{0}{1}/", url, argument);
            }
            HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
            request.ContentType = "application/json";
            request.Method = method;
            if (data != null)
            {
                using (StreamWriter writer = new StreamWriter(request.GetRequestStream()))
                {
                    writer.Write(data);
                    //writer.Flush();
                    //writer.Close();
                }
            }
            string base64Credentials = GetEncodedCredentials(USERNAME, PASSWORD);
            request.Headers.Add("Authorization", "Basic " + base64Credentials);
            try
            {
                HttpWebResponse response = request.GetResponse() as HttpWebResponse;
                string result = string.Empty;
                using (StreamReader reader = new StreamReader(response.GetResponseStream()))
                {
                    result = reader.ReadToEnd();
                }
                response.Dispose();
                response.Close();
                if (verbose) Console.WriteLine(result);
            } 
            catch (Exception)
            {
                if (!retry)
                {
                    Console.WriteLine("Failed to processes, trying again...");
                    RunQuery(resource, argument, data, method, verbose, true);
                }
                else throw;
            }
        }

1 answer

0 votes
Amit Rawat Mar 14, 2017

I am also stuck with this problem. How can we get this macro id. Did you find any solution?

Suggest an answer

Log in or Sign up to answer
This widget could not be displayed.
This widget could not be displayed.
Community showcase
Published in Next-gen

Introducing subtasks for breaking down work in next-gen projects

Teams break work down in order to help simplify complex tasks. This is often done iteratively, with tasks being broken down into smaller tasks and so on until the work is accurately captured in well-...

2,715 views 14 17
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