Sitecore 9 Dynamic CRM Connector: Create custom facets (Part 1)

Sitecore 9 Dynamic CRM Connector: Create custom facets (Part 1)

crm, crm connector, custom facet, dynamic crm, microsoft dynamic crm, Sitecore


Dynamic CRM Connector will provide you default facets like first name, last name etc. but you can also create custom facets and sync to CRM.

Step 1: Create a Visual Studio project

  • Create Class Library (.Net Framework) project
  • Add below reference from NuGet: 
  1. Sitecore.DataExchange.Tools.DynamicsConnect.NoReferences
  2. Sitecore.XConnect.NoReferences

Step 2: Implementation of Custom facets

  • Create new class to create custom facet “Student” and add following code:
 using Sitecore.XConnect;   
  using System;   
  namespace YourNamespace   
  {   
   [FacetKey(DefaultFacetKey)]   
   [Serializable]   
   public class StudentFacets: Facet   
   {   
    public const string DefaultFacetKey = "Student";   
    public string GraduateYear { get; set; }   
   }   
  }   

Step 3: Implementation of collection model

After creating Custom facets class, you need to create Collection model which define available contact facet.
 using Sitecore.DataExchange.Tools.DynamicsConnect.Models;   
  using Sitecore.XConnect;   
  using Sitecore.XConnect.Schema;   
  namespace YourNamespace   
  {   
   public class StudentModel   
   {   
    public StudentModel()   
    {   
    }   
    static StudentModel()   
    {   
     _model = BuildModel();   
    }   
    private static XdbModel _model = null;   
    public static XdbModel Model { get { return _model; } }   
    private static XdbModel BuildModel()   
    {   
     var builder = new XdbModelBuilder(typeof(StudentModel).FullName, new XdbModelVersion(1, 0));   
     builder.DefineFacet<Contact, StudentFacets>(StudentFacets.DefaultFacetKey);   
     builder.ReferenceModel(DynamicsConnectCollectionModel.Model);   
     return builder.BuildModel();   
    }   
   }   
  }   

Step 4: Deploy the model

  • Build your solution and copy DLL into bin directory of your Sitecore instance.
  • Patch your custom model into \App_Config\Sitecore\XConnect.Client.Configuration\Sitecore.XConnect.Client.config
 <?xml version="1.0"?>   
  <configuration xmlns:patch="http://www.sitecore.net/xmlconfig/" xmlns:set="http://www.sitecore.net/xmlconfig/set/">   
  <sitecore>   
   <xconnect>   
   <runtime type="Sitecore.XConnect.Client.Configuration.RuntimeModelConfiguration,Sitecore.XConnect.Client.Configuration">   
    <schemas hint="list:AddModelConfiguration">   
    <schema name="StudentModel" type="Sitecore.XConnect.Client.Configuration.StaticModelConfiguration,Sitecore.XConnect.Client.Configuration" patch:after="schema[@name='collectionmodel']">   
     <param desc="modeltype">YourNamespace.StudentModel,AssemblyName</param>   
    </schema>   
    </schemas>   
   </runtime>   
   </xconnect>   
  </sitecore>   
  </configuration>  

Step 5: Add a collection model definition

To define collection model in Sitecore follow below steps:
  • Navigate to this location “/sitecore/system/Settings/Data Exchange/Providers/xConnect/Collection Models”
  • Add the following item.
  1. Template :Collection Model Folder 
  2. Item Name: Custom Models 
  • Select the new item.
  • Add the following item.
  1. Template :Compiled Collection Model 
  2.  Item Name: Custom Collection Model for Dynamics
  • In “Collection Model Type” field set “YourNamespace.StudentModel,AssemblyName”

Step 6: Convert Model to JSON

  • Select Custom Collection Model for Dynamics item
  • To convert Model into Json click on “Convert Model to Json” In the ribbon, it will download model
  • Deploy the model file to the following location:

  1. yourwebsite.xconnect\App_data\Models xConnect indexing
  2. yourwebsite.xconnect\App_data\jobs\continuous\IndexWorker\App_data\Models

Step 7: Configure the xConnect Client Endpoint

  • Navigate to this location “/sitecore/system/Data Exchange/XXX/Endpoints/Providers/xConnect/xConnect Client Endpoint”
  • Select “Collection Models/Custom Model/Custom Collection Model for Dynamics” in “Collection Model” field

Step 8: Run Troubleshooter


  • In ribbon, click on “Run Troubleshooter” to ensure that the collection model is deployed and xConnect client endpoint is configured correctly.
  • If everything is OK, it will show popup message that “Connection was successfully established”

Step 9: Verify in Database

  • When you will save custom facets, you can check in DB that custom facets is saving in DB or not. Below are the table name:
  1. [XXX_Xdb.Collection.Shard0].[xdb_collection].[ContactFacets]
  2. [XXX_Xdb.Collection.Shard1].[xdb_collection].[ContactFacets]

In next article, we are going to sync these custom facets with CRM.

Comments

Popular Posts