Created
June 27, 2018 16:27
-
-
Save JRondeau16/2e53a5a354bda2a971f53146cba59d67 to your computer and use it in GitHub Desktop.
Populate Sitecore Contact Data from Salesforce Marketing Cloud
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Linq; | |
using Sitecore.Analytics; | |
using Sitecore.Analytics.Tracking; | |
using Sitecore.Diagnostics; | |
using Sitecore.Mvc.Pipelines.Request.RequestBegin; | |
using Sitecore.Analytics.Model.Entities; | |
using Sitecore.Analytics.Model.Framework; | |
namespace YourNamespace.Pipelines.MvcRequestBegin | |
{ | |
public class IdentifyContact | |
{ | |
public void Process(RequestBeginArgs args) | |
{ | |
if ((Tracker.Current != null) && (Tracker.Current.Contact != null)) | |
{ | |
var contact = Tracker.Current.Contact; | |
if (!string.IsNullOrWhiteSpace(contact.Identifiers.Identifier)) | |
return; | |
var email = args.RequestContext.HttpContext.Request.QueryString["sfmcemail"]; | |
if (string.IsNullOrWhiteSpace(email)) | |
return; | |
var response = GetUserByEmail(email); | |
if (response.Length != 1) | |
return; | |
try | |
{ | |
Tracker.Current.Session.Identify(email); | |
var objectExtension = (Salesforce.SFMCS7.ObjectExtension)response.First(); | |
var personalFacet = contact.GetFacet<IContactPersonalInfo>("Personal"); | |
personalFacet.FirstName = objectExtension.Properties.FirstOrDefault(x => x.Name == "FirstName")?.Value; | |
personalFacet.Surname = objectExtension.Properties.FirstOrDefault(x => x.Name == "LastName")?.Value; | |
var phoneFacet = contact.GetFacet<IContactPhoneNumbers>("Phone Numbers"); | |
phoneFacet.Entries.Create("HomePhone"); | |
phoneFacet.Entries["HomePhone"].Number = objectExtension.Properties.FirstOrDefault(x => x.Name == "HomePhone")?.Value; | |
var emailFacet = contact.GetFacet<IContactEmailAddresses>("Emails"); | |
emailFacet.Entries.Create("SFMCEmail"); | |
emailFacet.Entries["SFMCEmail"].SmtpAddress = email; | |
var communicationFacet = contact.GetFacet<IContactCommunicationProfile>("Communication Profile"); | |
communicationFacet.CommunicationRevoked = bool.Parse(objectExtension.Properties.FirstOrDefault(x => x.Name == "DoNotContact")?.Value ?? "false"); | |
var addressFacet = contact.GetFacet<IContactAddresses>("Addresses"); | |
addressFacet.Entries.Create("HomeAddress"); | |
addressFacet.Entries["HomeAddress"].StreetLine1 = objectExtension.Properties.FirstOrDefault(x => x.Name == "Address")?.Value; | |
addressFacet.Entries["HomeAddress"].City = objectExtension.Properties.FirstOrDefault(x => x.Name == "City")?.Value; | |
addressFacet.Entries["HomeAddress"].StateProvince = objectExtension.Properties.FirstOrDefault(x => x.Name == "State")?.Value; | |
addressFacet.Entries["HomeAddress"].PostalCode = objectExtension.Properties.FirstOrDefault(x => x.Name == "Zip")?.Value; | |
} | |
catch (FacetNotAvailableException ex) | |
{ | |
Log.Warn("The 'Personal' facet is not available.", ex); | |
} | |
} | |
} | |
public Salesforce.SFMCS7.APIObject[] GetUserByEmail(string email) | |
{ | |
try | |
{ | |
var client = new Salesforce.SFMCS7.SoapClient | |
{ | |
ClientCredentials = | |
{ | |
UserName = | |
{ | |
UserName = "XXXX", | |
Password = "XXXX" | |
} | |
} | |
}; | |
//Retrieve Subscriber | |
Salesforce.SFMCS7.APIObject[] Results; | |
string requestID; | |
string status; | |
// Setting up a simple filter | |
var filter = new Salesforce.SFMCS7.SimpleFilterPart | |
{ | |
SimpleOperator = Salesforce.SFMCS7.SimpleOperators.@equals, | |
Property = "Email", | |
Value = new[] { email } | |
}; | |
// Instantiate the retrieve request | |
Salesforce.SFMCS7.RetrieveRequest request = new Salesforce.SFMCS7.RetrieveRequest | |
{ | |
ObjectType = "DataExtensionObject[Master]", //data extenion name I am querying | |
Filter = filter, | |
Properties = new[] { "FirstName", "LastName", "Address", "City", "State", "Zip", "HomePhone", "DoNotContact" } | |
}; | |
status = client.Retrieve(request, out requestID, out Results); | |
return Results; | |
} | |
catch (Exception ex) | |
{ | |
Sitecore.Diagnostics.Log.Error($"{this}: Could not retrieve subscriber with email {email}", ex, this); | |
return new Salesforce.SFMCS7.APIObject[0]; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment