Bitrix24 Connector is a small simple library built to work with Rest API Bitrix24


Keywords
Api, Bitrix24, Client, HappyProg
License
MIT
Install
Install-Package Bitrix24.Connector -Version 0.3.20

Documentation

Nuget Nuget

Bitrix24 .NET Client

Bitrix24 Connector is a small simple library built to work with Rest API Bitrix24. Currently under development...

Getting started

Bitrix24 connector can be registered and used with Microsoft.Extensions.DependencyInjection. The HttpClient instance is injected in DI container (IHttpClientFactory). To use, with an IServiceCollection:

 using Bitrix24.Connector.DependecyInjection;
 
 // ....
  
services.AddBitrix24Client("Bitrix24:Hook");

Or create Bitirx24 connector manually using constructors:

 // Better Approach
 var client = new Bitrix24Client(url); 

or

 // Bad way
 var client = Bitrix24Client(HttpClient client)  

This is a bad way! It is highly recommended to use Microsoft.Extensions.DependencyInjection with services.AddHttpClient<IBitrix24Client, Bitrix24Client>

Usage

  • FirstOrDefaultAsync
   var contact = await _client.Crm.Contacts
         .Select(s => s.Id, s => s.Phone, s => s.Email, 
               s => s.AddressCity,
               s => s.Birthdate, s => s.Name,
               s => s.LastName, s => s.SecondName, s => s.DateModify)
         .FirstOrDefaultAsync(x => x.Email == "mrbrown@mail.com", cancellationToken)
         .ConfigureAwait(false);
  • ToListAsync
   var contacts = await _connector.Crm.Contacts
         .Select(s => s.Id, s => s.Phone, s => s.Email, 
               s => s.AddressCity,
               s => s.Birthdate, s => s.Name,
               s => s.LastName, s => s.SecondName, s => s.DateModify)
         .ToListAsync(x => x.Email == "mrbrown@mail.com", cancellationToken)
         .ConfigureAwait(false);

Inheritance

   public class Bitrix24Customer : Bitrix24Contact
   {
       [Bitrix24Property("UF_PARENT_ID")]
       public int? ParentId { get; set; }

       [Bitrix24Property("UF_GUID")]
       public Guid? CustomerId { get; set; }
   }
        
   var contact = await _client.Crm.Contacts
         .Select<Bitrix24Customer>(s => s.Id, s => s.Phone, s => s.Email, 
               s => s.AddressCity, s => s.ParentId,
               s => s.Birthdate, s => s.Name, s => s.CustomerId,
               s => s.LastName, s => s.SecondName, s => s.DateModify)
         .FirstOrDefaultAsync<Bitrix24Customer>(x => x.Email == "mrbrown@mail.com" && x.ParentId == 1, cancellationToken)
         .ConfigureAwait(false);
         
   var contacts = await _connector.Crm.Contacts
         .Select<Bitrix24Customer>(s => s.Id, s => s.Phone, s => s.Email, 
               s => s.AddressCity, s => s.ParentId,
               s => s.Birthdate, s => s.Name,
               s => s.LastName, s => s.SecondName, s => s.DateModify)
         .ToListAsync<Bitrix24Customer>(x => x.Email == "mrbrown@mail.com" && x.ParentId == 1, cancellationToken)
         .ConfigureAwait(false);         

Filter (IQueryable)

var query = _connector.Crm.Contacts
    .Select(s => s.Id, s => s.Phone, s => s.Email,
        s => s.AddressCity,
        s => s.Birthdate, s => s.Name,
        s => s.LastName, s => s.SecondName, s => s.DateModify)
    .Filter(x => x.Email == "mrbrown@mail.com");
    
var contacts = await query
   .ToListAsync(cancellationToken)
   .ConfigureAwait(false);

Also, any commands can be executed using IBitrix24RequestHandler:

 public interface IBitrix24RequestHandler
 {
     Task<string> ExecuteAsync(string command, object query = null);
     Task<Bitrix24Response<T>> ExecuteAsync<T>(string command, object query = null);
     Task<T> ExecuteDefaultAsync<T>(string command, object query = null);
 }
  • command - example: user.get
  • query - Bitrix24Query or any custom object
 var users = await _connector.RequestHandler.ExecuteAsync<IEnumerable<Bitrix24User>>("user.get", query);