Zoho CRM API in asp.net

 Posted on 09/14/2018

Share On: facebook gplus twitter

In this article, I will explain how you can use Zoho CRM API in your asp.net application. In this API, we will retrieve all customer from Zoho CRM API.

 //API for ZOHO to get customers

 public void GetCustomerList()

        {

            try

            {

                //API for ZOHO to get customers

                string strAuthToken = "Your Auth Token should be here";

                var webAddr = "https://crm.zoho.com/crm/private/xml/Contacts/getRecords?newFormat=1&authtoken=" + strAuthToken + "&scope=crmapi&fromIndex=1&toIndex=500&sortColumnString=Account%20Name&sortOrderString=desc";

                var request = (HttpWebRequest)WebRequest.Create(webAddr);

                request.ContentType = "application/xml; charset=utf-8";

                request.Method = "GET";               

                var httpResponse = (HttpWebResponse)request.GetResponse();

                string result = "";

                using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
                {

                    result = streamReader.ReadToEnd();

                }

                //Create the XmlDocument.

                XmlDocument doc = new XmlDocument();

                doc.LoadXml(result);

                //Display all the book titles.

                XmlNodeList elemList = doc.SelectNodes("response/result/Contacts/row");

                for (int i = 0; i < elemList.Count; i++)
                {

                    XmlNodeList XMLNode = elemList[i].SelectNodes("FL");

                    string CustomerId = "";

                    string Relationship = "";

                    string FirstName = "";

                    string LastName = "";

                    string Mobile = "";

                    string Email = "";

                    string MailingStreet = "";

                    string MailingSuburb = "";

                    string MailingState = "";

                    string MailingZip = "";

                    string AccountId = "";

                    string AccountName = "";

                    string PostalAddressName = MailingStreet + ", " + MailingSuburb + ", " + MailingState + " - " + MailingZip;

                    string PhysicalAddressName = MailingStreet + ", " + MailingSuburb + ", " + MailingState + " - " + MailingZip;

                    for (int j = 0; j < XMLNode.Count; j++)

                    {

                        if (XMLNode[j].Attributes[0].Value == "Customer ID") CustomerId = XMLNode[j].InnerText;

                        if (XMLNode[j].Attributes[0].Value == "Relationship") Relationship = XMLNode[j].InnerText;

                        if (XMLNode[j].Attributes[0].Value == "First Name") FirstName = XMLNode[j].InnerText;

                        if (XMLNode[j].Attributes[0].Value == "Last Name") LastName = XMLNode[j].InnerText;

                        if (XMLNode[j].Attributes[0].Value == "Mobile") Mobile = XMLNode[j].InnerText;

                        if (XMLNode[j].Attributes[0].Value == "Email") Email = XMLNode[j].InnerText;

                        if (XMLNode[j].Attributes[0].Value == "Mailing Street") MailingStreet = XMLNode[j].InnerText;

                        if (XMLNode[j].Attributes[0].Value == "Mailing City") MailingSuburb = XMLNode[j].InnerText;

                        if (XMLNode[j].Attributes[0].Value == "Mailing State") MailingState = XMLNode[j].InnerText;

                        if (XMLNode[j].Attributes[0].Value == "Mailing Zip") MailingZip = XMLNode[j].InnerText;

                        if (XMLNode[j].Attributes[0].Value.ToUpper() == "ACCOUNTID") AccountId = XMLNode[j].InnerText;

                        if (XMLNode[j].Attributes[0].Value.ToUpper() == "ACCOUNT NAME") AccountName = XMLNode[j].InnerText;

                    }

                }

            }

            catch (Exception Ex)

            {

                throw Ex;

            }

        }       

    }

In above sample code, there is CURL and HttpWebRequest used to make API call and get response in XML format. Once response received as XML string, I have used XMLDocument to convert string into xml document.

Above API call return customer list so for loop is used to get each customer details into separate variables which you can either add into custom data table and use it.

I hope this give you better idea about how you can use API in asp.net for Zoho CRM.

Add Comment:

Comments

newformat=1 eliminates records with null values. Then are we not getting less records than actual? why should we leave out records?
09/08/2019 7:26:39 AM   07:26:39   Parthasarathy Mandayam
While inserting records into sql server after few records it errors out with "string or binary data may be truncated". HOw to find the exact record that caused the error?
08/08/2019 6:38:57 PM   18:38:57   Parthasarathy Mandayam
Nice article
12/11/2018 3:37:11 PM   15:37:11   Shailesh Chaudhary