C# SDK - Bulk Read API
Use the below code to schedule a bulk read job to export records that match the criteria. This job returns a "job_id" in the response. For more details, refer to the Create a Bulk Read Job page of our API guide.
public void CreateBulkReadJob()
{
try
{
ZCRMBulkRead bulkReadrecordIns = ZCRMRestClient.GetInstance().GetBulkReadInstance("ModuleAPIName"); //To get the ZCRMBulkRead instance of the module
ZCRMCriteria group1 = ZCRMCriteria.GetInstance(); // To get ZCRMCriteria instance
group1.GroupOperator = "or"; // To set criteria group_operator(Supported values - and, or).
List<ZCRMCriteria> grouplist1= new List<ZCRMCriteria>(); // List of ZCRMCriteria instance
ZCRMCriteria group2 = ZCRMCriteria.GetInstance();
group2.GroupOperator = "and";
List<ZCRMCriteria> grouplist2 = new List<ZCRMCriteria>();
ZCRMCriteria group3 = ZCRMCriteria.GetInstance();
group3.GroupOperator = "or";
List<ZCRMCriteria> grouplist3 = new List<ZCRMCriteria>();
ZCRMCriteria group4 = ZCRMCriteria.GetInstance();
group4.FieldAPIName = "Last_Name"; // To set API name of a field.
group4.Comparator = "contains"; // To set comparator(eg: equal, greater_than.).
group4.Value = "John";// To set the value to be compare.
grouplist3.Add(group4); // To add ZCRMCriteria instance.
ZCRMCriteria group5 = ZCRMCriteria.GetInstance();
group5.FieldAPIName = "City";
group5.Comparator = "equal";
group5.Value = "Chennai";
grouplist3.Add(group5);
group3.Group = grouplist3;
grouplist2.Add(group3);
ZCRMCriteria group21 = ZCRMCriteria.GetInstance();
group21.FieldAPIName = "Company";
group21.Comparator = "equal";
group21.Value = "Zoho";
grouplist2.Add(group21);
group2.Group = grouplist2;
grouplist1.Add(group2);
ZCRMCriteria group11 = ZCRMCriteria.GetInstance();
group11.FieldAPIName = "Email";
group11.Comparator = "equal";
group11.Value = "Email@zoho.com";
grouplist1.Add(group11);
group1.Group = grouplist1;
ZCRMBulkQuery query = ZCRMBulkQuery.GetInstance();
query.CvId = 347706087501; // To set custom view id of the module
List<string> fields = new List<string>(); // List of Field API Names
fields.Add("First_Name");
fields.Add("Last_Name");
fields.Add("Email");
query.Fields = fields;// To set list of API Name of the fields to be fetched.
query.Page = 1; // To set page value, By default value is 1.
query.Criteria = group1; // To set ZCRMCriteria instance.
bulkReadrecordIns.Query = query; // To set ZCRMBulkQuery instance.
bulkReadrecordIns.FileType = "ics"; // Set the value for this key as "ics" to export all records in the Events module as an ICS file.
ZCRMBulkCallBack callBack = ZCRMBulkCallBack.GetInstance(); // To get ZCRMBulkCallBack instance
callBack.Url = "https://www.zoho.com/"; // To set callback URL.
callBack.Method = "post"; // To set the HTTP method of the callback url. The allowed value is post.
bulkReadrecordIns.CallBack = callBack;// To set ZCRMBulkCallBack instance
APIResponse response = bulkReadrecordIns.CreateBulkReadJob(); // To create Bulk read job.
Console.WriteLine(response.Message);
Console.WriteLine(response.Status);
Console.WriteLine(response.HttpStatusCode);
Console.WriteLine(response.ResponseJSON);
ZCRMBulkRead readIns = (ZCRMBulkRead) response.Data; // To get the ZCRMBulkRead instance.
Console.WriteLine(readIns.CreatedTime);
Console.WriteLine(readIns.Operation);
Console.WriteLine(readIns.State);
Console.WriteLine(readIns.JobId);// To get the job_id of bulk read job.
ZCRMUser created_by = readIns.CreatedBy;
if(created_by != null)
{
Console.WriteLine(created_by.Id);
Console.WriteLine(created_by.FullName);
}
}
catch(Exception ex)
{
Console.WriteLine(ex.StackTrace);
}
}
// Sample request
// API Format: [[[{Last_Name:contains:John} or {City:equal:Chennai}] and {Company:equal:Zoho}] or {Email:equal:Email@zoho.com}]
// SQL Format: ((((Last_Name:contains:John)or(City:equal:Chennai))and(Company:equal:Zoho))or(Email:equal:Email@zoho.com))
Use the below code to know the status of the bulk read job scheduled previously. For more details, refer to Get Job Details page of our API guide.
public void GetBulkReadJobDetails()
{
try
{
ZCRMBulkRead readIns= ZCRMRestClient.GetInstance().GetBulkReadInstance(3477061000002); // To get the ZCRMBulkRead instance using job_id
APIResponse response = readIns.GetBulkReadJobDetails();
ZCRMBulkRead bulkReadIns = (ZCRMBulkRead)response.Data;// To get ZCRMBulkRead instance
Console.WriteLine(bulkReadIns.JobId);
Console.WriteLine(bulkReadIns.Operation);
Console.WriteLine(bulkReadIns.State);
ZCRMBulkResult result = bulkReadIns.Result;// To get ZCRMBulkResult instance(It is available only after the completion of the job).
Console.WriteLine(result.Count);// To get the actual number of records exported.
Console.WriteLine(result.DownloadUrl); // To get the url which contains the CSV file.
Console.WriteLine(result.MoreRecords);
Console.WriteLine(result.Page);
ZCRMBulkQuery query = bulkReadIns.Query;// To get ZCRMBulkQuery instance.
foreach(string fieldName in query.Fields)// To get list of Fields.
{
Console.WriteLine(fieldName);
}
Console.WriteLine(query.ModuleAPIName);
Console.WriteLine(query.CvId);// To get module custom view id.
Console.WriteLine(query.Page);
ZCRMCriteria criteria = query.Criteria;
if (criteria != null)
{
Console.WriteLine(criteria.FieldAPIName);
Console.WriteLine(criteria.Comparator);
Console.WriteLine(criteria.Value);
Console.WriteLine(criteria.GroupOperator);
foreach (ZCRMCriteria criteria1 in criteria.Group)
{
Console.WriteLine(criteria1.FieldAPIName);
Console.WriteLine(criteria1.Comparator);
Console.WriteLine(criteria1.Value);
Console.WriteLine(criteria1.GroupOperator);
Console.WriteLine(JsonConvert.SerializeObject(criteria1.Group));
}
}
Console.WriteLine(query.CriteriaPattern);// To get query criteria pattern(eg:(1and(((2or3)and4)or5))).
Console.WriteLine(query.CriteriaCondition);// To get query criteria condition(eg:(($converted:equal:False)and((((Last_Name:contains:John)or(Email_Opt_Out:equal:True))and(Company:equal:Zoho))or(Email:equal:Email@zoho.com))))).
ZCRMUser user = bulkReadIns.CreatedBy;
if(user!=null)
{
Console.WriteLine(user.Id);
Console.WriteLine(user.FullName);
}
Console.WriteLine(bulkReadIns.CreatedTime);
}
catch(Exception ex)
{
Console.WriteLine(ex.StackTrace);
}
}
Use the below code to download the result of the bulk read job. The response contains a zip file. Extract it to get the CSV or ICS file depending on the "file_type" you specified while creating the bulk read job. For more details, refer to the Download Result page of our API guide.
public void DownloadBulkReadResult()
{
try
{
ZCRMBulkRead readIns = ZCRMRestClient.GetInstance().GetBulkReadInstance(31443322); // To get the ZCRMBulkRead instance using job_id
FileAPIResponse respone = readIns.DownloadBulkReadResult();
Stream file = respone.GetFileAsStream();// To get response file stream.
CommonUtil.SaveStreamAsFile("/Users/Desktop/", file, respone.GetFileName());//To download stream as zip.
file.Close();
Console.WriteLine(respone.Status);
}
catch(Exception ex)
{
Console.WriteLine(ex.StackTrace);
}
}
Use the below code to download the zip file, extract the CSV file, and display the records as instances.
/** Download the zip and get the records as instances*/
public void DownloadandGetRecords()
{
try
{
ZCRMBulkRead readIns = ZCRMRestClient.GetInstance().GetBulkReadInstance(31443322); // To get the ZCRMBulkRead instance using job_id
BulkResponse response = readIns.DownloadANDGetRecords("/Users/Desktop");// To download the zip and get list of record instances.
while(response.HasNext())// To iterate class instance
{
ZCRMRecord record = response.Next();// To get ZCRMRecord instance.
Console.WriteLine(record.EntityId);// To get record Id.
Console.WriteLine(record.CreatedTime);
Console.WriteLine(record.GetFieldValue("Company"));//To get particular field value.
Console.WriteLine(record.ModifiedTime);
Console.WriteLine(record.ModuleAPIName);
ZCRMUser owner=(record.Owner);
if(owner!=null)
{
Console.WriteLine(owner.Id);
}
ZCRMUser modified_by=(record.ModifiedBy);
if(modified_by != null)
{
Console.WriteLine(modified_by.Id);
}
ZCRMUser created_by=(record.CreatedBy);
if(created_by != null)
{
Console.WriteLine(created_by.Id);
}
foreach(KeyValuePair<string,object> data in record.Data)
{
Console.WriteLine("key is: " + data.Key + " & Value is: " + data.Value);
}
// response.Close(); // use this method close the file reader
}
}
catch(Exception ex)
{
Console.WriteLine(ex.StackTrace);
}
}
Use the below code to download the zip file, extract the ICS file, and display the records as instances.
/** Download the zip and Get the records as instances*/
/** Use this method for ics file type supported module */
public void DownloadandGetRecords()
{
ZCRMBulkRead readIns = ZCRMRestClient.GetInstance().GetBulkReadInstance(31443322); // To get the ZCRMBulkRead instance using job_id
BulkResponse response = readIns.DownloadANDGetRecords("/Users/Desktop");// To download the zip and get list of record instances.
foreach (KeyValuePair<string,object> EventsRecords in response.Data)
{
Console.WriteLine(EventsRecords.Key);
Console.WriteLine(EventsRecords.Value);
if (EventsRecords.Key.Equals("EventsData"))
{
BulkResponse events = (BulkResponse)EventsRecords.Value;
while (events.HasNext())
{
ZCRMRecord record = events.Next();
foreach (KeyValuePair<string,object> data in record.Data)
{
Console.WriteLine("key is: " + data.Key + " & Value is: " + data.Value);
}
//events.Close();// use this method close the file reader
}
}
}
}
Use the below code to fetch all the records from the CSV file mentioned in the file path.
public void GetRecordsFromFile()
{
try
{
ZCRMBulkRead readIns= ZCRMBulkRead.GetInstance("ModuleAPIName");// To get the ZCRMBulkRead instance of the module
BulkResponse response = readIns.GetRecords("/Users/Desktop", "zip_file_name");// Absolute path of the downloaded zip and the name of the zip file to fetch record.
while (response.HasNext())
{
ZCRMRecord record = response.Next();
Console.WriteLine(record.EntityId);
Console.WriteLine(record.CreatedTime);
Console.WriteLine(record.GetFieldValue("Company"));
Console.WriteLine(record.ModifiedTime);
Console.WriteLine(record.ModuleAPIName);
ZCRMUser owner = (record.Owner);
if (owner != null)
{
Console.WriteLine(owner.Id);
}
ZCRMUser modified_by = (record.ModifiedBy);
if (modified_by != null)
{
Console.WriteLine(modified_by.Id);
}
ZCRMUser created_by = (record.CreatedBy);
if (created_by != null)
{
Console.WriteLine(created_by.Id);
}
foreach (KeyValuePair<string,object> data in record.Data)
{
Console.WriteLine("key is: " + data.Key + " & Value is: " + data.Value);
}
// response.Close(); // use this method close the file reader
}
}
catch(Exception ex)
{
Console.WriteLine(ex.StackTrace);
}
}
Use the below code to fetch all the records from the ICS file mentioned in the file path.
public void GetRecordsFromFile()
{
ZCRMBulkRead readIns = ZCRMBulkRead.GetInstance("ModuleAPIName");// To get the ZCRMBulkRead instance of the module
BulkResponse response = readIns.GetRecords("/Users/Desktop", "zip_file_name");// Absolute path of the downloaded zip and the name of the zip file to fetch record.
foreach(KeyValuePair<string,object> EventsRecords in response.Data)
{
Console.WriteLine(EventsRecords.Key);
Console.WriteLine(EventsRecords.Value);
if(EventsRecords.Key.Equals("EventsData"))
{
BulkResponse events = (BulkResponse)EventsRecords.Value;
while (events.HasNext())
{
ZCRMRecord record = events.Next();
foreach (KeyValuePair<string,object> data in record.Data)
{
Console.WriteLine("key is: " + data.Key + " & Value is: " + data.Value);
}
//events.Close();// use this method close the file reader
}
}
}
}