Get operation results

Abstract

Examples showing how to see a list of all operations that were included in a batch and how to save a reference to a specific operation and inspect it after submitting a batch.

Inspect client.LastBatch after successfully submitting a batch to see a list of all operations that were included in that batch.

using Sitecore.XConnect;
using Sitecore.XConnect.Client;
using Sitecore.XConnect.Operations;
using System;

namespace Documentation
{
    public class SeeAllExecutedOperations
    {
        // Async Example
        public async void ExampleAsync()
        {
            using (Sitecore.XConnect.Client.XConnectClient client = Sitecore.XConnect.Client.Configuration.SitecoreXConnectClientConfiguration.GetClient())
            {
                try
                {
                    var newContact = new Sitecore.XConnect.Contact();
                    client.AddContact(newContact);

                    Guid channelId = Guid.NewGuid();
                    string userAgent = "Sample User Agent";

                    var interaction = new Sitecore.XConnect.Interaction(newContact, InteractionInitiator.Brand, channelId, userAgent);

                    var fakeItemID = Guid.NewGuid();
                    var fakeItemVersion = 3;

                    Sitecore.XConnect.Collection.Model.PageViewEvent pageView = new Sitecore.XConnect.Collection.Model.PageViewEvent(DateTime.UtcNow, fakeItemID, fakeItemVersion, "en")
                    {
                        Duration = new TimeSpan(0, 0, 30),
                    };

                    interaction.Events.Add(pageView);

                    client.AddInteraction(interaction);

                    await client.SubmitAsync();

                    var allOperations = client.LastBatch;

                    foreach (IXdbOperation op in allOperations)
                    {
                        // Output operation type
                        var operationType = op.OperationType;
                        // Output status
                        var status = op.Status;
                    }
                }
                catch (XdbExecutionException ex)
                {
                    // Handle exception
                }
            }
        }

        // Sync example
        public void ExampleSync()
        {
            using (Sitecore.XConnect.Client.XConnectClient client = Sitecore.XConnect.Client.Configuration.SitecoreXConnectClientConfiguration.GetClient())
            {
                try
                {
                    var newContact = new Sitecore.XConnect.Contact();
                    client.AddContact(newContact);

                    Guid channelId = Guid.NewGuid();
                    string userAgent = "Sample User Agent";

                    var interaction = new Sitecore.XConnect.Interaction(newContact, InteractionInitiator.Brand, channelId, userAgent);

                    var fakeItemID = Guid.NewGuid();
                    var fakeItemVersion = 3;

                    Sitecore.XConnect.Collection.Model.PageViewEvent pageView = new Sitecore.XConnect.Collection.Model.PageViewEvent(DateTime.UtcNow, fakeItemID, fakeItemVersion, "en")
                    {
                        Duration = new TimeSpan(0, 0, 30),
                    };

                    interaction.Events.Add(pageView);

                    client.AddInteraction(interaction);

                    client.Submit();

                    var allOperations = client.LastBatch;

                    foreach (IXdbOperation op in allOperations)
                    {
                        // Output operation type
                        var operationType = op.OperationType;
                        // Output status
                        var status = op.Status;
                    }
                }
                catch (XdbExecutionException ex)
                {
                    // Handle exception
                }
            }
        }
    }
}

Note

Refer to documentation on exception handling and retrying operations for information about how to handle failed operations.

You can also save a reference to a specific operation and inspect it after submitting a batch. For example:

using Sitecore.XConnect;
using Sitecore.XConnect.Client;
using Sitecore.XConnect.Operations;
using System;

namespace Documentation
{
    public class SeeSingleOperation
    {
        // Async Example
        public async void ExampleAsync()
        {
            using (Sitecore.XConnect.Client.XConnectClient client = Sitecore.XConnect.Client.Configuration.SitecoreXConnectClientConfiguration.GetClient())
            {
                try
                {
                    var newContact = new Sitecore.XConnect.Contact();
                    client.AddContact(newContact);

                    Guid channelId = Guid.NewGuid();
                    string userAgent = "Sample User Agent";

                    var interaction = new Sitecore.XConnect.Interaction(newContact, InteractionInitiator.Brand, channelId, userAgent);

                    var fakeItemID = Guid.NewGuid();
                    var fakeItemVersion = 3;

                    Sitecore.XConnect.Collection.Model.PageViewEvent pageView = new Sitecore.XConnect.Collection.Model.PageViewEvent(DateTime.UtcNow, fakeItemID, fakeItemVersion, "en")
                    {
                        Duration = new TimeSpan(0, 0, 30),
                    };

                    interaction.Events.Add(pageView);

                    // Save reference to operation
                    var addInteractionOperation = client.AddInteraction(interaction);

                    await client.SubmitAsync();

                    if (addInteractionOperation.Result.Status == SaveResultStatus.Conflict)
                    {
                        // Do something
                    }
                }
                catch (XdbExecutionException ex)
                {
                    // Handle exception
                }
            }
        }

        // Sync example
        public void ExampleSync()
        {
            using (Sitecore.XConnect.Client.XConnectClient client = Sitecore.XConnect.Client.Configuration.SitecoreXConnectClientConfiguration.GetClient())
            {
                try
                {
                    var newContact = new Sitecore.XConnect.Contact();
                    client.AddContact(newContact);

                    Guid channelId = Guid.NewGuid();
                    string userAgent = "Sample User Agent";

                    var interaction = new Sitecore.XConnect.Interaction(newContact, InteractionInitiator.Brand, channelId, userAgent);

                var fakeItemID = Guid.NewGuid();
                var fakeItemVersion = 3;

                Sitecore.XConnect.Collection.Model.PageViewEvent pageView = new Sitecore.XConnect.Collection.Model.PageViewEvent(DateTime.UtcNow, fakeItemID, fakeItemVersion, "en")
                {
                    Duration = new TimeSpan(0, 0, 30),
                };

                interaction.Events.Add(pageView);

                // Save reference to operation
                var addInteractionOperation = client.AddInteraction(interaction);

                client.Submit();

                if (addInteractionOperation.Result.Status == SaveResultStatus.Conflict)
                {
                    // Do something
                }
            }
            catch (XdbExecutionException ex)
            {
                // Handle exception
            }
        }
    }
}