Copyright (c) 2021, ZOHO CORPORATION PRIVATE LIMITED
All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
- Overview
- Registering a Zoho Client
- Environmental Setup
- Including the SDK in your project
- Persistence
- Configuration
- Initialization
- Class Hierarchy
- Responses And Exceptions
- Threading
- Sample Code
Zoho CRM C# SDK offers a way to create client C# applications that can be integrated with Zoho CRM.
Since Zoho CRM APIs are authenticated with OAuth2 standards, you should register your client app with Zoho. To register your app:
-
Visit this page https://api-console.zoho.com/
-
Click
ADD CLIENT
. -
Choose the
Client Type
. -
Enter Client Name, Client Domain or Homepage URL and Authorized Redirect URIs. Click
CREATE
. -
Your Client app will be created.
-
Select the created OAuth client.
-
Generate grant token by providing the necessary scopes, time duration (the duration for which the generated token is valid) and Scope Description.
C# SDK requires .NET Framework 4.6.1 or above to be set up in your development environment.
You can include the SDK to your project using:
-
Install Visual Studio IDE from Visual Studio (if not installed).
-
C# SDK is available as a Nuget package. The ZOHOCRMSDK-2.1 assembly can be installed through the Nuget Package Manager or through the following options:
-
Package Manager
Install-Package ZOHOCRMSDK-2.1 -Version 3.0.0 Install-Package MySql.Data -Version 6.9.12 Install-Package Newtonsoft.Json -Version 11.0.1
-
.NET CLI
dotnet add package ZOHOCRMSDK-2.1 --version 3.0.0 dotnet add package Newtonsoft.Json --version 11.0.1 dotnet add package MySql.Data --version 6.9.12
-
PackageReference
For projects that support PackageReference, copy this XML node into the project file to refer the package.
<ItemGroup> <PackageReference Include="ZOHOCRMSDK-2.1" Version="3.0.0" /> <PackageReference Include="Newtonsoft.Json" Version="11.0.1" /> <PackageReference Include="MySql.Data" Version="6.9.12" /> </ItemGroup>
- A sample .csproj file
-
Token persistence refers to storing and utilizing the authentication tokens that are provided by Zoho. There are three ways provided by the SDK in which persistence can be utilized. They are DataBase Persistence, File Persistence, and Custom Persistence.
Once the application is authorized, OAuth access and refresh tokens can be used for subsequent user data requests to Zoho CRM. Hence, they need to be persisted by the client app.
The persistence is achieved by writing an implementation of the inbuilt TokenStore interface, which has the following callback methods.
-
GetToken(UserSignature user, Token token) - invoked before firing a request to fetch the saved tokens. This method should return an implementation of Token interface object for the library to process it.
-
SaveToken(UserSignature user, Token token) - invoked after fetching access and refresh tokens from Zoho.
-
DeleteToken(Token token) - invoked before saving the latest tokens.
-
GetTokens() - The method to retrieve all the stored tokens.
-
DeleteTokens() - The method to delete all the stored tokens.
-
GetTokenById(string id, Token token) - This method is used to retrieve the user token details based on unique ID.
In case the user prefers to use the default DataBase persistence, MySQL can be used.
-
The database name should be zohooauth.
-
There must be a table named oauthtoken with the following columns.
-
id varchar(255)
-
user_mail varchar(255)
-
client_id varchar(255)
-
client_secret varchar(255)
-
refresh_token varchar(255)
-
access_token varchar(255)
-
grant_token varchar(255)
-
expiry_time varchar(20)
-
redirect_url varchar(255)
-
Note:
- Custom database name and table name can be set in DBStore instance
CREATE TABLE oauthtoken (
id varchar(255) NOT NULL,
user_mail varchar(255) NOT NULL,
client_id varchar(255),
client_secret varchar(255),
refresh_token varchar(255),
access_token varchar(255),
grant_token varchar(255),
expiry_time varchar(20),
redirect_url varchar(255),
primary key (id)
);
/*
* Create an instance of TokenStore.
* Host -> DataBase host name. Default value "localhost"
* DatabaseName -> DataBase name. Default value "zohooauth"
* UserName -> DataBase user name. Default value "root"
* Password -> DataBase password. Default value ""
* PortNumber -> DataBase port number. Default value "3306"
* TableName -> Table Name. Default value "oauthtoken"
*/
TokenStore tokenstore = new DBStore.Builder().Build();
//TokenStore interface
TokenStore tokenstore = new DBStore.Builder()
.Host("hostName")
.DatabaseName("dataBaseName")
.TableName("tableName")
.UserName("userName")
.Password("password")
.PortNumber("portNumber")
.Build();
In case of default File Persistence, the user can persist tokens in the local drive, by providing the the absolute file path to the FileStore object.
-
The File contains
-
id
-
user_mail
-
client_id
-
client_secret
-
refresh_token
-
access_token
-
grant_token
-
expiry_time
-
redirect_url
-
//Parameter containing the absolute file path to store tokens
TokenStore tokenstore = new FileStore("/Users/user_name/Documents/csharp_sdk_token.txt");
To use Custom Persistence, the user must implement TokenStore interface(Com.Zoho.API.Authenticator.Store.TokenStore) and override the methods.
using System;
using Com.Zoho.API.Authenticator;
using Com.Zoho.API.Authenticator.Store;
using Com.Zoho.Crm.API;
namespace user.store
{
public class CustomStore : TokenStore
{
public CustomStore()
{
}
/// <summary></summary>
/// <param name="user">A UserSignature class instance.</param>
/// <param name="token">A Token (Com.Zoho.API.Authenticator.OAuthToken) class instance.</param>
/// <returns>A Token class instance representing the user token details.</returns>
public Token GetToken(UserSignature user, Token token)
{
// Add code to get the token
return null;
}
/// <summary></summary>
/// <param name="user">A UserSignature class instance.</param>
/// <param name="token">A Token (Com.Zoho.API.Authenticator.OAuthToken) class instance.</param>
public void SaveToken(UserSignature user, Token token)
{
// Add code to save the token
}
/// <summary></summary>
/// <param name="user">A UserSignature class instance.</param>
/// <param name="token">A Token (Com.Zoho.API.Authenticator.OAuthToken) class instance.</param>
public void DeleteToken(UserSignature user, Token token)
{
// Add code to delete the token
}
public void GetTokens()
{
// Add code to get the all stored tokens
}
public void DeleteTokens()
{
// Add code to delete the all stored token
}
/// <summary></summary>
/// <param name="id">A String representing the unique ID</param>
/// <param name="token">A Token (Com.Zoho.API.Authenticator.OAuthToken) class instance.</param>
/// <returns>A Token class instance representing the user token details.</returns>
public Token GetTokenById(string id, Token token)
{
// Add code to get the token using unique id
return null;
}
}
}
Before you get started with creating your C# application, you need to register your client and authenticate the app with Zoho.
Mandatory Keys | Optional Keys |
---|---|
user | logger |
environment | store |
token | SDKConfig |
requestProxy | |
resourcePath |
-
Create an instance of UserSignature that identifies the current user.
//Create an UserSignature instance that takes user Email as parameter UserSignature user = new UserSignature("abc@zoho.com");
-
Configure API environment which decides the domain and the URL to make API calls.
/* * Configure the environment * which is of the pattern Domain.Environment * Available Domains: USDataCenter, EUDataCenter, INDataCenter, CNDataCenter, AUDataCenter * Available Environments: PRODUCTION, DEVELOPER, SANDBOX */ Environment environment = USDataCenter.PRODUCTION;
-
Create an instance of OAuthToken with the information that you get after registering your Zoho client.
/* * Create a Token instance * ClientId -> OAuth client id. * ClientSecret -> OAuth client secret. * GrantToken -> GRANT token. * RefreshToken -> REFRESH token. * RedirectURL -> OAuth redirect URL. * Id -> User unique id. * RedirectURL -> OAuth redirect URL. */ //Create a Token instance // if refresh token is available // The SDK throws an exception, if the given id is invalid. Token token = new OAuthToken.Builder() .Id("userID") .Build(); // if grant token is available Token token = new OAuthToken.Builder() .ClientId("clientId") .ClientSecret("clientSecret") .GrantToken("grantToken") .RedirectURL("redirectURL") .Build(); // if ID (obtained from persistence) is available Token token = new OAuthToken.Builder() .ClientId("clientId") .ClientSecret("clientSecret") .RefreshToken("refreshToken") .RedirectURL("redirectURL") .Build(); // if access token is available Token token = new OAuthToken.Builder() .AccessToken("accessToken") .Build();
-
Create an instance of Logger Class to log exception and API information. By default, the SDK constructs a Logger instance with level - INFO and file_path - (sdk_logs.log parallel to bin/(Debug or Release) folder )
/* * Create an instance of Logger Class that requires the following * Level -> Level of the log messages to be logged. Can be configured by typing Levels "." and choose any level from the list displayed. * FilePath -> Absolute file path, where messages need to be logged. */ Logger logger = new Logger.Builder() .Level(Logger.Levels.ALL) .FilePath("/Users/user_name/Documents/csharp_sdk_log.log") .Build();
-
Create an instance of TokenStore to persist tokens, used for authenticating all the requests. By default, the SDK creates the sdk_tokens.txt file (parallel to to bin/(Debug or Release) folder) to persist the tokens.
/* * Create an instance of DBStore that requires the following * Host -> DataBase host name. Default "localhost" * DatabaseName -> DataBase name. Default "zohooauth" * UserName -> DataBase user name. Default "root" * Password -> DataBase password. Default "" * PortNumber -> DataBase port number. Default "3306" * TableName -> Table Name. Default value "oauthtoken" */ //TokenStore tokenstore = new DBStore.Builder().Build(); TokenStore tokenstore = new DBStore.Builder() .Host("hostName") .DatabaseName("dataBaseName") .TableName("tableName") .UserName("userName") .Password("password") .PortNumber("portNumber") .Build(); //TokenStore tokenstore = new FileStore("absolute_file_path"); //TokenStore tokenStore = new CustomStore();
-
Create an instance of SDKConfig containing the SDK configuration.
/* * By default, the SDK creates the SDKConfig instance * autoRefreshFields (default - false) * if true - all the modules' fields will be auto-refreshed in the background, every hour. * if false - the fields will not be auto-refreshed in the background. The user can manually delete the file(s) or refresh the fields using methods from ModuleFieldsHandler(com.zoho.crm.api.util.ModuleFieldsHandler) * * pickListValidation (default - true) * A boolean field that validates user input for a pick list field and allows or disallows the addition of a new value to the list. * true - the SDK validates the input. If the value does not exist in the pick list, the SDK throws an error. * false - the SDK does not validate the input and makes the API request with the user’s input to the pick list */ SDKConfig config = new SDKConfig.Builder() .AutoRefreshFields(false) .PickListValidation(true) .Timeout(10) .Build();
-
The path containing the absolute directory path to store user-specific files containing module fields information. By default, the SDK stores the user-specific files in a folder parallel to bin/(Debug or Release)
string resourcePath = "/Users/user_name/Documents/csharpsdk-application";
-
Create an instance of RequestProxy containing the proxy properties of the user.
/* * Create an instance of RequestProxy * host -> proxyHost * port -> proxyPort * user -> proxyUser * password -> password * userDomain -> userDomain */ RequestProxy requestProxy = new RequestProxy.Builder() .Host("proxyHost") .Port(proxyPort) .User("proxyUser") .Password("password") .UserDomain("userDomain") .Build();
Initialize the SDK using the following code.
using System;
using Com.Zoho.API.Authenticator;
using Com.Zoho.API.Authenticator.Store;
using Com.Zoho.Crm.API;
using Com.Zoho.Crm.API.Dc;
using Com.Zoho.Crm.API.Logger;
using static Com.Zoho.API.Authenticator.OAuthToken;
using Environment = Com.Zoho.Crm.API.Dc.DataCenter.Environment;
using SDKInitializer = Com.Zoho.Crm.API.Initializer;
namespace Com.Zoho.Crm.Sample.Initializer
{
public class Initialize
{
public static void SDKInitialize()
{
/*
* Create an instance of Logger Class that requires the following
* Level -> Level of the log messages to be logged. Can be configured by typing Levels "." and choose any level from the list displayed.
* FilePath -> Absolute file path, where messages need to be logged.
*/
Logger logger = new Logger.Builder()
.Level(Logger.Levels.ALL)
.FilePath("/Users/user_name/Documents/csharp_sdk_log.log")
.Build();
//Create an UserSignature instance that takes user Email as parameter
UserSignature user = new UserSignature("abc@zoho.com");
/*
* Configure the environment
* which is of the pattern Domain.Environment
* Available Domains: USDataCenter, EUDataCenter, INDataCenter, CNDataCenter, AUDataCenter
* Available Environments: PRODUCTION, DEVELOPER, SANDBOX
*/
Environment environment = USDataCenter.PRODUCTION;
/*
* Create a Token instance
* ClientId -> OAuth client id.
* ClientSecret -> OAuth client secret.
* GrantToken -> GRANT token.
* RedirectURL -> OAuth redirect URL.
*/
Token token = new OAuthToken.Builder()
.ClientId("clientId")
.ClientSecret("clientSecret")
.GrantToken("GRANT token")
.RedirectURL("redirectURL")
.Build();
/*
* TokenStore can be any of the following
* DB Persistence - Create an instance of DBStore
* File Persistence - Create an instance of FileStore
* Custom Persistence - Create an instance of CustomStore
*/
/*
* Create an instance of DBStore.
* Host -> DataBase host name. Default "localhost"
* DatabaseName -> DataBase name. Default "zohooauth"
* UserName -> DataBase user name. Default "root"
* Password -> DataBase password. Default ""
* PortNumber -> DataBase port number. Default "3306"
* TableName -> Table Name. Default value "oauthtoken"
*/
//TokenStore tokenstore = new DBStore.Builder().Build();
TokenStore tokenstore = new DBStore.Builder()
.Host("hostName")
.DatabaseName("dataBaseName")
.TableName("tableName")
.UserName("userName")
.Password("password")
.PortNumber("portNumber")
.Build();
// TokenStore tokenstore = new FileStore("absolute_file_path");
/*
* autoRefreshFields
* if true - all the modules' fields will be auto-refreshed in the background, every hour.
* if false - the fields will not be auto-refreshed in the background. The user can manually delete the file(s) or refresh the fields using methods from ModuleFieldsHandler(com.zoho.crm.api.util.ModuleFieldsHandler)
*
* pickListValidation
* if true - value for any picklist field will be validated with the available values.
* if false - value for any picklist field will not be validated, resulting in creation of a new value.
*/
SDKConfig config = new SDKConfig.Builder().AutoRefreshFields(false).PickListValidation(true).Timeout(10).Build();
string resourcePath = "/Users/user_name/Documents/csharpsdk-application";
/**
* Create an instance of RequestProxy class that takes the following parameters
* Host -> Host
* Port -> Port Number
* User -> User Name
* Password -> Password
* UserDomain -> User Domain
*/
RequestProxy requestProxy = new RequestProxy.Builder()
.Host("proxyHost")
.Port(proxyPort)
.User("proxyUser")
.Password("password")
.UserDomain("userDomain")
.Build();
/*
* The initialize method of Initializer class that takes the following arguments
* User -> UserSignature instance
* Environment -> Environment instance
* Token -> Token instance
* Store -> TokenStore instance
* SDKConfig -> SDKConfig instance
* ResourcePath -> resourcePath -A String
* Logger -> Logger instance
* RequestProxy -> RequestProxy instance
*/
// Set the following in InitializeBuilder
new SDKInitializer.Builder()
.User(user)
.Environment(environment)
.Token(token)
.Store(tokenstore)
.SDKConfig(config)
.ResourcePath(resourcePath)
.Logger(logger)
.RequestProxy(requestProxy)
.Initialize();
}
}
}
- You can now access the functionalities of the SDK. Refer to the sample codes to make various API calls through the SDK.
All SDK method calls return an instance of the APIResponse class
Use the Object Property in the returned APIResponse object to obtain the response handler interface depending on the type of request (GET, POST,PUT,DELETE).
APIResponse<ResponseHandler> and APIResponse<ActionHandler> are the common wrapper objects for Zoho CRM APIs’ responses.
Whenever the API returns an error response, the response will be an instance of APIException class.
All other exceptions such as SDK anomalies and other unexpected behaviours are thrown under the SDKException class.
-
For operations involving records in Tags
- APIResponse<RecordActionHandler>
-
For getting Record Count for a specific Tag operation
- APIResponse<CountHandler>
-
For operations involving BaseCurrency
- APIResponse<BaseCurrencyActionHandler>
-
For Lead convert operation
- APIResponse<ConvertActionHandler>
-
For retrieving Deleted records operation
- APIResponse<DeletedRecordsHandler>
-
For Record image download operation
- APIResponse<DownloadHandler>
-
For MassUpdate record operations
-
APIResponse<MassUpdateActionHandler>
-
APIResponse<MassUpdateResponseHandler>
-
-
For Transfer Pipeline operation
- APIResponse<TransferActionHandler>
-
The Object Property of the returned APIResponse instance returns the response handler interface.
-
The ResponseHandler interface encompasses the following
- ResponseWrapper class (for application/json responses)
- FileBodyWrapper class (for File download responses)
- APIException class
-
The CountHandler interface encompasses the following
- CountWrapper class (for application/json responses)
- APIException class
-
The DeletedRecordsHandler interface encompasses the following
- DeletedRecordsWrapper class (for application/json responses)
- APIException class
-
The DownloadHandler interface encompasses the following
- FileBodyWrapper class (for File download responses)
- APIException class
-
The MassUpdateResponseHandler interface encompasses the following
- MassUpdateResponseWrapper class (for application/json responses)
- APIException class
-
The Object Property of the returned APIResponse instance returns the action handler interface.
-
The ActionHandler interface encompasses the following
- ActionWrapper class (for application/json responses)
- APIException class
-
The ActionWrapper class contains Property/Properties that may contain one/list of ActionResponse interfaces.
-
The ActionResponse interface encompasses following
- SuccessResponse class (for application/json responses)
- APIException class
-
The ActionHandler interface encompasses following
- ActionWrapper class (for application/json responses)
- APIException class
-
The RecordActionHandler interface encompasses following
- RecordActionWrapper class (for application/json responses)
- APIException class
-
The BaseCurrencyActionHandler interface encompasses following
- BaseCurrencyActionWrapper class (for application/json responses)
- APIException class
-
The MassUpdateActionHandler interface encompasses following
- MassUpdateActionWrapper class (for application/json responses)
- APIException class
-
The ConvertActionHandler interface encompasses following
- ConvertActionWrapper class (for application/json responses)
- APIException class
-
The TransferActionHandler interface encompasses the following
- TransferActionWrapper class (for application/json responses)
- APIException class
Threads in a C# program help you achieve parallelism. By using multiple threads, you can make a C# program run faster and do multiple things simultaneously.
The C# SDK (version 4.x.x) supports both single threading and multi-threading irrespective of a single user or a multi-user app.
Multi-users functionality is achieved using SwitchUser() method.
new Initializer.Builder()
.User(user)
.Environment(environment)
.Token(token)
.SDKConfig(config)
.SwitchUser();
Here is a sample code to depict multi-threading for a multi-user app.
using System;
using System.Collections.Generic;
using System.Threading;
using Com.Zoho.API.Authenticator;
using Com.Zoho.API.Authenticator.Store;
using Com.Zoho.Crm.API;
using Com.Zoho.Crm.API.Dc;
using Com.Zoho.Crm.API.Logger;
using Com.Zoho.Crm.API.Record;
using Com.Zoho.Crm.API.Util;
using Newtonsoft.Json;
using SDKInitializer = Com.Zoho.Crm.API.Initializer;
namespace Com.Zoho.Crm.Sample.Threading.MultiUser
{
public class MultiThread
{
DataCenter.Environment environment;
UserSignature user;
Token token;
string moduleAPIName;
public MultiThread(UserSignature user, DataCenter.Environment environment, Token token, string moduleAPIName)
{
this.environment = environment;
this.user = user;
this.token = token;
this.moduleAPIName = moduleAPIName;
}
static void Main(string[] args)
{
Logger logger = new Logger.Builder()
.Level(Logger.Levels.ALL)
.FilePath("/Users/user_name/Documents/csharp_sdk_log.log")
.Build();
DataCenter.Environment environment1 = USDataCenter.PRODUCTION;
UserSignature user1 = new UserSignature("user1@zoho.com");
TokenStore tokenstore = new DBStore.Builder()
.Host("hostName")
.DatabaseName("dataBaseName")
.TableName("tableName")
.UserName("userName")
.Password("password")
.PortNumber("portNumber")
.Build();
Token token1 = new OAuthToken.Builder()
//.Id("userID")
.ClientId("clientId1")
.ClientSecret("clientSecret1")
.GrantToken("GRANT token")
.RefreshToken("REFRESH token")
.RedirectURL("redirectURL1")
.Build();
string resourcePath = "/Users/user_name/Documents/csharpsdk-application";
SDKConfig config = new SDKConfig.Builder().AutoRefreshFields(true).Build();
DataCenter.Environment environment2 = EUDataCenter.PRODUCTION;
UserSignature user2 = new UserSignature("user2@zoho.eu");
Token token2 = new OAuthToken.Builder()
//.Id("userID")
.ClientId("clientId2")
.ClientSecret("clientSecret2")
.GrantToken("GRANT token")
.RefreshToken("REFRESH token")
.RedirectURL("redirectURL2")
.Build();
new SDKInitializer.Builder()
.User(user1)
.Environment(environment1)
.Token(token1)
.Store(tokenstore)
.SDKConfig(config)
.ResourcePath(resourcePath)
.Logger(logger)
.Initialize();
MultiThread multiThread1 = new MultiThread(user1, environment1, token1, "Vendors");
Thread thread1 = new Thread(() => multiThread1.GetRecords());
thread1.Start();
MultiThread multiThread2 = new MultiThread(user2, environment2, token2, "Quotes");
Thread thread2 = new Thread(() => multiThread2.GetRecords());
thread2.Start();
thread1.Join();
thread2.Join();
}
public void GetRecords()
{
try
{
SDKConfig config = new SDKConfig.Builder().AutoRefreshFields(true).Build();
new SDKInitializer.Builder()
.User(this.user)
.Environment(this.environment)
.Token(this.token)
.SDKConfig(config).SwitchUser();
Console.WriteLine("Fetching records for user - " + SDKInitializer.GetInitializer().User.Email);
RecordOperations recordOperation = new RecordOperations();
APIResponse<ResponseHandler> response = recordOperation.GetRecords(this.moduleAPIName, null, null);
if (response != null)
{
//Get the status code from response
Console.WriteLine("Status Code: " + response.StatusCode);
if (new List<int>() { 204, 304 }.Contains(response.StatusCode))
{
Console.WriteLine(response.StatusCode == 204 ? "No Content" : "Not Modified");
return;
}
//Check if expected response is received
if (response.IsExpected)
{
//Get object from response
ResponseHandler responseHandler = response.Object;
if (responseHandler is ResponseWrapper)
{
//Get the received ResponseWrapper instance
ResponseWrapper responseWrapper = (ResponseWrapper)responseHandler;
//Get the list of obtained Record instances
List<API.Record.Record> records = responseWrapper.Data;
foreach (API.Record.Record record in records)
{
Console.WriteLine(JsonConvert.SerializeObject(record));
}
}
//Check if the request returned an exception
else if (responseHandler is APIException)
{
//Get the received APIException instance
APIException exception = (APIException)responseHandler;
//Get the Status
Console.WriteLine("Status: " + exception.Status.Value);
//Get the Code
Console.WriteLine("Code: " + exception.Code.Value);
Console.WriteLine("Details: ");
//Get the details map
foreach (KeyValuePair<string, object> entry in exception.Details)
{
//Get each value in the map
Console.WriteLine(entry.Key + ": " + JsonConvert.SerializeObject(entry.Value));
}
//Get the Message
Console.WriteLine("Message: " + exception.Message.Value);
}
}
}
}
catch (System.Exception ex)
{
Console.WriteLine(JsonConvert.SerializeObject(ex));
}
}
}
}
-
The program execution starts from Main().
-
The details of "user1" is given in the variables user1, token1, environment1.
-
Similarly, the details of another user "user2" is given in the variables user2, token2, environment2.
-
For each user, an instance of MultiThread class is created.
-
When the Start() is called which in-turn invokes the run(), the details of user1 are passed to the SwitchUser function through the MultiThread object. Therefore, this creates a thread for user1.
-
Similarly, When the Start() is invoked again, the details of user2 are passed to the SwitchUser function through the MultiThread object. Therefore, this creates a thread for user2.
using System;
using System.Collections.Generic;
using System.Threading;
using Com.Zoho.API.Authenticator;
using Com.Zoho.API.Authenticator.Store;
using Com.Zoho.Crm.API;
using Com.Zoho.Crm.API.Dc;
using Com.Zoho.Crm.API.Logger;
using Com.Zoho.Crm.API.Record;
using Com.Zoho.Crm.API.Util;
using Newtonsoft.Json;
using SDKInitializer = Com.Zoho.Crm.API.Initializer;
namespace Com.Zoho.Crm.Sample.Threading.SingleUser
{
/// <summary>
///
/// </summary>
public class MultiThread
{
static void Main(string[] args)
{
Logger logger = new Logger.Builder()
.Level(Logger.Levels.ALL)
.FilePath("/Users/user_name/Documents/csharp_sdk_log.log")
.Build();
DataCenter.Environment env = USDataCenter.PRODUCTION;
UserSignature user = new UserSignature("user1@zoho.com");
TokenStore tokenstore = new FileStore("/Users/user_name/Documents/csharp_sdk_token.txt");
Token token = new OAuthToken.Builder()
//.Id("userID")
.ClientId("clientId")
.ClientSecret("clientSecret1")
.GrantToken("GRANT token")
.RefreshToken("REFRESH token")
.RedirectURL("redirectURL1")
.Build();
string resourcePath = "/Users/user_name/Documents/csharpsdk-application";
SDKConfig config = new SDKConfig.Builder().AutoRefreshFields(true).Build();
new SDKInitializer.Builder()
.User(user)
.Environment(env)
.Token(token)
.Store(tokenstore)
.SDKConfig(config)
.ResourcePath(resourcePath)
.Logger(logger)
.Initialize();
MultiThread multiThread1 = new MultiThread();
Thread thread1 = new Thread(() => multiThread1.GetRecords("Quotes"));
thread1.Start();
Thread thread2 = new Thread(() => multiThread1.GetContactRoles());
thread2.Start();
thread1.Join();
thread2.Join();
}
public void GetRecords(string moduleAPIName)
{
try
{
Console.WriteLine("Fetching records for user - " + SDKInitializer.GetInitializer().User.Email);
RecordOperations recordOperation = new RecordOperations();
APIResponse<API.Record.ResponseHandler> response = recordOperation.GetRecords(moduleAPIName, null, null);
if (response != null)
{
//Get the status code from response
Console.WriteLine("Status Code: " + response.StatusCode);
if (new List<int>() { 204, 304 }.Contains(response.StatusCode))
{
Console.WriteLine(response.StatusCode == 204 ? "No Content" : "Not Modified");
return;
}
//Check if expected response is received
if (response.IsExpected)
{
//Get object from response
API.Record.ResponseHandler responseHandler = response.Object;
if (responseHandler is API.Record.ResponseWrapper)
{
//Get the received ResponseWrapper instance
API.Record.ResponseWrapper responseWrapper = (API.Record.ResponseWrapper)responseHandler;
//Get the list of obtained Record instances
List<API.Record.Record> records = responseWrapper.Data;
foreach (API.Record.Record record in records)
{
Console.WriteLine(JsonConvert.SerializeObject(record));
}
}
//Check if the request returned an exception
else if (responseHandler is API.Record.APIException)
{
//Get the received APIException instance
APIException exception = (APIException)responseHandler;
//Get the Status
Console.WriteLine("Status: " + exception.Status.Value);
//Get the Code
Console.WriteLine("Code: " + exception.Code.Value);
Console.WriteLine("Details: ");
//Get the details map
foreach (KeyValuePair<string, object> entry in exception.Details)
{
//Get each value in the map
Console.WriteLine(entry.Key + ": " + JsonConvert.SerializeObject(entry.Value));
}
//Get the Message
Console.WriteLine("Message: " + exception.Message.Value);
}
}
}
}
catch (System.Exception ex)
{
Console.WriteLine(JsonConvert.SerializeObject(ex));
}
}
public void GetContactRoles()
{
try
{
Console.WriteLine("Fetching Cr's for user - " + SDKInitializer.GetInitializer().User.Email);
ContactRolesOperations contactRolesOperations = new ContactRolesOperations();
APIResponse<ResponseHandler> response = contactRolesOperations.GetContactRoles();
if (response != null)
{
//Get the status code from response
Console.WriteLine("Status Code: " + response.StatusCode);
if (new List<int>() { 204, 304 }.Contains(response.StatusCode))
{
Console.WriteLine(response.StatusCode == 204 ? "No Content" : "Not Modified");
return;
}
//Check if expected response is received
if (response.IsExpected)
{
//Get object from response
ResponseHandler responseHandler = response.Object;
if (responseHandler is ResponseWrapper)
{
//Get the received ResponseWrapper instance
ResponseWrapper responseWrapper = (ResponseWrapper)responseHandler;
//Get the list of obtained Record instances
List<ContactRole> contactRoles = responseWrapper.ContactRoles;
foreach (ContactRole contactRole in contactRoles)
{
Console.WriteLine(JsonConvert.SerializeObject(contactRole));
}
}
//Check if the request returned an exception
else if (responseHandler is APIException)
{
//Get the received APIException instance
APIException exception = (APIException)responseHandler;
//Get the Status
Console.WriteLine("Status: " + exception.Status.Value);
//Get the Code
Console.WriteLine("Code: " + exception.Code.Value);
Console.WriteLine("Details: ");
//Get the details map
foreach (KeyValuePair<string, object> entry in exception.Details)
{
//Get each value in the map
Console.WriteLine(entry.Key + ": " + JsonConvert.SerializeObject(entry.Value));
}
//Get the Message
Console.WriteLine("Message: " + exception.Message.Value);
}
}
}
}
catch (System.Exception ex)
{
Console.WriteLine(JsonConvert.SerializeObject(ex));
}
}
}
}
-
The program execution starts from Main() where the SDK is initialized with the details of user and an instance of MultiThread class is created .
-
When the Start() is called which in-turn invokes the Run(), the moduleAPIName is switched through the method parameter. Therefore, this creates a thread for the particular method called with the MultiThread instance.
using System;
using System.Collections.Generic;
using Com.Zoho.API.Authenticator;
using Com.Zoho.API.Authenticator.Store;
using Com.Zoho.Crm.API;
using Com.Zoho.Crm.API.Dc;
using Com.Zoho.Crm.API.Logger;
using Com.Zoho.Crm.API.Record;
using Com.Zoho.Crm.API.Tags;
using Com.Zoho.Crm.API.Users;
using Com.Zoho.Crm.API.Util;
using Newtonsoft.Json;
using static Com.Zoho.API.Authenticator.OAuthToken;
using static Com.Zoho.Crm.API.Record.RecordOperations;
using Environment = Com.Zoho.Crm.API.Dc.DataCenter.Environment;
using ResponseHandler = Com.Zoho.Crm.API.Record.ResponseHandler;
using ResponseWrapper = Com.Zoho.Crm.API.Record.ResponseWrapper;
using SDKInitializer = Com.Zoho.Crm.API.Initializer;
namespace TestAutomatedSDK
{
public class MainClass
{
public static void Main(string[] args)
{
/*
* Create an instance of Logger Class that takes two parameters
* Level -> Level of the log messages to be logged. Can be configured by typing Levels "." and choose any level from the list displayed.
* FilePath -> Absolute file path, where messages need to be logged.
*/
Logger logger = new Logger.Builder()
.Level(Logger.Levels.ALL)
.FilePath("/Users/user_name/Documents/csharp_sdk_log.log")
.Build();
//Create an UserSignature instance that takes user Email as parameter
UserSignature user = new UserSignature("abc@zoho.com");
/*
* Configure the environment
* which is of the pattern Domain.Environment
* Available Domains: USDataCenter, EUDataCenter, INDataCenter, CNDataCenter, AUDataCenter
* Available Environments: PRODUCTION, DEVELOPER, SANDBOX
*/
Environment environment = USDataCenter.PRODUCTION;
/*
* Create a Token instance
*/
Token token = new OAuthToken.Builder()
//.Id("userID")
.ClientId("clientId")
.ClientSecret("clientSecret")
.GrantToken("GRANT token")
.RefreshToken("REFRESH token")
.RedirectURL("redirectURL")
.Build();
/*
* Create an instance of TokenStore.
*/
TokenStore tokenstore = new DBStore.Builder()
.Host("hostName")
.DatabaseName("dataBaseName")
.TableName("tableName")
.UserName("userName")
.Password("password")
.PortNumber("portNumber")
.Build();
//TokenStore tokenstore = new FileStore("absolute_file_path");
/*
* autoRefreshFields
* if true - all the modules' fields will be auto-refreshed in the background, every hour.
* if false - the fields will not be auto-refreshed in the background. The user can manually delete the file(s) or refresh the fields using methods from ModuleFieldsHandler(com.zoho.crm.api.util.ModuleFieldsHandler)
*
* pickListValidation
* if true - value for any picklist field will be validated with the available values.
* if false - value for any picklist field will not be validated, resulting in creation of a new value.
*/
SDKConfig config = new SDKConfig.Builder().AutoRefreshFields(true).PickListValidation(false).Build();
string resourcePath = "/Users/username/Documents/csharpsdk-application";
/*
* Call static initialize method of Initializer class that takes the arguments
* User -> UserSignature instance
* Environment -> Environment instance
* Token -> Token instance
* Store -> TokenStore instance
* SDKConfig -> SDKConfig instance
* ResourcePath -> resourcePath - A String
* Logger -> Logger instance
*/
new SDKInitializer.Builder()
.User(user)
.Environment(environment)
.Token(token)
.Store(tokenstore)
.SDKConfig(config)
.ResourcePath(resourcePath)
.Logger(logger)
.Initialize();
String moduleAPIName = "Leads";
RecordOperations recordOperations = new RecordOperations();
ParameterMap paramInstance = new ParameterMap();
paramInstance.Add(GetRecordsParam.APPROVED, "both");
HeaderMap headerInstance = new HeaderMap();
DateTimeOffset ifmodifiedsince = new DateTimeOffset(new DateTime(2020, 05, 15, 12, 0, 0, DateTimeKind.Local));
headerInstance.Add(GetRecordsHeader.IF_MODIFIED_SINCE, ifmodifiedsince);
//Call getRecords method
APIResponse<ResponseHandler> response = recordOperations.GetRecords(moduleAPIName, paramInstance, headerInstance);
if (response != null)
{
//Get the status code from response
Console.WriteLine("Status Code: " + response.StatusCode);
if (new List<int>() { 204, 304 }.Contains(response.StatusCode))
{
Console.WriteLine(response.StatusCode == 204 ? "No Content" : "Not Modified");
return;
}
//Check if expected response is received
if (response.IsExpected)
{
//Get the object from response
ResponseHandler responseHandler = response.Object;
if (responseHandler is ResponseWrapper)
{
//Get the received ResponseWrapper instance
ResponseWrapper responseWrapper = (ResponseWrapper)responseHandler;
//Get the obtained Record instances
List<Com.Zoho.Crm.API.Record.Record> records = responseWrapper.Data;
foreach (Com.Zoho.Crm.API.Record.Record record in records)
{
//Get the ID of each Record
Console.WriteLine("Record ID: " + record.Id);
//Get the createdBy User instance of each Record
User createdBy = record.CreatedBy;
//Check if createdBy is not null
if (createdBy != null)
{
//Get the ID of the createdBy User
Console.WriteLine("Record Created By User-ID: " + createdBy.Id);
//Get the name of the createdBy User
Console.WriteLine("Record Created By User-Name: " + createdBy.Name);
//Get the Email of the createdBy User
Console.WriteLine("Record Created By User-Email: " + createdBy.Email);
}
//Get the CreatedTime of each Record
Console.WriteLine("Record CreatedTime: " + record.CreatedTime);
//Get the modifiedBy User instance of each Record
User modifiedBy = record.ModifiedBy;
//Check if modifiedBy is not null
if (modifiedBy != null)
{
//Get the ID of the modifiedBy User
Console.WriteLine("Record Modified By User-ID: " + modifiedBy.Id);
//Get the name of the modifiedBy User
Console.WriteLine("Record Modified By User-Name: " + modifiedBy.Name);
//Get the Email of the modifiedBy User
Console.WriteLine("Record Modified By User-Email: " + modifiedBy.Email);
}
//Get the ModifiedTime of each Record
Console.WriteLine("Record ModifiedTime: " + record.ModifiedTime);
//Get the list of Tag instance each Record
List<Tag> tags = record.Tag;
//Check if tags is not null
if (tags != null)
{
foreach (Tag tag in tags)
{
//Get the Name of each Tag
Console.WriteLine("Record Tag Name: " + tag.Name);
//Get the Id of each Tag
Console.WriteLine("Record Tag ID: " + tag.Id);
}
}
//To get particular field value
Console.WriteLine("Record Field Value: " + record.GetKeyValue("Last_Name"));// FieldApiName
Console.WriteLine("Record KeyValues: ");
//Get the KeyValue map
foreach (KeyValuePair<string, object> entry in record.GetKeyValues())
{
string keyName = entry.Key;
object value = entry.Value;
if (value != null)
{
Console.WriteLine("Field APIName : " + keyName + "\tValue : " + JsonConvert.SerializeObject(value));
}
}
}
}
}
}
}
}
}