Sitecore custom Contact Facet with WFFM
In Sitecore by Out of box, we have predefined Facets but most of them are useful for us, but still, due to some client requirement we may need to store different details about the customer to Mongo DB and will be aggregated to Reporting DB.
Facets have these following.
v Attributes – attributes are an element's subitems. For example, the Address element could contain attributes such as country, state, town or email address. Attributes can be strings, GUIDs, integers, or floating point numbers.
v Elements – an element is a data structure that contains one or more members or attributes, such as a facet.
v Dictionaries – A dictionary contains named elements. Each element in a dictionary is identified by a unique key
Let us start creating Custom Facet for the storing customer’s Email, Age, Designation, and Company but the rest like FirstName into the out of Sitecore Facet.
v In order to create a Facet, we need to first create an Element which holds all Customer properties like Email, Age, Designation, and company.
v Mark this class with [Serializable] Attribute, it is very important when we using shared session, else it will throw Tracker not initialized error
public interface IcustomerDetailsElement : IElement
{
string Email { get; set; }
string Company { get; set; }
string Designation { get; set; }
int Age { get; set; }
}
[Serializable]
public class CustomerDetailElement : Element, IcustomerDetailsElement
{
private const string EMAIL = "Email";
private const string COMPANY = "Company";
private const string DESIGNATION = "Designation";
private const string AGE = "Age";
public CustomerDetailElement()
{
this.EnsureAttribute<string>(COMPANY);
this.EnsureAttribute<string>(DESIGNATION);
this.EnsureAttribute<int>(AGE);
}
public string Email
{
get { return base.GetAttribute<string>(EMAIL); }
set { base.SetAttribute<string>(EMAIL, value); }
}
public int Age
{
get { return base.GetAttribute<int>(AGE); }
set { base.SetAttribute<int>(AGE, value); }
}
public string Company
{
get { return base.GetAttribute<string>(COMPANY); }
set { base.SetAttribute<string>(COMPANY, value); }
}
public string Designation
{
get { return base.GetAttribute<string>(DESIGNATION); }
set { base.SetAttribute<string>(DESIGNATION, value); }
}
}
· Once we created the element class with necessary fields that we need to store to Mongo DB, now let's create the Facet class.
IFacet implements IElement Class.
public interface IContactCommentsFacet : IFacet
{
string Comments { get; set; }
IElementCollection<IcustomerDetailsElement> CustomerDetails { get; }
}
[Serializable]
public class ContactCommentsFacet : Facet, IContactCommentsFacet
{
private const string CONTACT_COMMENT = "Comments";
private const string CUSTOMER_DETAILS = "CustomerDetails";
public ContactCommentsFacet()
{
base.EnsureAttribute<string>(CONTACT_COMMENT);
base.EnsureCollection<IcustomerDetailsElement>(CUSTOMER_DETAILS);
}
public string Comments
{
get { return base.GetAttribute<string>(CONTACT_COMMENT); }
set { base.SetAttribute<string>(CONTACT_COMMENT, value); }
}
public IElementCollection<IcustomerDetailsElement> CustomerDetails
{
get
{
return base.GetCollection<IcustomerDetailsElement>(CUSTOMER_DETAILS);
}
}
}
v Now our Elements and Facets created now its time to make Sitecore aware of this new Facet.
v Add the Element and Facet to the Config patch file
Elements
|
|
Interface
|
Implementation
|
ICustomerDetailsElement
|
CustomerDetailElement
|
IContactCommentsFacet
|
ContactCommentsFacet
|
Facet
|
|
Name
|
Interface
|
Contact Details
|
IContactCommentsFacet
|
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
<sitecore>
<model>
<elements>
<element interface="xxxxxxx.IcustomerDetailsElement, xxxxxxx" implementation="xxxxxxx.CustomerDetailElement, xxxxxxx" />
<element interface="xxxxxxx.IContactCommentsFacet, xxxxxxx" implementation="xxxxxxx.ContactCommentsFacet, xxxxxxx" />
</elements>
<entities>
<contact>
<facets>
<facet name="Contact Details" contract="xxxxxxx.IContactCommentsFacet, xxxxxxx" />
</facets>
</contact>
</entities>
</model>
</sitecore>
</configuration>
Once this step is done, if we go to WFFM form we can see this new Facet shows when updating the Fields.
Comments
Post a Comment