Skip to content

Instantly share code, notes, and snippets.

@ThomPuiman
Created December 9, 2020 07:20
Show Gist options
  • Select an option

  • Save ThomPuiman/260e71255a96ef82cc1674b84d3ac64a to your computer and use it in GitHub Desktop.

Select an option

Save ThomPuiman/260e71255a96ef82cc1674b84d3ac64a to your computer and use it in GitHub Desktop.
Sitecore 9/10 - Quickly identify contact and set facets
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="xConnect.Identify.aspx.cs" Inherits="HeadlessTraining.sitecore.admin.xConnect_Identify" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<div runat="server" ID="Notification"></div>
<div>
<span style="width: 150px;">First Name:</span>
<span><input ID="FirstName" type="text" runat="server" /></span>
</div>
<div>
<span style="width: 150px;">Last Name:</span>
<span><input ID="LastName" type="text" runat="server" /></span>
</div>
<div>
<span style="width: 150px;">Country Code:</span>
<span><input ID="CountryCode" type="text" runat="server" /></span>
</div>
<div>
<span style="width: 150px;">Phone:</span>
<span><input ID="Phone" type="text" runat="server" /></span>
</div>
<div>
<span style="width: 150px;">Email:</span>
<span><input ID="Email" type="text" runat="server" /></span>
</div>
<div>
<input ID="Submit" type="submit" value="submit" runat="server" />
</div>
<div>
<input ID="Abandon" value="Abandon" type="submit" runat="server" />
</div>
</div>
</form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.Extensions.DependencyInjection;
using Sitecore.Analytics.XConnect.Facets;
using Sitecore.XConnect;
using Sitecore.XConnect.Client;
using Sitecore.XConnect.Collection.Model;
namespace HeadlessTraining.sitecore.admin
{
public partial class xConnect_Identify : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
this.Submit.ServerClick += Submit_ServerClick;
this.Abandon.ServerClick += Abandon_ServerClick;
}
private void Abandon_ServerClick(object sender, EventArgs e)
{
Sitecore.Analytics.Tracker.Current.EndTracking();
Session.Abandon();
this.Notification.InnerText = "Session abandoned!";
}
private void Submit_ServerClick(object sender, EventArgs e)
{
if (Sitecore.Analytics.Tracker.Current == null)
{
Sitecore.Analytics.Tracker.StartTracking();
}
using (Sitecore.XConnect.Client.XConnectClient client = Sitecore.XConnect.Client.Configuration.SitecoreXConnectClientConfiguration.GetClient())
{
{
try
{
Contact contact = new Contact(new ContactIdentifier("test", this.Email.Value, ContactIdentifierType.Known));
client.AddContact(contact);
// Facet with a reference object, key is specified
PersonalInformation personalInfoFacet = new PersonalInformation()
{
FirstName = this.FirstName.Value,
LastName = this.LastName.Value
};
FacetReference reference = new FacetReference(contact, PersonalInformation.DefaultFacetKey);
client.SetFacet(reference, personalInfoFacet);
// Facet without a reference, using default key
EmailAddressList emails = new EmailAddressList(new EmailAddress(this.Email.Value, true), "Home");
client.SetFacet(contact, emails);
// Facet without a reference, key is specified
PhoneNumberList phone = new PhoneNumberList(new PhoneNumber(this.CountryCode.Value, this.Phone.Value), "Home");
client.SetFacet(contact, PhoneNumberList.DefaultFacetKey, phone);
// Submit operations as batch
client.Submit();
}
catch (XdbExecutionException ex)
{
}
}
}
this.Notification.InnerText = "Contact Identified!";
}
}
}
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
namespace HeadlessTraining.sitecore.admin
{
public partial class xConnect_Identify
{
/// <summary>
/// form1 control.
/// </summary>
/// <remarks>
/// Auto-generated field.
/// To modify move field declaration from designer file to code-behind file.
/// </remarks>
protected global::System.Web.UI.HtmlControls.HtmlForm form1;
/// <summary>
/// Notification control.
/// </summary>
/// <remarks>
/// Auto-generated field.
/// To modify move field declaration from designer file to code-behind file.
/// </remarks>
protected global::System.Web.UI.HtmlControls.HtmlGenericControl Notification;
/// <summary>
/// FirstName control.
/// </summary>
/// <remarks>
/// Auto-generated field.
/// To modify move field declaration from designer file to code-behind file.
/// </remarks>
protected global::System.Web.UI.HtmlControls.HtmlInputText FirstName;
/// <summary>
/// LastName control.
/// </summary>
/// <remarks>
/// Auto-generated field.
/// To modify move field declaration from designer file to code-behind file.
/// </remarks>
protected global::System.Web.UI.HtmlControls.HtmlInputText LastName;
/// <summary>
/// CountryCode control.
/// </summary>
/// <remarks>
/// Auto-generated field.
/// To modify move field declaration from designer file to code-behind file.
/// </remarks>
protected global::System.Web.UI.HtmlControls.HtmlInputText CountryCode;
/// <summary>
/// Phone control.
/// </summary>
/// <remarks>
/// Auto-generated field.
/// To modify move field declaration from designer file to code-behind file.
/// </remarks>
protected global::System.Web.UI.HtmlControls.HtmlInputText Phone;
/// <summary>
/// Email control.
/// </summary>
/// <remarks>
/// Auto-generated field.
/// To modify move field declaration from designer file to code-behind file.
/// </remarks>
protected global::System.Web.UI.HtmlControls.HtmlInputText Email;
/// <summary>
/// Submit control.
/// </summary>
/// <remarks>
/// Auto-generated field.
/// To modify move field declaration from designer file to code-behind file.
/// </remarks>
protected global::System.Web.UI.HtmlControls.HtmlInputSubmit Submit;
/// <summary>
/// Abandon control.
/// </summary>
/// <remarks>
/// Auto-generated field.
/// To modify move field declaration from designer file to code-behind file.
/// </remarks>
protected global::System.Web.UI.HtmlControls.HtmlInputSubmit Abandon;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment