Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

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

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

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 Leader Sep 15, 2020

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

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.'

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 Leader Sep 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 :)

@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.

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... 

@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
Community showcase
Published in Trello

📹 Favorite Trello templates

Hello Community!  My name is Brittany Joiner and I am a Trello enthusiast and Atlassian Community Leader. In this video, I'll share my favorite Trello templates. Templates mentioned in ...

3,623 views 25 63
Read article

Community Events

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

Find an event

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

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you