Sitecore Experience Platform

Using the reporting foundation

Abstract

Describes how to use the reporting client

After following the installation in the previous step you’re ready to consume the reporting client.

Reporting Client is registered in Sitecore IoC Container you can add IReportingClient as a dependency to your service if you’re using Sitecore DI.

Or resolve it via ServiceLocator Sitecore.DependencyInjection:


Sitecore.DependencyInjection.ServiceLocator.ServiceProvider.GetService<IReportingClient>();

To query data you can use the method GetFactAsync<TFact>(Action<IFactQueryOptionsBuilder>TFact>> builder) this method accepts a delegate in which it builds the query. Find the example below:

await _reportingClient.GetFactAsync<ProductSales>(cfg =>
            {
                cfg.UseConnectionStringName("MyConnectionStringName")
                    .QueryFrom(DateTime.Now.AddYears(-1))
                    .To(DateTime.Now)
                    .ExpandDimension(o => o.Country)
                    .FilterWithKey("MY")
                    .ExpandDimension(o => o.Product);
            });

Notes:

  • Reporting Client methods are asynchronous

  • The query builder accepts a parameter which is a delegate that builds the query

  • You’ve to pass the connection string name (e.g., reporting) in which you want to query data from (i.e., the database having your SQL Objects)

  • QueryFrom…To will filter data based on the DateTime field in the fact object.

  • ExpandDimension will include the dimension in the result

  • FilterWithKey will filter the result based on the DimensionKey of the dimension

Note

The query must include at least one filter (either dimension or date time) and the connection string name otherwise a ValidationException will be thrown.

Similar to Querying facts, Delete Facts also have the same filters. Find the below examples:

await _reportingClient.DeleteFactsAsync<ProductSales>(cfg =>
            {
                cfg.UseConnectionStringName("MyConnectionStringName")
                    .DeleteFrom(DateTime.Now.AddYears(-1))
                    .To(DateTime.Now)
                    .DeleteThatHasDimension(o => o.Country)
                    .WithKey("MY");
            });

Notes

  • Same filters are used (with different names) in the builder.

  • The delete query builder must include at least one filter too and the connection string

To add or update data you use the method AddOrUpdate async and pass connection string name with a list of the data. Check the example below.


await _reportingClient.AddOrUpdateAsync(_connectionStringName, data)