Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

How do I set a custom field item via C# using Http Request Message?

David Funni September 15, 2020

I am able to successfully get all the cards and custom fields from my board using:

string qString = @"https://api.trello.com/1/boards/{boardId}/cards?customFieldItems=true&key={myKey}&token={myToken}";  using(var request = new HttpRequestMessage(new HttpMethod("GET"), qString))
  {
    var response = await httpClient.SendAsync(request);
    String JsonString = await response.Content.ReadAsStringAsync();

but now I want to Set one of those custom fields.  I've tried this but it doesn't work:

    using(var httpClient = new HttpClient())
    {       using(var request = new HttpRequestMessage(new HttpMethod("PUT"), @"https://api.trello.com/1/cards/" + myCardId + "/" + myCustomFieldId + "/item?" + "key={myKey}&token={myToken}" + "{value: { text: \"New Value\"}}"))
       {
            // i don't know how to set the header content so i commented it out
            //request.Headers.Add("content-type", "application/json");
            var response = await httpClient.SendAsync(request);
            String JsonString = await response.Content.ReadAsStringAsync();

 Thjanks,

Dave

3 answers

0 votes
David Funni September 19, 2020

Thanks for your guy's help. 

I was able to figure out my question using this website which converts Curl to C#:

https://curl.olsh.me/

private async void button4_Click(object sender, EventArgs e)
{
    using(var httpClient = new HttpClient())
    {
        String querry = @"https://api.trello.com/1/card/" + myCardId + "/customField/" + myCardsCustomFieldItems_IdCustomField + "/item";
        using(var request = new HttpRequestMessage(new HttpMethod("PUT"), querry))
        {
            request.Content = new StringContent("{  \"value\": { \"text\"\"Hello, world!\" }, \"key\"\"########\",  \"token\"\"#######\" }");
            request.Content.Headers.ContentType = System.Net.Http.Headers.MediaTypeHeaderValue.Parse("application/json");
 
            var response = await httpClient.SendAsync(request);
            String JsonString = await response.Content.ReadAsStringAsync();
 
            this.textBox4.Text = querry;
            this.richTextBox4.Text = JsonString;
        }
    }
}
0 votes
Iain Dooley
Community Champion
September 15, 2020

@David Funni you need to have the header set, why did you comment it out?

David Funni September 17, 2020

When I put that line in, I get this exception:

System.InvalidOperationException: 'Misused header name. Make sure request headers are used with HttpRequestMessage, response headers with HttpResponseMessage, and content headers with HttpContent objects.'

David Funni September 17, 2020

I do believe I need to figure that out though as the error message I get back has my Json string all converted to some ascii representation or something like that: 

Cannot PUT /1/cards/{cardId}/{CustomFieldId}/item?key={myKey}&token={myToken}%7Bvalue:%20%7Btext:%20%22TestName%22%7D%7D

Iain Dooley
Community Champion
September 17, 2020

@David Funni you might find the specifics of how to format the request here:

https://github.com/gregsdennis/Manatee.Trello

or you could just use that lib :)

0 votes
milynnus
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
September 15, 2020

@David Funni  I code using python wrapper for Trello API so I am not familiar with c#. First Trello CF definitions are kept at the board level. So you need to use this

Get Custom Fields for Board

GET /1/boards/{id}/customFields

Get the Custom Field Definitions that exist on a board.

 

in the first part of your code to get CF definitions. As such I believe that your myCustomFieldID not the id of the CF text field you want to set. 

For the second part the API should be

Update Custom Field item on Card

PUT /1/cards/{idCard}/customField/{idCustomField}/item

Setting, updating, and removing the value for a Custom Field on a card.

David Funni September 17, 2020

I see that I can get the custom field definitions for the board.  And I do.  And each one has an Id.

When I get the cards for the board I can request that it give me the card's custom field items also. So I do. And each of those has an Id and an IdCustomField.    The Id is a unique Id of this field in this card, whereas the IdCustomField is equal to the corresponding custom field Id in the board. 

I'm not sure which Id to use in my PUT though... 

milynnus
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
September 17, 2020

@David Funni the equivalent in py-Trello for what you are doing is likely this:

def set_custom_field(self, value, custom_field):

in the code for PUT the definition ID is used. 

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events