Creating an Item with Column Values in C#

I’m using C# ASP.NET with the monday.com API. I’ve been able to do queries and also I’m able to create an item. However, I’m having trouble creating an item with field values. I’ve seen various posts on the same issue but I can’t seem to get it right. Any help or ideas would be appreciated.

I’ll share the code I’m using. First, I created a new class:


public class MondayAPIClass
    {
        private const string MondayApiKey = "yourkey";

        private const string MondayApiUrl = "https://api.monday.com/v2/";

        public string QueryMonday(string query)
        {
            byte[] dataBytes = System.Text.Encoding.UTF8.GetBytes(query);

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(MondayApiUrl);
            request.ContentType = "application/json";
            request.Method = "POST";
            request.Headers.Add("Authorization", MondayApiKey);

            using (Stream requestBody = request.GetRequestStream())
            {
                requestBody.Write(dataBytes, 0, dataBytes.Length);
            }

            using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
            using (Stream stream = response.GetResponseStream())
            using (StreamReader reader = new StreamReader(stream))
            {
                return reader.ReadToEnd();
            }
        }
    }

Then, I use the class in the code behind of my .aspx page.

public string CreateItem()
    {

        MondayAPIClass objMonday = new MondayAPIClass();

   // The code below works to create an item.
        string queryStr = "{\"query\":\"mutation{ create_item(board_id: 2698883853,  item_name: \\\"Joe's Item\\\"){ id} }\"}";

// However, this code to include  column values does not work.
// Error message: {"errors":[{"message":"Parse error on \"text\" (STRING) at [1, 88]","locations":[{"line":1,"column":88}]}],"account_id":10876741}

//        queryStr = "{\"query\":\"mutation{ create_item(board_id: 2698883853,  item_name: \\\"Joe's Item\\\" ";
//        queryStr += ", column_values:  {\\\"text\\\": \\\"ready3\\\"}  ){ id} }\"}";

// And, this code with extra " around the values doesn't work 
// This produces a (500) Internal Server Error.

//        queryStr = "{\"query\":\"mutation{ create_item(board_id: 2698883853,  item_name: \\\"Joe's Item\\\" ";
//        queryStr += ", column_values: \" {\\\"text\\\": \\\"ready3\\\"} \" ){ id} }\"}";

        string json = objMonday.QueryMonday(queryStr);
        return json;
    }

The CreateItem() function works with the code shown. However, when using the code that is commented out (includes column values), it fails with error messages.

I finally figured this out!!! If anyone else is having trouble you should give this a try. I had to use Newtonsoft (using Newtonsoft.Json;). Now my CreateItem() function looks like this:

public string CreateItem()
    {       
         MondayAPIClass objMonday = new MondayAPIClass();

        var call = JsonConvert.SerializeObject(
        new
        {
            query = "mutation {create_item(board_id: 2695234567, item_name: \"Joe's Item\", column_values: \"{\\\"text\\\": \\\"Joes's Stuff\\\",\\\"status\\\": \\\"New\\\"}\") {id} }"
        }
       );

        string json = objMonday.QueryMonday(call);
        return json;
    }

I hope this helps someone as I’ve spent a lot of time figuring this out.

2 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.