Export all contact data

Version: 10.2

The right to data portability gives individuals the right to obtain and reuse their personal data for either their own use or to be used for a different service. Use expand options to retrieve all available data for a single contact, including facets and interactions.

Important

Exported JSON will include PII (Personally Identifiable Information) if you choose to extract facets marked [PIISensitive].

The following example demonstrates how to export a contact’s data to JSON, including facets and interactions. The example uses the client.Model.Facets property to build a list of contact and interaction facets to return. This property returns facet keys that are available in the current model. In a Sitecore context, models are loaded from a configuration file. When instantiating the xConnect Client API in a non-Sitecore context, you can pass an array of models into the XConnectClientConfiguration object.

RequestResponse
using Newtonsoft.Json;
using Sitecore.XConnect;
using Sitecore.XConnect.Client;
using Sitecore.XConnect.Client.Serialization;
using System;
using System.Linq;


namespace Documentation.Examples
{
    class GetAllData
    {
        public async void AsyncExample()
        {
            try
            {
                using (Sitecore.XConnect.Client.XConnectClient client = Sitecore.XConnect.Client.Configuration.SitecoreXConnectClientConfiguration.GetClient())
                {
                    try
                    {
                        var contactReference = new IdentifiedContactReference("twitter", "myrtlemcsitecore");

                        // Get all available contact facets in current model
                        var contactFacets = client.Model.Facets.Where(c => c.Target == EntityType.Contact).Select(x => x.Name);

                        // Get all available interaction facets in current model
                        var interactionFacets = client.Model.Facets.Where(c => c.Target == EntityType.Interaction).Select(x => x.Name);

                        var contact = await client.GetAsync<Contact>(contactReference, new ContactExecutionOptions(new ContactExpandOptions(contactFacets.ToArray())
                        {
                            Interactions = new RelatedInteractionsExpandOptions(interactionFacets.ToArray())
                            {
                                // Get all interactions
                                EndDateTime = DateTime.MaxValue,
                                StartDateTime = DateTime.MinValue
                            }
                        }));


                        // Serialize response
                        // Note special XdbJsonContractResolver - mandatory for serializing xConnect entities
                        var serializerSettings = new JsonSerializerSettings
                        {
                            ContractResolver = new XdbJsonContractResolver(client.Model,
                                serializeFacets: true,
                                serializeContactInteractions: true),
                            Formatting = Formatting.Indented,
                            DateTimeZoneHandling = DateTimeZoneHandling.Utc,
                            DefaultValueHandling = DefaultValueHandling.Ignore
                        };

                        string allData = JsonConvert.SerializeObject(contact, serializerSettings);
                        string name = "ContactData.json";

                        System.IO.File.WriteAllText(name, allData);

                    }
                    catch (XdbExecutionException ex)
                    {
                        Console.WriteLine("An error occurred");
                    }
                }

            }
            catch (XdbExecutionException ex)
            {
                // Handle exception
            }
        }

        public void SyncExample()
        {
            try
            {
                using (Sitecore.XConnect.Client.XConnectClient client = Sitecore.XConnect.Client.Configuration.SitecoreXConnectClientConfiguration.GetClient())
                {
                    try
                    {
                        var contactReference = new IdentifiedContactReference("twitter", "myrtlemcsitecore");

                        // Get all available contact facets in current model
                        var contactFacets = client.Model.Facets.Where(c => c.Target == EntityType.Contact).Select(x => x.Name);

                        // Get all available interaction facets in current model
                        var interactionFacets = client.Model.Facets.Where(c => c.Target == EntityType.Interaction).Select(x => x.Name);

                        var contact = client.Get<Contact>(contactReference, new ContactExecutionOptions(new ContactExpandOptions(contactFacets.ToArray())
                        {
                            Interactions = new RelatedInteractionsExpandOptions(interactionFacets.ToArray())
                            {
                                // Get all interactions
                                EndDateTime = DateTime.MaxValue,
                                StartDateTime = DateTime.MinValue
                            }
                        }));


                        // Serialize response
                        // Note special XdbJsonContractResolver - mandatory for serializing xConnect entities
                        var serializerSettings = new JsonSerializerSettings
                        {
                            ContractResolver = new XdbJsonContractResolver(client.Model,
                                serializeFacets: true,
                                serializeContactInteractions: true),
                            Formatting = Formatting.Indented,
                            DateTimeZoneHandling = DateTimeZoneHandling.Utc,
                            DefaultValueHandling = DefaultValueHandling.Ignore
                        };

                        string allData = JsonConvert.SerializeObject(contact, serializerSettings);
                        string name = "ContactData.json";

                        System.IO.File.WriteAllText(name, allData);

                    }
                    catch (XdbExecutionException ex)
                    {
                        Console.WriteLine("An error occurred");
                    }
                }

            }
            catch (XdbExecutionException ex)
            {
                // Handle exception
            }
        }
    }
}

Do you have some feedback for us?

If you have suggestions for improving this article,