I'm trying to write a .aspx script to update an existing contact in the experience profile, the basic logic is:
- get the contact by e-mail
- get a custom facet from the contact
- if the facet is null create a new one and fill some properties with random values
- else update the properties with some value
The weird thing is the facet is coming null, so I'm creating a new one, but when I call the submit I'm getting the following error:
Exception: Sitecore.XConnect.Operations.FacetOperationException Message: Operation #0, AlreadyExists, Contact {75e750cb-dc05-0000-0000-065e2a461d1a}, CustomExmFacets Source: Sitecore.Xdb.Common.Web
Has someone faced this before?
Here is the whole script
<%@ Page Language="C#" Debug="true"%>
<%@ Import Namespace="Microsoft.Extensions.DependencyInjection" %>
<%@ Import Namespace="Sitecore.XConnect.Collection.Model" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="Sitecore.XConnect" %>
<%@ Import Namespace="Sitecore.XConnect.Client" %>
<%@ Import Namespace="Sitecore.XConnect.Collection.Model" %>
<%@ Import Namespace="Sitecore.XConnect.Client.Configuration" %>
<%
var contactListId = new System.Guid("{70D937C2-D809-4CCE-B5B2-07C68DCAD230}");
int batchSize = 200; // Size of the batch
string[] facets =
{
CollectionModel.FacetKeys.PersonalInformation,
CollectionModel.FacetKeys.ListSubscriptions,
"CustomExmFacet"
};
List<string> updatedContacts = new List<string>();
using (XConnectClient client = SitecoreXConnectClientConfiguration.GetClient())
{
string email = "[email protected]";
Sitecore.XConnect.Contact existingContact
= client.Get<Contact>(new IdentifiedContactReference("ListManager", email)
, new ContactExpandOptions(PersonalInformation.DefaultFacetKey));
if (existingContact == null) {
//Update CustomExmFacets type for some existing custom facet in your instance or use a Sitecore default one
var facet = existingContact.GetFacet<CustomExmFacets>("CustomExmFacet");
if (facet == null)
{
// Facet is new
var newFacet = new CustomExmFacets
{
VideoUrl = videoUrl.Trim(),
ThumbnailUrl = thumbnailUrl.Trim()
};
client.SetFacet<CustomExmFacets>(existingContact, "CustomExmFacet", newFacet);
}
else
{
facet.ThumbnailUrl = "https://random.thumbnail.url.com";
facet.VideoUrl = "https://random.video.url.com";
client.SetFacet<CustomExmFacets>(existingContact, "CustomExmFacet", facet);
updatedContacts.Add(email);
}
client.Submit();
}
}
%>
<html>
<head>
<title>Updated Contact</title>
</head>
<body>
<h1>Updated Contact</h1>
<%
foreach (string email in updatedContacts)
{
if (!string.IsNullOrEmpty(email))
{ %>
<p><% Response.Write(email); %></p>
<%
}
}%>
</body>
</html>