Last active
August 26, 2016 13:52
-
-
Save mhwelander/4d0876ecec17e2486f3bdf088c66b99a to your computer and use it in GitHub Desktop.
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
[HttpPost] | |
public ActionResult ImportContacts() | |
{ | |
// For the purposes of this example, the dictionary key represents the contact identifier | |
// and the value represents their first name | |
var contacts = new Dictionary<string, string>(); | |
contacts.Add("martina_welander", "Martina"); | |
contacts.Add("bob_mcbob", "Bob"); | |
ContactRepository contactRepository = Factory.CreateObject("tracking/contactRepository", true) as ContactRepository; | |
ContactManager contactManager = Factory.CreateObject("tracking/contactManager", true) as ContactManager; | |
Assert.IsNotNull(contactRepository, "contactRepository"); | |
Assert.IsNotNull(contactManager, "contactManager"); | |
foreach (var contactDataToImport in contacts) | |
{ | |
// This attempts to lock the contact to this particular job for a five seconds | |
LeaseOwner leaseOwner = new LeaseOwner("CustomImportJob-" + Guid.NewGuid().ToString(), LeaseOwnerType.OutOfRequestWorker); | |
TimeSpan leaseDuration = TimeSpan.FromSeconds(5.0); | |
var result = contactRepository.TryLoadContact(contactDataToImport.Key, leaseOwner, leaseDuration); | |
Contact contact = null; | |
switch (result.Status) | |
{ | |
// If the contact is already locked, you need to update the contact in the context of a session | |
// on the cluster where they currently have an active session | |
// get the name of the cluster that they are locked to - note that in a production | |
// environment, cluster names are URIs (e.g. cluster1.domain.com) | |
case LockAttemptStatus.AlreadyLocked: | |
if (result.LockedBy.Type == LeaseOwnerType.WebCluster) | |
{ | |
var clusterName = result.LockedBy.Identifier; | |
using (WebClient client = new WebClient()) | |
{ | |
string url = "http://" + clusterName + "/sitecore/Contacts.ashx?cid=" + contactDataToImport.Key + "&firstname=" + contactDataToImport.Value; | |
bool success = bool.Parse(client.DownloadString(url)); | |
} | |
break; | |
} | |
throw new NotSupportedException(); | |
case LockAttemptStatus.Success: | |
contact = result.Object; | |
break; | |
case LockAttemptStatus.NotFound: | |
// Contact does not exist - create new contact | |
contact = contactRepository.CreateContact(Guid.NewGuid()); | |
contact.Identifiers.Identifier = contactDataToImport.Key; | |
contact.Identifiers.IdentificationLevel = ContactIdentificationLevel.Known; | |
// You should always set ContactSaveMode to AlwaysSave; NeverSave is reserved for robots | |
contact.ContactSaveMode = ContactSaveMode.AlwaysSave; | |
break; | |
} | |
if (contact != null) | |
{ | |
var personalFacet = contact.GetFacet<IContactPersonalInfo>("Personal"); | |
personalFacet.FirstName = contactDataToImport.Value; | |
contactRepository.SaveContact(contact, new ContactSaveOptions(true, leaseOwner, null)); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment