XCONNECT

One of the main changes from Sitecore 8.0 to 9.0 is, Sitecore changed xDB related API to the service based API. When you install Sitecore 9.0 or Sitecore 9 - Update 1, it creates two websites in your machine. One for your regular CMS and another is for hosting service, that can be used to perform xDB related activity.

http://www.tekkishare.com

Now, xDB can't be connect directly by any means. We must have to use xConnect service to play around xDB.

HOW TO STORE USER PROFILE USING XCONNECT

As you can see above, we have to create a instance of xConnect to save user profile information into xDBs.

  • XConnectClient client = SitecoreXConnectClientConfiguration.GetClient();

We can save user profile information as a contact, hence have to create an object of contact.

  • Sitecore.XConnect.Contact contact = new Sitecore.XConnect.Contact(new ContactIdentifier("Custom", user.Username , ContactIdentifierType.Known));
  • client.AddContact(contact);

xDB store information as a Facet. So, either we have to use existing (out of the box) facet or have to create custom facet. Creating a custom facet is not in scope of this article. So, we are using out of the box facet.

  • PersonalInformation personalInfoFacet = new PersonalInformation()
  • {
  • FirstName = user.FirstName ?? "Firstname",
  • LastName = user.LastName ?? "Lastname"
  • }
  • FacetReference reference = new FacetReference(contact, PersonalInformation.DefaultFacetKey);
  • client.SetFacet(reference, personalInfoFacet);
  • // Facet without a reference, using default key
  • EmailAddressList emails = new EmailAddressList(new EmailAddress(user.Username+"@gmail.com", true), "Home");
  • client.SetFacet(contact, emails);
  • // Facet without a reference, key is specified
  • AddressList addresses = new AddressList(new Address() { AddressLine1 = "Cool Street 12", City = "Sitecore City", PostalCode = "ABC 123" }, "Home");
  • client.SetFacet(contact, AddressList.DefaultFacetKey, addresses);
  • // Submit operations as batch
  • client.Submit();



HOW TO VERIFY PROFILE INFORMATION

You can see your saved profile information in the database SC91_Xdb.Collection.Shard0, table ContactFacets

Execute below query:

  • select * from [xdb_collection].ContactFacets order by LastModified desc


http://www.tekkishare.com

As you can see in the above screenshot, for each of the facet, Sitecore create a new row. You can identify by column FacetKey. You can get your profile data in the JSON formation against the column FacetData.


Thanks for your time. Happy learning & sharing.