Search code examples
c#.nettrello

C# .NET Update Custom Field item on Card using Trello REST API


I'm creating an app using .NET in c# to create cards on a Trello board but I can't make updates to the custom fields. I already scoured the API documentation and the internet but cannot make it work.

The code that I'm using to update the custom field value on the card is the following:

private static async Task<String> PutCustomFieldsAsync(string Url, string jsonContent)
{
    using (var httpClient = new HttpClient())
    {
        var response = await httpClient.PutAsync(Url, new StringContent(jsonContent, System.Text.Encoding.UTF8, "application/json"));
        json = await response.Content.ReadAsStringAsync();
    };

    return json;
}

The values of the variable are the following:

Url: "https://api.trello.com/1/cards/6...3fe/customField/5...bb9/item?key=4...f&token=e...6"

jsonContent: "{"Value":{"text":"test"}}"

When I set the field manually on Trello and then get the card customfields the JSON that comes out has the following data:

{
    "id": "6...c66",
    "value": {
      "text": "teste"
    },
    "idCustomField": "5...bb9",
    "idModel": "6...3fe",
    "modelType": "card"
  }

The response that I'm getting is "400 Bad Request" due to "Invalid value for custom field type".

Can anyone help me get this to work?

Thanks.


Solution

  • If someone has the same problem, I managed to get trello to update the custom fields with the following code:

    using System.Net.Http;
    using System.Net.Http.Headers;
    using System.Text.Json;
    using System.Collections;
    
    private static async Task<String> PutCustomFieldsAsync(string CardId, string CustomFieldId, string AppKey, string Token, string Value)
    {
        string json = null;
    
        Hashtable tableAux= new Hashtable();
        tableAux["text"] = value;
        Hashtable table = new Hashtable();
        table["value"] = tableAux; 
    
        string Url = String.Format("https://api.trello.com/1/cards/{0}/customField/{1}/item?key={2}&token={3}", CardId, CustomFieldId, AppKey, Token);
    
        string jsonContent= JsonSerializer.Serialize(table);
    
                using (var httpClient = new HttpClient())
                {
                    using (var request = new HttpRequestMessage(new HttpMethod(Method), Url))
                    {
                        request.Headers.TryAddWithoutValidation("Content - Type", "application/json");
                        request.Content = new StringContent(jsonContent);
                        request.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json");
    
                        HttpResponseMessage response = httpClient.SendAsync(request).Result;
                        if (response.IsSuccessStatusCode)
                            json = await response.Content.ReadAsStringAsync();
                    }
                }
        return json; 
    }