Get interactions
Use .GetAsync<Interaction>()
/ .Get<Interaction>()
to retrieve an interaction or set of interactions. Interactions can be retrieved by:
-
A combination of interaction ID and contact ID
-
A combination of interaction ID and contact reference
The contact ID or contact reference is included to increase the efficiency of the query in a sharded environment. Collection data is sharded on contact ID, so including the contact ID in the query means that xConnect immediately knows which shard holds the interaction data.
In the following example, two interactions are retrieved.
using Sitecore.XConnect;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Collections.ObjectModel;
using Sitecore.XConnect.Operations;
using Sitecore.XConnect.Client;
namespace Documentation
{
public class GetInteractions
{
// Async interaction
public async void ExampleAsync()
{
using (Sitecore.XConnect.Client.XConnectClient client = Sitecore.XConnect.Client.Configuration.SitecoreXConnectClientConfiguration.GetClient())
{
try
{
var interactionIdOne = Guid.Parse("DA2DA5F0-4348-E611-82E7-34E6D7117DCB");
var interactionIdTwo = Guid.Parse("B9814105-1F45-E611-82E6-34E6D7117DCB");
// Contact reference from ID
var contactRef = new ContactReference(interactionIdOne);
Sitecore.XConnect.InteractionReference interactionRef = new Sitecore.XConnect.InteractionReference(contactRef, interactionIdTwo);
// Contact reference from identifier
var identifiedContactRef = new IdentifiedContactReference("twitter", "myrtlesitecore");
Sitecore.XConnect.InteractionReference secondInteractionRef = new Sitecore.XConnect.InteractionReference(identifiedContactRef, Guid.Parse("E6067926-1F45-E611-82E6-34E6D7117DCB"));
var references = new List<Sitecore.XConnect.InteractionReference>()
{
interactionRef, secondInteractionRef
};
Task<ReadOnlyCollection<IEntityLookupResult<Interaction>>> interactionTask = client.GetAsync<Sitecore.XConnect.Interaction>(references, new InteractionExecutionOptions(new InteractionExpandOptions() { }));
ReadOnlyCollection<IEntityLookupResult<Interaction>> interaction = await interactionTask;
}
catch (Exception ex)
{
// Manage exceptions
}
}
}
// Sync example
public async void ExampleSync()
{
using (Sitecore.XConnect.Client.XConnectClient client = Sitecore.XConnect.Client.Configuration.SitecoreXConnectClientConfiguration.GetClient())
{
try
{
var interactionIdOne = Guid.Parse("DA2DA5F0-4348-E611-82E7-34E6D7117DCB");
var interactionIdTwo = Guid.Parse({B9814105-1F45-E611-82E6-34E6D7117DCB});
// Contact reference from ID
var contactRef = new ContactReference(interactionIdOne);
Sitecore.XConnect.InteractionReference interactionRef = new Sitecore.XConnect.InteractionReference(contactRef, interactionIdTwo);
// Contact reference from identifier
var identifiedContactRef = new IdentifiedContactReference("twitter", "myrtlesitecore");
Sitecore.XConnect.InteractionReference secondInteractionRef = new Sitecore.XConnect.InteractionReference(identifiedContactRef, Guid.Parse("E6067926-1F45-E611-82E6-34E6D7117DCB"));
var references = new List<Sitecore.XConnect.InteractionReference>()
{
interactionRef, secondInteractionRef
};
IReadOnlyCollection<IEntityLookupResult<Interaction>> interactions = client.Get<Interaction>(references, new InteractionExecutionOptions(new InteractionExpandOptions() { }));
}
catch (Exception ex)
{
// Manage exceptions
}
}
}
}
}
When requesting a list of interactions, your return type will be a list of operation results, not a list of contacts. The return type is ReadOnlyCollection<IEntityLookupResult<Contact>>
. When looping through this collection, you can access the Interaction
object itself through the Entity
property on each EntityLookupResult<Interaction>
object. Recommended practices is to check if the Interaction
is null or check if the entity Exists
. The associated Exception
is also available for each operation result.
If you are getting a single contact, you can use the .GetContactAsync()
method. If you are getting more than one contact, use the .GetAsync<Contact>()
as it performs better.
Get interaction events
Interactions are always returend with all available events. Refer to Get interaction events for more information about working with the different types of events.
Get interaction facets and related contact
By default, interactions are returned without facets or the related contact. Use expand options to determine which facets and contact information should be returned: