Extend the .NET SDK

When developing solutions with Sitefinity Insight, you can inherit and extend any of the .NET SDK clients. To create new clients, use the following abstract classes:

  • CrudClientBase<T>
  • CrudAndGetAllClientBase <TAll, T, TId>
  • DatacenterBoundClientBase
  • ClientBase

This article demonstrates how to create a client from scratch. The client gets all contacts in Sitefinity Insight and can also get a single contact and their interactions, reported to Sitefinity Insight.

NOTE: In this article, all visitors - both anonymous visitors or contacts who provided their email address, are referred as contacts.

You will need the following classes to create the sample:

  • Sitefinity Insight contact:
    using System.Collections.Generic;
    namespace Sitefinity.DEC.Snippets
    {
    public class DecContact
    {
    public int ContactId { get; set; }
    public string ContactIdentifier { get; set; }
    public Dictionary<string, string> Properties { get; set; }
    }
    }
    view raw DecContact.cs hosted with ❤ by GitHub

  • Sitefinity Insight contact interaction:
    using System;
    namespace Sitefinity.DEC.Snippets
    {
    public class DecContactInteraction
    {
    public string Id { get; set; }
    public string InteractionPredicate { get; set; }
    public string InteractionObject { get; set; }
    public DateTime InteractionOn { get; set; }
    }
    }

Implement the client

To implement the client, you inherit from the DatacenterBoundClientBase abstract class. You use the methods and constructors of the class to make requests to the Sitefinity Insight API Server. The following code sample demonstrates how to implement the client:

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading.Tasks;
using System.Web;
using Telerik.DigitalExperienceCloud.Client;
namespace Sitefinity.DEC.Snippets
{
public class DecContactsClient : DatacenterBoundClientBase
{
private const string contactsEndpoint = "analytics/v1/contacts";
public DecContactsClient(IAccessToken accessToken, string datacenterKey) : base(accessToken, datacenterKey) { }
public DecContactsClient(IAccessToken accessToken, string serverAddress, string datacenterKey) : base(accessToken, serverAddress, datacenterKey) { }
private HeaderOptions HeaderOptions
{
get
{
return new HeaderOptions() { DatacenterKey = base.DatacenterKey };
}
}
public Task<IEnumerable<DecContact>> GetAll(LoadOptions loadOptions)
{
return base.GetAsync(contactsEndpoint, null, this.HeaderOptions, loadOptions).ContinueWith(endpointResponse =>
JsonProvider.GetObject<CollectionResponse<DecContact>>(endpointResponse.Result).Items);
}
public Task<DecContact> GetById(int contactId)
{
string contactByIdPath = string.Format(CultureInfo.InvariantCulture, "{0}/{1}", contactsEndpoint, contactId.ToString(CultureInfo.InvariantCulture));
return base.GetAsync(contactByIdPath, null, this.HeaderOptions)
.ContinueWith(endpointResponse => JsonProvider.GetObject<DecContact>(endpointResponse.Result));
}
public Task<CollectionResponse<DecContactInteraction>> ContactInteractions(int contactId, LoadOptions loadOptions)
{
string contactsInteractionsPath = string.Format(CultureInfo.InvariantCulture, "{0}/{1}/interactions", contactsEndpoint, contactId);
return base.GetAsync(contactsInteractionsPath, null, this.HeaderOptions, loadOptions)
.ContinueWith(endpointResponse => JsonProvider.GetObject<CollectionResponse<DecContactInteraction>>(endpointResponse.Result));
}
}
}

The following sample demonstrates how to get twenty contacts from Sitefinity Insight and then get the details and twenty interactions of the first contact:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Telerik.DigitalExperienceCloud.Client;
namespace Sitefinity.DEC.Snippets
{
public class GetContactDetailsInteractions
{
public static void GetContactsDetailsAndInteractions()
{
AccessToken accessToken = new AccessToken("{Access Key Token}");
string dataCenterKey = "c27aeeb7-d222-d848-d8bf-9792485dafa9";
LoadOptions loadOptions = new LoadOptions()
{
Take = 20
};
DecContactsClient decContactsClient = new DecContactsClient(accessToken, dataCenterKey);
DecContact oneDecContact = decContactsClient.GetAll(loadOptions).Result.ToArray().First();
Console.WriteLine("Contact reported to DEC has the following properties:");
foreach (var contactProperty in oneDecContact.Properties)
{
if (!String.IsNullOrEmpty(contactProperty.Value))
{
Console.WriteLine("{0}: {1}", contactProperty.Key, contactProperty.Value);
}
}
DecContactInteraction[] contactFirstTwentyInteractions = decContactsClient.ContactInteractions(oneDecContact.ContactId, loadOptions).Result.Items.ToArray();
// Clients reported to DEC are considered known contacts as soon as they have an Email reported as metadata
Console.WriteLine("Contact with Id {0} and Email {1} has performed the following interactions:", oneDecContact.ContactId, oneDecContact.Properties["Email"]);
foreach (var contactInteraction in contactFirstTwentyInteractions)
{
Console.WriteLine("{0} {1} on {2}", contactInteraction.InteractionPredicate, contactInteraction.InteractionObject, contactInteraction.InteractionOn);
}
}
}
}

Abstract classes

The following table lists the abstract classes that you can use to extend the .NET SDK, together with their constructors, properties, and methods.

ClientBase

An abstract base class, containing logic for making PUT, POST, DELETE, and GET requests to the Sitefinity Insight API Server.

Constructors

  • protected ClientBase()
  • protected ClientBase(IAccessToken accessToken)
  • protected ClientBase(IAccessToken accessToken, string serverAddress)

Properties

  • public int? Timeout

DataCenterBoundClientBase

This abstract class inherits from ClientBase class and provides a basis infrastructure to simplify the implementation of new clients for data center bound operations. You use this class when you want to call a specific API endpoint that requires the data center key as part of the request header. 
To create advanced abstraction over CRUD (Create, Read, Update, and Modify) operations, or to automatically set restrictions for entities, you use different classes, such as CrudAndGetAllClientBase, described below.

Constructors

  • protected DatacenterBoundClientBase(IAccessToken accessToken, string datacenterKey)
  • protected DatacenterBoundClientBase(string datacenterKey)
  • protected DatacenterBoundClientBase(string serverAddress, string datacenterKey)

Properties

public string DatacenterKey { get; private set; }

CrudAndGetAllClientBase <TAll, T, TId>

An abstract class containing methods for creating, retrieving, updating, and deleting all artifacts. Inherits from DataCenterBoundClientBase.

Constructors

  • protected CrudAndGetAllClientBase(IAccessToken token, string datacenterKey)
  • protected CrudAndGetAllClientBase(IAccessToken token, string serverAddress, string datacenterKey)

Properties

protected abstract string EntityUrl { get; }

Methods

  • public Task<CollectionResponse<TAll>> GetAll(LoadOptions loadOptions)
  • public Task<T> Create(T entity)
  • public Task<T> GetById(TId id)
  • public Task<T> Update(T entity)
  • public Task Delete(TId id)

Was this article helpful?

Next article

.NET SDK data types