C# wrapper for inserting items

I have used some time now on integrating with monday. And a lot of people here has been very helpful so want to give a little back. This is just POC code but can properly help some getting started with c#. I will start to extend it for production and return in some weeks with more functionality.
How to use the code

var boardId = "111";
var groupId = "topics";

var monday = new Monday(_config.MondayAPIKey);

var columnValues = new List<IMondayColumn>();

var systemObservationColumn = new LongTextColumn("long_text", "LongTextColumn text");
columnValues.Add(systemObservationColumn);

var actionColumn = new LongTextColumn("long_text1", "LongTextColumn text");
columnValues.Add(actionColumn);

var warningGivenToColumn = new PersonColumn("person", "13958870");
columnValues.Add(warningGivenToColumn);

var typeColumn = new DropDownColumn("dropdown", "Due date");
columnValues.Add(typeColumn);

var result = await monday.InsertItem(boardId, groupId, "Tester 2", columnValues);

The code:

public interface IMondayColumn
{
    string GetJSON();
}

public class DropDownColumn : IMondayColumn
{
    string _label;
    string _columnName;

    public DropDownColumn(string columnName, string label)
    {
        _columnName = columnName;
        _label = label;
    }
    public string GetJSON()
    {
        return @"\\\"""+_columnName+@"\\\"":{\\\""labels\\\"":[\\\"""+_label+@"\\\""]}";
    }
}

public class LongTextColumn : IMondayColumn
{
    string _text;
    string _columnName;
    public LongTextColumn(string columnName, string text)
    {
        _columnName = columnName;
        _text = text;
    }
    public string GetJSON()
    {
        return @"\\\"""+_columnName+@"\\\"": { \\\""text\\\"": \\\""" + _text+ @"\\\""}";
    }
}

public class PersonColumn : IMondayColumn
{
    string _userId;
    string _columnName;

    public PersonColumn(string columName, string userId)
    {
        _userId = userId;
        _columnName = columName;
    }

    public string GetJSON()
    {
        return @"\\\"""+_columnName+@"\\\"":{\\\""personsAndTeams\\\"":[{\\\""id\\\"":"+_userId+@",\\\""kind\\\"":\\\""person\\\""}]}";
    }
}

public class Monday
{
	string _apiKey;
	string MondayApiUrl = "https://api.monday.com/v2/";

	public Monday(string apiKey)
    {
		_apiKey = apiKey;
	}

	public async Task<string> InsertItem(string boardId, string groupId, string itemName, List<IMondayColumn> columnValues)
    {
		var columnValuesStr = GetColumnValuesFronList(columnValues);
		//var query = @"{""query"": ""mutation {create_item(board_id: 674077648, group_id:\""topics\"", item_name: \""adding works\"", column_values: \"" {\\\""long_text\\\"": { \\\""text\\\"": \\\""Sample text\\\""}} \"") {id}  }"" }";
		var query = @"{""query"": ""mutation {create_item(board_id: "+boardId+@", group_id:\"""+groupId+@"\"", item_name: \"""+itemName+@"\"", column_values: \"" {" + columnValuesStr + @"} \"") {id}  }"" }";




		return await Query(query);
    }

	string GetColumnValuesFronList(List<IMondayColumn> columnValues)
    {
		string columnValuesStr = string.Empty;

		foreach(var column in columnValues)
        {
			columnValuesStr += column.GetJSON()+",";
		}
		columnValuesStr = columnValuesStr.Trim(',');

		return columnValuesStr;

	}

	public async Task<string> Query(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", _apiKey);

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

		using (HttpWebResponse response = (HttpWebResponse)await request.GetResponseAsync())
		using (Stream stream = response.GetResponseStream())
		using (StreamReader reader = new StreamReader(stream))
		{
			return await reader.ReadToEndAsync();
		}
	}
}
3 Likes

Thanks for sharing ! Are you planning to make it into nuget package ?

Hey @ThomasSe :wave:

I LOVE this! Thanks so much for sharing with the community and giving back to other builders :slight_smile: Iā€™m sure this will help some developers get a quicker and smoother start when building apps and integrations with C#.

-Alex

Yes I am. I will get back later.

1 Like