CRM-service CIWS-kirjasto REST-rajapintaan

Yleistä

CRM-service CIWS on asiakaskirjasto, jota voidaan käyttää REST-rajapinnassamme.

Vaatimukset

  • .NET Framework 4.5 tai uudempi

Esimerkkikoodi

using System;
using System.Collections.Generic;
using System.Linq;
using CRMservice.ciws.rest;
using CRMservice.ciws.rest.Authenticators;
using CRMservice.ciws.rest.Core;
using MongoDB.Driver;

namespace SyncInterfaceService
{
 class Program
 {
  [CrmModule(Module.Accounts)]
  public class CrmAccountForRelation
  {
   [CrmFieldName("id")]
   public string Id { get; set; }
   public string Type { get; set; } = "accounts";
  }
 
  [CrmModule(Module.Accounts)]
  public class CrmAccount
  {
    public string Type { get; set; } = "accounts";
    [CrmFieldName("id")] public string Id { get; set; }
    [CrmFieldName("name")] public string Name { get; set; }
    [CrmFieldName("cf_1617")] public string CustomField { get; set; }
  }
 
  [CrmModule(Module.Contacts)]
  public class CrmContact
  {
   [CrmFieldName("id")]
   public string Id { get; set; }
   public string Type { get; set; } = "contacts";
   [CrmFieldName("first_name")]
   public string Firstname { get; set; }
   [CrmFieldName("last_name")]
   public string Lastname { get; set; }
   [CrmFieldName("passivation_date")]
   public DateTime? PassivationDate { get; set; }
   [CrmRelation(ContactsRelations.Account)]
   public CrmAccountForRelation Account { get; set; }
  }
 
  public class CrmManager : CrmManagerBase
  {
   public CrmAccount UpdateAccount(CrmAccount account)
   {
    return BulkEntitySave(account);
   }

   public List UpdateMultipleAccounts(List accounts)
   {
    return BulkEntitySave(accounts);
   }

   public CrmAccount SearchForAccount(string crmId)
   {
    var search = new CrmSearch
    {
     Limit = 1,
     Module = Module.Accounts
    };
    var filter = Builders.Filter.Eq("id", crmId);
    return ExtendedSearch(search, filter).FirstOrDefault();
   }

   public CrmContact SearchForContact(string crmId)
   {
    var search = new CrmSearch
    {
     Limit = 1,
     Module = Module.Contacts
    };
    var filter = Builders.Filter.Eq("id", crmId);
    return ExtendedSearch(search, filter).FirstOrDefault();
   }

   public IEnumerable<List> GetAllAccounts()
   {
    var search = new CrmSearch
    {
     Buffer = 100,
     Module = Module.Accounts
    };

    var searchYield = ExtendedSearchYield(search);
    foreach (var yieldedList in searchYield)
    {
     yield return yieldedList;
    }
   }

   static void Main()
   {
    var crmManager = new CrmManager();
    crmManager.Authenticate("https://crmaddress.fi", new CrmJwtAuthenticator("username", "password"));

    var account = crmManager.SearchForAccount("1234");
    account.Name = "My Company";
    var updated = crmManager.UpdateAccount(account);

    IEnumerable<List> yieldedAccounts = crmManager.GetAllAccounts();
    foreach (var listAccounts in yieldedAccounts)
    {
     //this will give you list of the size defined in search buffer
     //and exit after all accounts have been handled
    }

    var contact = new CrmContact
    {
     Firstname = "Matti",
     Lastname = "Meikäläinen",
     Account = new CrmAccountForRelation {Id = account.Id}
    };
    //Create new entities by saving data without Id value
    var crmContact = crmManager.BulkEntitySave(contact);

    var searchedContact = crmManager.SearchForContact(crmContact.Id);
    if (searchedContact.Account.Id == account.Id)
    {
     //relation data is loaded for searched contact because of attribute,
     //but it must be manually maintained in creation

     Console.WriteLine(searchedContact.Firstname); //Matti
    }
   }
  }
 }
}
Web Service -rajapinnat
CRM-service CIWS-kirjasto
REST-rajapinta
Combined Shape