Skip to main content

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
  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

World Citizen said…
Dear Swati, is this folder path a typo?

yourwebsite.xconnect\App_data\Models xConnect indexing
Swati Gupta said…
Thanks for informing, Corrected it.

Popular posts from this blog

Sitecore 10.2 - “Failed to start service ‘Sitecore Marketing Automation Engine’” on Windows 11

Sitecore 10.2 - “Failed to start service ‘Sitecore Marketing Automation Engine' ” on Windows 11 Today I started to install Sitecore 10.2 using Sitecore Instance Manager on Windows 11 and I got this issue “Failed to start service ‘Sitecore Marketing Automation Engine' ” . Error : On event viewer it was showing the below error: I also tried to run ‘ Sitecore.MAEngine.exe ’ like this C:\Windows\system32>C:\inetpub\wwwroot\sclocal102xconnect.dev.local\App_Data\jobs\continuous\AutomationEngine\Sitecore.MAEngine.exe Which was throwing below error: Starting Marketing Automation Engine... 2022-01-29 22:21:11 ERR Error initializing XConnect client. System.AggregateException: One or more errors occurred. ---> Sitecore.XConnect.XdbCollectionUnavailableException: An error occurred while sending the request. ---> System.Net.Http.HttpRequestException: An error occurred while sending the request. ---> System.Net.WebException: The underlying connection was closed: An unexpected err

Azure AD Integration with Sitecore 10.2

 Azure AD Integration with Sitecore 10.2 Sitecore identity server that comes with Sitecore 9.1 allows you to log in through an external identity provider like Azure Active Directory, Facebook, Apple, or Google. It is built on Federation Authentication. What is Federation Authentication? Federation authentication is a technology to allows users to access multiples application, tools, and domains using one credential. Using one set of credential user can access multiple applications, and resources after authentication.  Federation authentication consists of two systems, the Identity provider and the Service provider. Identity providers that maintain/create/manage identity information like name, email address, device, and location. Some examples of identity providers are Azure AD, Google, Facebook, and Apple. Service providers basically refer to a website, software, or app that the user is trying to access and SP basically relies on the identity provider to authenticate the user and provi

Sitecore Custom API Issue with Federation Authentication

Sitecore Custom API Issue with Federation Authentication In earlier segments, detailed in Part 1 and Part 2 of the blogs on Keycloak Integration with Sitecore, I introduced Keycloak functionality for CM login. Concurrently, I addressed a necessity to develop custom APIs for retrieving Sitecore users and roles. Following the development of custom APIs, during authentication failures, the API erroneously returned a status code of 200 instead of 401. The problem arose because API requests were being routed through the "owin.identityProviders" pipeline, which was not intended for API usage. Solution: When OWIN identifies a 401 response and the AuthenticationMode is set to "Active," it fails to capture the URL hash included in the request. Another choice is to activate the "Passive" AuthenticationMode, wherein OWIN refrains from actively intercepting 401 responses. In passive mode, your application needs to explicitly issue a Challenge to trigger the OWIN aut