0

Is there a way to retrieve data from table xdb_collection.Interactions in xdb_collection.shard0 datasource in my code?

I already tried to use the method in the following link Sorting Pages by most views but it only works with the reporting datasource.

Edit: When trying the extraction approach i get the below error and the code exists. extraction approach error

1

1 Answer 1

3

You should not query xdb_collection database. XConnect API is meant to be used for this type of scenarios. You have 2 ways to retrieve interactions from XDB database:

  1. Interaction extraction to get all interactions only limited by date.
  2. Search interactions if you need to have more complex search criteria.

Code samples taken from documentation for reference

Extraction approach

using Sitecore.XConnect;
using Sitecore.XConnect.Collection.Model;
using System;
using System.Collections.Generic;

namespace Documentation
{
    public class DataExtraction
    {
        // Async interaction
        public async void ExampleAsync()
        {
            using (Sitecore.XConnect.Client.XConnectClient client = Sitecore.XConnect.Client.Configuration.SitecoreXConnectClientConfiguration.GetClient())
            {
                try
                {
                    // Extract interactions
                    var allInteractions = new List<Interaction>();
                    var interactionEnumerator = await client.CreateInteractionEnumerator(DateTime.UtcNow, new InteractionExpandOptions(), 200);
                    while (await interactionEnumerator.MoveNext())
                    {
                        foreach (var interaction in interactionEnumerator.Current)
                        {
                            allInteractions.Add(interaction);
                        }
                    }

                }
                catch (XdbExecutionException ex)
                {
                    // Manage exceptions
                }
            }
        }
    }
}

Search based approach

using Sitecore.XConnect.Collection.Model;
using Sitecore.XConnect.Operations;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Linq;
using Sitecore.XConnect.Search;
using Sitecore.XConnect;
using Sitecore.XConnect.Client;

namespace Documentation
{
    public class SearchInteractionProperty
    {
        // Async example
        public async void ExampleAsync()
        {
            using (Sitecore.XConnect.Client.XConnectClient client = Sitecore.XConnect.Client.Configuration.SitecoreXConnectClientConfiguration.GetClient())
            {
                try
                {
                    var someChannelId = Guid.Parse("0565e9b0-936c-4594-8cc3-5fcace3918ed");

                    IAsyncQueryable<Sitecore.XConnect.Interaction> queryable = client.Interactions
                        .Where(x => x.ChannelId == someChannelId && x.StartDateTime > DateTime.UtcNow.AddDays(-10));

                    var enumerable = await queryable.GetBatchEnumerator(10);

                    while (await enumerable.MoveNext())
                    {
                        IReadOnlyCollection<Interaction> currentBatch = enumerable.Current;
                    }
                }
                catch (XdbExecutionException ex)
                {
                    // Handle exception
                }
            }
        }    
    }
}
4
  • i tried this method but i get the following error: '((Sitecore.XConnect.AsyncEntity Batch Enumerator<Sitecore.XConnect.Interaction>)interaction Cursor).Current' threw an exception of type 'System.InvalidOperationException' , any idea what i'm missing?
    – AME
    Commented Oct 3, 2018 at 15:09
  • @AmiraElEwady, could you please update the question with your code sample that's failing?
    – grg
    Commented Oct 3, 2018 at 19:13
  • @AmiraElEwady, I update the "Extraction approach" sample. You can give another try.
    – grg
    Commented Oct 5, 2018 at 8:58
  • @grg I am trying the same code but var interactionEnumerator = await client.CreateInteractionEnumerator(DateTime.UtcNow, new InteractionExpandOptions(), 200); this line sometime kills the process... means whenever I am debugging it it is killing the debugger on same like Commented Nov 21, 2018 at 10:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.