Zoho CRM Integration In ASP.NET MVC

Introduction

Zoho CRM is a cloud-based application, and therefore the application and the data are not stored in your computer system’s memory. Since cloud applications rely on remote servers, you are required to have a continual internet connection for accessing Zoho CRM

In this article, we will learn how to integrate Zoho CRM in ASP.NET MVC Web application.

Let’s begin

Go to the site: accounts.zoho.com/developerconsole

Click on “Sign Up Now” to make registration. After a successful login, the below page appears.

after Click on “Get Started” the below page appears.

after select Server-based Applications and the below page appears.

Enter the Client name. Set your Project Url in the HomePage Url and Authorized Redirect Url.

Click on create so the client created successfully.

You will get a Client ID and Client Secret.

Set Client ID and Client Secret in web.config and add the below code in it.

we need to Install “ZCRMSDK” SDK in our application.

C# Code Example

Open the Web.config file and add the below code in it.

<add key="client_id" value="Your Client Id" />
<add key="client_secret" value="Your Client Secret" />


<add key="redirect_uri" value="Your Project URL" />
<add key="access_type" value="offline" />
<add key="photoUrl" value="https://profile.zoho.com/api/v1/user/self/photo" />
<add key="currentUserEmail" value="Your Email Address" />
<add key="Scopes" value="aaaserver.profile.READ,ZohoCRM.modules.ALL,ZohoCRM.modules.leads.ALL,ZohoCRM.modules.products.ALL,ZohoCRM.users.ALL,ZohoCRM.settings.profiles.ALL,ZohoCRM.settings.roles.ALL,ZohoCRM.org.all,ZohoCRM.settings.fields.all,ZohoCRM.settings.layouts.all,ZohoCRM.settings.all,ZohoCRM.settings.related_lists.all,ZohoCRM.settings.custom_views.all,ZohoCRM.modules.leads.ALL,ZohoCRM.modules.products.ALL,ZohoCRM.settings.tags.all,ZohoCRM.modules.attachments.all,ZohoCRM.mass_update.leads.UPDATE,ZohoCRM.bulk.read,ZohoFiles.files.ALL,ZohoCRM.bulk.ALL,ZohoCRM.modules.attachments.all,ZohoCRM.modules.leads.READ,ZohoCRM.modules.products.CREATE,ZohoCRM.modules.invoices.CREATE,ZohoCRM.modules.accounts.ALL,ZohoCRM.modules.contacts.ALL,ZohoCRM.modules.vendors.ALL,ZohoCRM.modules.purchaseorders.ALL,ZohoCRM.modules.invoices.ALL,ZohoCRM.modules.deals.ALL" />

Open the HomeController.cs file and add the below code in it.

public static Dictionary<string, string> config = new Dictionary<string, string>()
{
    {"client_id",ConfigurationManager.AppSettings["client_id"]},
    {"client_secret",ConfigurationManager.AppSettings["client_secret"]},
    {"redirect_uri",ConfigurationManager.AppSettings["redirect_uri"]},
    {"access_type",ConfigurationManager.AppSettings["access_type"]},
    {"loginAuthClass", "ZCRMSDK.CRM.Library.Common.ZCRMConfigUtil, ZCRMSDK"},
    {"persistence_handler_class","ZCRMSDK.OAuth.ClientApp.ZohoOAuthFilePersistence, ZCRMSDK"},
    {"oauth_tokens_file_path",""},
    {"apiBaseUrl","https://www.zohoapis.com"},
    {"iamURL","https://accounts.zoho.com"},
    {"photoUrl","https://profile.zoho.com/api/v1/user/self/photo"},
    {"apiVersion","v2"},
    {"logFilePath","" },
    {"timeout",""},
    {"minLogLevel",""},
    {"domainSuffix",""},
    {"currentUserEmail",ConfigurationManager.AppSettings["currentUserEmail"]},
    {"Scopes",ConfigurationManager.AppSettings["Scopes"]},
    {"response_type","code"},
};

public ActionResult Index(string code = "")
{
    try
    {
        //code = Grant Token
        if (string.IsNullOrWhiteSpace(code))
        {
            string ParentURL = "https://accounts.zoho.com/oauth/v2/auth?scope=" + config["Scopes"] + "&prompt=consent&client_id=" + config["client_id"] + "&response_type=code&access_type=" + config["access_type"] + "&redirect_uri=" + config["redirect_uri"];

            return Redirect(ParentURL);
        }
        else
        {
            string accesstoken = GenerateZohoToken(code);
            Response.Write(accesstoken);
            
        }
    }
    catch (Exception Ex)
    {

    }
    return View();
}

 public string GenerateZohoToken(string GrantToken)
 {
    string result = string.Empty;
    try
    {
        string path = Server.MapPath("~/ZohoToken");
        string filePath = Server.MapPath("~/ZohoToken") + "\\" + "ZohoTemp.txt";
        if (!(Directory.Exists(path)))
        {
            Directory.CreateDirectory(path);
            
            if (!System.IO.File.Exists(filePath))
            {
                System.IO.File.Create(filePath);
            }

            
            
        }
        config["oauth_tokens_file_path"] = filePath;
        ZCRMRestClient.Initialize(config);
        ZohoOAuthClient client = ZohoOAuthClient.GetInstance();
        ZohoOAuthTokens tokens = client.GenerateAccessToken(GrantToken);
        if (tokens.AccessToken != null)
        {
            result = tokens.AccessToken;
        }

    }
    catch (Exception ex)
    {
        result = null;
    }
    return result;
}

 

Output:

if you have any questions or issues about this article, please let me know and more details here.

 

 

 

 

1 Comment

  1. Sunil

    Hi,
    The following code is giving error to include the namespace in C# code

    ZCRMRestClient.Initialize(config);
    ZohoOAuthClient client = ZohoOAuthClient.GetInstance();
    ZohoOAuthTokens tokens = client.GenerateAccessToken(GrantToken);

    0
    0
    Reply

Submit a Comment

Your email address will not be published. Required fields are marked *

Subscribe

Select Categories