Instantiate client in a non-Sitecore context
Example of how to instantiate the xConnect Client API from a console application.
The xConnect Client API is portable and can be used in a non-Sitecore context. The following example demonstrates how to instantiate the client from a console application.
Only trusted clients with the correct client certificates can communicate with xConnect. If you want to write to xConnect from a mobile application, you must set up an intermediary server that collects data from the application and submits that data to xConnect at regular intervals. You cannot write directly to xConnect from a mobile device.
using Sitecore.XConnect;
using Sitecore.XConnect.Client;
using Sitecore.XConnect.Schema;
using System;
using System.Threading.Tasks;
using Sitecore.XConnect.Client.WebApi;
using Sitecore.Xdb.Common.Web;
using Sitecore.XConnect.Collection.Model;
using System.Collections.Generic;
namespace Symposium.xConnect.Course
{
public class Program
{
private static void Main(string[] args)
{
MainAsync(args).ConfigureAwait(false).GetAwaiter().GetResult();
}
private static async Task MainAsync(string[] args)
{
// Valid certificate thumbprints must be passed in
CertificateHttpClientHandlerModifierOptions options =
CertificateHttpClientHandlerModifierOptions.Parse("StoreName=My;StoreLocation=LocalMachine;FindType=FindByThumbprint;FindValue=15E6693B0AECB63DE57D991EC363CA462DC52432");
// Optional timeout modifier
var certificateModifier = new CertificateHttpClientHandlerModifier(options);
List<IHttpClientModifier> clientModifiers = new List<IHttpClientModifier>();
var timeoutClientModifier = new TimeoutHttpClientModifier(new TimeSpan(0, 0, 20));
clientModifiers.Add(timeoutClientModifier);
// This overload takes three client end points - collection, search, and configuration
var collectionClient = new CollectionWebApiClient(new Uri("https://sc900rev170622_xconnect/odata"), clientModifiers, new[] { certificateModifier });
var searchClient = new SearchWebApiClient(new Uri("https://sc900rev170622_xconnect/odata"), clientModifiers, new[] { certificateModifier });
var configurationClient = new ConfigurationWebApiClient(new Uri("https://sc900rev170622_xconnect/configuration"), clientModifiers, new[] { certificateModifier });
var cfg = new XConnectClientConfiguration(
new XdbRuntimeModel(CollectionModel.Model), collectionClient, searchClient, configurationClient);
try
{
await cfg.InitializeAsync();
}
catch (XdbModelConflictException ce)
{
Console.WriteLine("ERROR:" + ce.Message);
return;
}
using (var client = new XConnectClient(cfg))
{
}
}
}
}
Using a custom model
In a Sitecore context, the xConnect Client API uses a runtime model that is assembled from all the models available in configuration. In a non-Sitecore context, you must pass the model that you want to work with into XConnectClientConfiguration
:
var cfg = new XConnectClientConfiguration(
new XdbRuntimeModel(Custom.Namespace.Model), collectionClient, searchClient, configurationClient);
You can instantiate an XdbRuntimeModel
object with an array of models.