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:
Update Code 2: After updating client.GetAsync, Contact from Sitecore.XConnect namespace, getting below error: