Harnessing the Power of Sitecore xConnect

Harnessing the Power of Sitecore xConnect: An In-depth Exploration with Code Examples

  • 31/07/2023
  • Technical
  • Sitecore XP, XConnect, Architecture, Best Practices, Code Examples

The development world has greatly evolved, and a myriad of new technologies are transforming the way we manage and interact with data. Among these revolutionary tools, Sitecore xConnect stands out as an exceptional framework to facilitate data exchange between Sitecore XP and external systems or third-party applications.

In this blog post, we'll delve into the depths of Sitecore xConnect, understanding its capabilities, exploring its architecture, and demonstrating its usage with practical code examples.

Understanding Sitecore xConnect

Sitecore xConnect is a service layer that sits between the Sitecore Experience Platform (XP) and external systems, allowing developers to read, write, and search experience data collected by Sitecore XP. It is agnostic to the type of client you're using - whether it's a .NET application or even a Java-based service, you can interact with it.

Architecture of Sitecore xConnect

At the heart of Sitecore xConnect is the xDB, which contains all your collected experience data. This data is exposed via an OData-based API that lets you interact with your data via CRUD operations. The xConnect Collection model is used to define the type of data that can be read from or written to the xDB.

Getting Started

To interact with xConnect, you must first create a client. This client can be created by using a XConnectClientConfiguration object. Here's a simple example:


var collectionClient = new CollectionWebApiClient(new Uri("https://collection.yourdomain.com"));
var searchClient = new SearchWebApiClient(new Uri("https://search.yourdomain.com"));
var configurationClient = new ConfigurationWebApiClient(new Uri("https://configuration.yourdomain.com"));

var cfg = new XConnectClientConfiguration(
    new XdbRuntimeModel(CollectionModel.Model), collectionClient, searchClient, configurationClient);

try
{
    await cfg.InitializeAsync();
}
catch (XdbModelConflictException ce)
{
    Console.WriteLine("ERROR:" + ce.Message);
}

Once your client is created and initialized, you can begin to perform operations on your data.

Creating and Sending Data

For example, if you want to create a new contact and send it to the xDB, you could do this:


using (var client = new XConnectClient(cfg))
{
    var identifiers = new ContactIdentifier[]
    {
        new ContactIdentifier("twitter", "@sampleuser", ContactIdentifierType.Known)
    };
    
    var contact = new Contact(identifiers);
    client.AddContact(contact);

    await client.SubmitAsync();
}

Searching for Data

xConnect also allows for powerful search capabilities. For instance, here's how you could find a contact based on their twitter handle:


using (var client = new XConnectClient(cfg))
{
    var contactReference = new IdentifiedContactReference("twitter", "@sampleuser");
    var contact = await client.GetAsync(contactReference, new ContactExpandOptions());
    
    if(contact != null)
    {
        Console.WriteLine("Contact found!");
    }
}

Updating Data

You can also easily update existing data:


using (var client = new XConnectClient(cfg))
{
    var contactReference = new IdentifiedContactReference("twitter", "@sampleuser");
    var contact = await client.GetAsync(contactReference, new ContactExpandOptions());

    if(contact != null)
    {
        contact.Personal().FirstName = "John";
        contact.Personal().LastName = "Doe";
        
        client.SetFacet(contact, PersonalInformation.DefaultFacetKey, contact.Personal());

        await client.SubmitAsync();
    }
}

This brief introduction to Sitecore xConnect just scratches the surface of what's possible. You can interact with facets, events, outcomes, and much more. xConnect offers an extremely flexible and powerful way to interact with your xDB data, enabling you to leverage the power of Sitecore XP in all new ways.

Enjoy using XConnect 🙂

P.S.: For more informations about Sitecore and our Services, please visit www.cyber-solutions.at