0

I am working with XDB to get Contact by Contact-Id using LoadContact method in Sitecore 9 Update 2, but it returns me the null value of contact, The same contact-Id exist in my Shard0 database with facets values. To do the same I am using below code:

public object GetContactCustomFields(Guid contactId)
    {
        try
        {
            var contactManager = Sitecore.Configuration.Factory.CreateObject("tracking/contactManager", true) as Sitecore.Analytics.Tracking.ContactManager;
            var contact = contactManager.LoadContact(contactId, true);
            if (contact == null)
            {
                throw new ContactNotFoundException();
            }
            var customFacet = contact.GetFacet<IFacetModelName>(FacetModelName.DefaultKey);
            return customFacet;
        }
        catch (ContactNotFoundException ex)
        {
            return Request.CreateResponse(HttpStatusCode.NotFound, ex.Message);
        }
    }

Here Contact type is Sitecore.Analytics.Tracking.Contact instead of Sitecore.XConnect.Contact. Because of by using this contact I am trying to get custom facet data and want to display on the experience profile tab.

After updating the code as suggested in answer by @x3mxray, I am getting below error:

Update Code:

[HttpGet]
    public async Task<StickyFormPreOTPInformation> GetContactCustomFields(Guid contactId)
    {
        using (Sitecore.XConnect.Client.XConnectClient client = Sitecore.XConnect.Client.Configuration.SitecoreXConnectClientConfiguration.GetClient())
        {
            try
            {
                Sitecore.XConnect.ContactReference reference = new Sitecore.XConnect.ContactReference(contactId);
                var contactTask = client.GetAsync<Contact>(
                    reference,
                    new Sitecore.XConnect.ContactExpandOptions(StickyFormPreOTPInformation.FACET_NAME)
                );

                var contact = await contactTask;

                if (contact == null)
                {
                    return null;
                }
                var customFacet = contact.GetFacet<IStickyFormPreOTPInformation>(StickyFormPreOTPInformation.FACET_NAME);
                return customFacet;
            }
            catch (Sitecore.XConnect.XdbExecutionException)
            {
                // TODO: catch exception 
            }
        }
        return null;
    }

Error:

enter image description here

Update Code 2: After updating client.GetAsync, Contact from Sitecore.XConnect namespace, getting below error:

enter image description here

1 Answer 1

3

You should extract Contact from XConnect (Shards databases related to xDb).

public async Task<FacetModelName> GetContactCustomFields(Guid contactId)
{
    using (XConnectClient client = Sitecore.XConnect.Client.Configuration.SitecoreXConnectClientConfiguration.GetClient())
    {
        try
        {
           ContactReference reference = new ContactReference(contactId);
            var contactTask = client.GetAsync<Contact>(
                reference,
                new ContactExpandOptions(FacetModelName.DefaultKey)
            );

            var contact = await contactTask;

            if (contact == null)
            {
                return null;
            }

            var customFacet = contact.GetFacet<FacetModelName>(FacetModelName.DefaultKey);
            return customFacet;

        }
        catch (XdbExecutionException ex)
        {
            // TODO: catch exception 
        }
    }

    return null;
}

FacetModelName:

    [Serializable]
    [FacetKey(DefaultFacetKey)]
    public class FacetModelName : Facet
    {
        public const string DefaultFacetKey = "MyFacet";

        public FacetModelName()
        {

        }

        // define needed properties
    }

To make your facet visible for the xConnect and xconnect jobs, you need to build custom model where we register new facet.

public static class XdbCustomModel
    {
        public static XdbModel Model { get; } = BuildModel();

        private static XdbModel BuildModel()
        {
            XdbModelBuilder modelBuilder = new XdbModelBuilder("CustomModel", new XdbModelVersion(1, 0));

            modelBuilder.ReferenceModel(Sitecore.XConnect.Collection.Model.CollectionModel.Model);
            modelBuilder.DefineFacet<Contact, FacetModelName>(FacetModelName.DefaultFacetKey);

            return modelBuilder.BuildModel();
        }
    }

Then you need generate json file:

 var fileName = XdbCustomModel.Model.FullName + ".json";
 var json = XdbModelWriter.Serialize(XdbCustomModel.Model);
 System.IO.File.WriteAllText(fileName, json);

Copy this generated json file to Model folder of xConnect and Model folder of xConnect jobs:

xconnect_instance\App_Data\Models\ xconnect_instance\App_Data\jobs\continuous\ProcessingEngine\App_Data\Models\ xconnect_instance\App_Data\jobs\continuous\AutomationEngine\App_Data\Models\ xconnect_instance\App_Data\jobs\continuous\IndexWorker\App_data\Models\

For more information about custom facets see corresponding section of official sitecore documentation: https://doc.sitecore.com/developers/90/sitecore-experience-platform/en/create-a-custom-facet.html

7
  • Hello, @x3mxray, I applied the same code as suggested by you but I am getting an error which I attached in the question in screenshot. Could you please help me. May 12, 2019 at 7:46
  • client.GetAsync<Sitecore.XConnect.Contact>, Contact from Sitecore.XConnect namespace, not from Sitecore.Analytics.Tracking
    – x3mxray
    May 12, 2019 at 12:03
  • Hello, @x3mxray, After updating your suggestion I am getting another error, attached in question. Please have a look. May 15, 2019 at 8:39
  • Hi @HimmatSinghDulawat, I added section with custom facet for xconnect in my answer.
    – x3mxray
    May 15, 2019 at 14:05
  • 1
    Hi @HimmatSinghDulawat, it looks like serialization error. It seems that you try to return Sitecore.XConnect.Facet in server response to browser. Facet is a complex model. Just create a simple c# class for view model only with fields that you need, populate them from facet and use it for server response..
    – x3mxray
    May 16, 2019 at 12:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.