Ken
Ken

Reputation: 143

Zendesk Custom User/Organization fields

Ok, so I got a problem in zendesk with two possible solutions but no idea if either of them is going to work. We have our system built with custom fields created for each of our clients and organizations. First I tried using the C# wrapper however there is no way to access custom fields through those methods, if there is I am unable to find it.

So if there isn't a way to do this through the C# wrapper I am trying to do this via full rest. Here is that code.

string url = "https://{mydomain}.zendesk.com/api/v2/user_fields/7.json";
        string creds = Convert.ToString(Encoding.UTF8.GetBytes(EMAIL + "/"+ TOKEN));
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
        request.Headers.Add("Authorization", "Basic" + creds);

        request.Method = "GET";

        HttpWebResponse resp = request.GetResponse() as HttpWebResponse;

        if (resp.StatusCode == HttpStatusCode.OK)
        {
            using (Stream respStream = resp.GetResponseStream())
            {
                StreamReader reader = new StreamReader(respStream, Encoding.UTF8);
                string test = reader.ReadToEnd();
            }
        }
        else
        {
            Console.WriteLine(string.Format("Status Code: {0}, Status Description: {1}", resp.StatusCode, resp.StatusDescription));
        }

Right now all this is supposed to do is get a response that will return a name of all columns to me, However I keep getting a 401 unauthorized response. Any suggestions as to what I am doing wrong?

Upvotes: 0

Views: 1487

Answers (1)

Tristan
Tristan

Reputation: 1

For Zendesk c# wrapper...

IndividualTicketResponse tr = zapi.Tickets.GetTicket(9);
        var customField = tr.Ticket.CustomFields.Where(x => x.Id == 23653058).Single();//.Select(x => x.Id).Single();
        customField.Value = "Updated via c# wrapper";
        IndividualTicketResponse ticketResponse = zapi.Tickets.UpdateTicket(tr.Ticket);

I do not use token access but if not using the wrapper this worked for me...

 string serverURL = "https://myzensubdomain.zendesk.com/api/v2/tickets/9.json";
        string userName = "[email protected]";
        string userPassword = "mySecretPassword";


        TicketCustomFieldUpdate dataTicket = new TicketCustomFieldUpdate();


        CustomField cf = new CustomField();
        cf.id = 23653058;
        cf.value = "It worked!";
        dataTicket.ticket = new Ticket();
        dataTicket.ticket.custom_fields = new List<CustomField>();
        dataTicket.ticket.custom_fields.Add(cf);

        string stJSON = clsJSONHelper.JsonSerializer<TicketCustomFieldUpdate>(dataTicket);


        NetworkCredential myCred = new NetworkCredential(userName, userPassword, serverURL);
        CredentialCache myCache = new CredentialCache();
        myCache.Add(new Uri(serverURL), "Basic", myCred);

        HttpWebRequest wreq = (HttpWebRequest)HttpWebRequest.Create(serverURL);
        wreq.Credentials = new NetworkCredential(userName, userPassword);
        wreq.ContentLength = 0;            
        wreq.Method = "PUT";
        wreq.ContentType = "application/json";
        wreq.PreAuthenticate = true;


        ASCIIEncoding encoding = new ASCIIEncoding();
        byte[] byte1 = encoding.GetBytes(stJSON);
        wreq.ContentLength = byte1.Length;

        using (Stream strm = wreq.GetRequestStream())
        {
            strm.Write(byte1, 0, byte1.Length);
        }

        var wresp = (HttpWebResponse)wreq.GetResponse();
        using (var strmr = new StreamReader(wresp.GetResponseStream()))
        {
            var responseText = strmr.ReadToEnd();
            context.Response.Write(responseText);
        }

Upvotes: 0

Related Questions