In Sitecore 8.2 we have a Reset()
function on all facets. This allows us to reset the facet, clearing all values. On xConnect we do not have that functionality.
The drawback for xConnect is that you are not allow to just new a object and stick it in. If the facet exists, you have to reuse it. That is what I believe.
The Facet
[FacetKey(DefaultFacetKey)]
public class MemberDataFacet : Facet
{
public const string DefaultFacetKey = "MemberData";
public MemberDataFacet()
{
}
public Location Location { get; set; } = new Location();
public Membership Membership { get; set; } = new Membership();
}
The xConnect call
using (var client = this.contactIdentificationRepository.CreateContext())
{
var contact = client.Get(contactReference, new ContactExpandOptions(MemberDataFacet.DefaultFacetKey));
if (contact == null)
{
throw new ArgumentNullException(nameof(contact));
}
var membershipFacet = contact.GetFacet<MemberDataFacet>(MemberDataFacet.DefaultFacetKey) ?? new MemberDataFacet();
ResetMemberData(membershipFacet);
membershipFacet.Location.City = "Sarasota";
client.SetFacet(contact, MemberDataFacet.DefaultFacetKey, membershipFacet);
client.Submit();
contactFacetsProvider.UpdateTracker();
}
The reset function
public void ResetMemberData(MemberDataFacet memberfacet)
{
memberfacet.Location = new Location();
memberfacet.Membership = new Membership();
}
This seems to be a crappy way of doing it. Especially if your facet gets big with a lot of properties that are not in a subclass. Like I said, using membershipFacet = new MemberDataFacet()
if the facet is already on the contact.
Anyone know of a way to allow me to reset the facet in the reset() in xDB?
Possible
I believe this is how it should be done. But it errors
Message: This context does not support this operation. Source: Sitecore.Xdb.Common.Web at Sitecore.Xdb.Common.Web.Synchronous.SynchronousExtensions.SuspendContextLock[TResult](Func
1 taskFactory) at Sitecore.XConnect.Client.XConnectSynchronousExtensions.SuspendContextLock(Func
1 taskFactory)
private void ResetFacet()
{
var contactReference = this.contactIdentificationRepository.GetContactReference();
using (var client = this.contactIdentificationRepository.CreateContext())
{
var contact = client.Get(contactReference, new ContactExpandOptions(MemberDataFacet.DefaultFacetKey));
if (contact == null) return;
client.ClearFacet(new FacetReference(contact, MemberDataFacet.DefaultFacetKey), new MemberDataFacet());
client.Submit();
contactFacetsProvider.UpdateTracker();
}
}