Add Macros To Page Body Programmatically (C#)

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

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
Community showcase
Posted yesterday in United States

Confluence Security Advisory

Good morning Members, Not sure if you are aware. Please read the following: https://www.icloud.com/keynote/0RyJ4VEdNUpjDpGfMhfZx9fEg#Confluence_Security_Advisor More details: https://co...

43 views 1 0
View post

Atlassian User Groups

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

Find a group

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

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you