public override void Execute(ID formId, AdaptedResultList adaptedFields, ActionCallContext actionCallContext = null, params object[] data) { _log = Sitecore.Diagnostics.LoggerFactory.GetLogger("LogFileAppender"); var name = adaptedFields.GetEntryByName("Full Name"); var contactno = adaptedFields.GetEntryByName("Contact Number"); var email = adaptedFields.GetEntryByName("Email Address"); var Age = adaptedFields.GetEntryByName("Age"); var Company = adaptedFields.GetEntryByName("Company"); var Designation = adaptedFields.GetEntryByName("Designation"); _log.Info("Writing comment to database"); HttpContext.Current.Session["Email"] = email; /// var goal = new PageEventData("My Custom Goal", Guid.Parse("{47FF654B-76B2-49EF-A6AA-C61AE6093768}")); // Tracker.Current.CurrentPage.Register(goal); LockAttemptResult <Contact> lockResult; ContactRepository contactRepository = Sitecore.Configuration.Factory.CreateObject("tracking/contactRepository", true) as ContactRepository; ContactManager contactManager = Sitecore.Configuration.Factory.CreateObject("tracking/contactManager", true) as ContactManager; var contact = Tracker.Current?.Contact; if (contact == null) { lockResult = new LockAttemptResult <Contact>(LockAttemptStatus.NotFound, null, null); contact = contactRepository.CreateContact(Sitecore.Data.ID.NewID); contact.Identifiers.AuthenticationLevel = Sitecore.Analytics.Model.AuthenticationLevel.None; contact.Identifiers.Identifier = "*****@*****.**"; contact.Identifiers.IdentificationLevel = ContactIdentificationLevel.Known; contact.System.Value = 0; contact.System.VisitCount = 0; contact.ContactSaveMode = ContactSaveMode.AlwaysSave; contactManager.FlushContactToXdb(contact); // VisitorIdentification s = new VisitorIdentification(); } else { lockResult = contactManager.TryLoadContact(contact.ContactId); contact = lockResult.Object; } if (contact != null) { var contactEmailAddresses = contact.GetFacet <IContactCommentsFacet>("Contact Details"); if (!contactEmailAddresses.CustomerDetails.Contains("Contact Details")) { contactEmailAddresses.CustomerDetails.Create("Contact Details"); contactEmailAddresses.CustomerDetails["Contact Details"].Age = "18"; contactEmailAddresses.CustomerDetails["Contact Details"].Company = "Companyemarr"; contactEmailAddresses.CustomerDetails["Contact Details"].FullName = "Designationdsddf"; contactEmailAddresses.CustomerDetails["Contact Details"].Email = "Emailaddress"; } var leaseOwner = new LeaseOwner(GetType() + Guid.NewGuid().ToString(), LeaseOwnerType.OutOfRequestWorker); contactManager.SaveAndReleaseContactToXdb(contact); } }
public Contact GetContact(string identifier) { if (IsContactInSession(identifier)) { return(Tracker.Current.Session.Contact); } var matchedContact = Tracker.Current.Session.Contact; var contact = contactRepository.LoadContactReadOnly(identifier); if (contact != null) { LockAttemptResult <Contact> lockResult = contactManager.TryLoadContact(contact.ContactId); switch (lockResult.Status) { case LockAttemptStatus.Success: Contact lockedContact = lockResult.Object; lockedContact.ContactSaveMode = ContactSaveMode.AlwaysSave; matchedContact = lockedContact; break; } } if (!matchedContact.ContactId.Equals(Tracker.Current.Session.Contact.ContactId)) { Tracker.Current.Session.Identify(identifier); } return(matchedContact); }
public ActionResult Index(SubscribeModel subscribe) { if (ModelState.IsValid) { using (new SecurityDisabler()) { try { string emailAddress = subscribe.Email; EmailHelper emailhelper = new EmailHelper(); ContactRepository contactRepository = Sitecore.Configuration.Factory.CreateObject("tracking/contactRepository", true) as ContactRepository; var contact = contactRepository.LoadContactReadOnly(emailAddress); ContactManager contactManager = Sitecore.Configuration.Factory.CreateObject("tracking/contactManager", true) as ContactManager; LockAttemptResult <Contact> lockResult; if (contact == null) { lockResult = new LockAttemptResult <Contact>(LockAttemptStatus.NotFound, null, null); contact = contactRepository.CreateContact(Sitecore.Data.ID.NewID); contact.Identifiers.AuthenticationLevel = Sitecore.Analytics.Model.AuthenticationLevel.None; contact.Identifiers.Identifier = emailAddress; contact.Identifiers.IdentificationLevel = ContactIdentificationLevel.Known; contact.System.Value = 0; contact.System.VisitCount = 0; contact.ContactSaveMode = ContactSaveMode.AlwaysSave; contactManager.FlushContactToXdb(contact); } else { lockResult = contactManager.TryLoadContact(contact.ContactId); contact = lockResult.Object; } var personal = contact.GetFacet <Sitecore.Analytics.Model.Entities.IContactPersonalInfo>("Personal"); personal.FirstName = subscribe.FullName; var emailAddresses = contact.GetFacet <Sitecore.Analytics.Model.Entities.IContactEmailAddresses>("Emails"); if (!emailAddresses.Entries.Contains("Email")) { emailAddresses.Entries.Create("Email"); } var email = emailAddresses.Entries["Email"]; email.SmtpAddress = emailAddress; emailAddresses.Preferred = "Email"; var contactEmailAddresses = contact.GetFacet <IVisitorSubscribtionFacet>("Contact Details"); if (!contactEmailAddresses.SubscriptionDetails.Contains("Subscriber Details")) { contactEmailAddresses.SubscriptionDetails.Create("Subscriber"); contactEmailAddresses.SubscriptionDetails["Subscriber"].FullName = subscribe.FullName; contactEmailAddresses.SubscriptionDetails["Subscriber"].ContactNumber = subscribe.ContactNumber; contactEmailAddresses.SubscriptionDetails["Subscriber"].Email = subscribe.Email; contactEmailAddresses.SubscriptionDetails["Subscriber"].Comments = subscribe.Comments; } ContactListManager listManager = Sitecore.Configuration.Factory.CreateObject("contactListManager", false) as ContactListManager; contactManager.SaveAndReleaseContactToXdb(contact); Tracker.Current.Session.Identify(subscribe.Email); //Getting the Email Campaign ManagerRoot Item managerRoot = Sitecore.Data.Database.GetDatabase(SitecoreHelper.GetSitecoreDB().ToString()).GetItem(GlobalProperties.EmailCampaignMangerRoot); RecipientId recipient = RecipientRepository.GetDefaultInstance().ResolveRecipientId("xdb:" + contact.ContactId); var recipientId = new Sitecore.Modules.EmailCampaign.Xdb.XdbContactId(contact.ContactId); Sitecore.Modules.EmailCampaign.ClientApi.UpdateSubscriptions(recipientId, new[] { GlobalGuids.ContactList }, new string[] { }, managerRoot.ID.ToString(), false); emailhelper.SendEmail(subscribe); subscribe.IsFormEnabled = false; } catch (Exception ex) { subscribe.IsError = true; subscribe.IsFormEnabled = false; Sitecore.Diagnostics.Log.Error(ex.Message, this); } } return(View("~/Views/Pages/Subscribe.cshtml", subscribe)); } subscribe.IsError = false; subscribe.IsFormEnabled = true; return(View("~/Views/Pages/Subscribe.cshtml", subscribe)); }
private bool ImportXdbContact(Models.Contact contact) { var leaseOwner = new LeaseOwner(GetType() + Guid.NewGuid().ToString(), LeaseOwnerType.OutOfRequestWorker); Contact xdbContact = null; Guid contactGuid = Guid.Empty; // Attempt to obtain an exclusive lock on an existing contact in xDB. LockAttemptResult <Contact> lockResult = _contactRepository.TryLoadContact(contact.Email, leaseOwner, TimeSpan.FromSeconds(30)); // If contact is not found by email try by contactId if (lockResult.Status == LockAttemptStatus.NotFound) { if (Guid.TryParse(contact.ContactId, out contactGuid)) { lockResult = _contactRepository.TryLoadContact(contactGuid, leaseOwner, TimeSpan.FromSeconds(30)); } } // If contact is still not found create a new contact if (lockResult.Status == LockAttemptStatus.NotFound) { // if contactGuid is NOT Guid.Empty use it or just generate new Guid xdbContact = XdbContactRepository.CreateContact(contactGuid != Guid.Empty ? contactGuid : Guid.NewGuid()); //Setting these values allows processing and aggregation to process this contact without erroring. xdbContact.System.Value = 0; xdbContact.System.VisitCount = 0; Log.Info(string.Format("{0} Creating new contact {1}", _logInfoPrefix, contact.ContactId), this); } else if (lockResult.Status == LockAttemptStatus.AlreadyLocked) { Log.Info(string.Format("{0} Contact {1} is locked and cannot be processed.", _logInfoPrefix, contact.ContactId), this); return(false); } else if (lockResult.Status == LockAttemptStatus.DatabaseUnavailable) { Log.Info(string.Format("{0} Database is unavailable. Contact {1} cannot be processed.", _logInfoPrefix, contact.ContactId), this); return(false); } else { xdbContact = lockResult.Object; Log.Info(string.Format("{0} Updating contact {1}", _logInfoPrefix, contact.ContactId), this); } // Update contact Identifier if needed if (xdbContact.Identifiers.IdentificationLevel != ContactIdentificationLevel.Known) { xdbContact.Identifiers.IdentificationLevel = Sitecore.Analytics.Model.ContactIdentificationLevel.Known; } if (xdbContact.Identifiers.Identifier != contact.Email) { xdbContact.Identifiers.Identifier = contact.Email; } // Personal facet var personalFacet = xdbContact.GetFacet <IContactPersonalInfo>("Personal"); personalFacet.FirstName = personalFacet.FirstName != contact.FirstName ? contact.FirstName : personalFacet.FirstName; personalFacet.Surname = personalFacet.Surname != contact.LastName ? contact.LastName : personalFacet.Surname; // Emails facet var emailsFacet = xdbContact.GetFacet <IContactEmailAddresses>("Emails"); emailsFacet.Preferred = "Home"; var email = emailsFacet.Entries.Contains("Home") ? emailsFacet.Entries["Home"] : emailsFacet.Entries.Create("Home"); email.SmtpAddress = email.SmtpAddress != contact.Email ? contact.Email : email.SmtpAddress; // Addresses facet var addressesFacet = xdbContact.GetFacet <IContactAddresses>("Addresses"); addressesFacet.Preferred = "Home"; var address = addressesFacet.Entries.Contains("Billing") ? addressesFacet.Entries["Billing"] : addressesFacet.Entries.Create("Billing"); address.Country = address.Country != contact.Country ? contact.Country : address.Country; address.PostalCode = address.PostalCode != contact.PostalCode ? contact.PostalCode : address.PostalCode; return(XdbContactRepository.SaveContact(xdbContact, new ContactSaveOptions(true, leaseOwner))); }
protected string DoImportContacts(ContactImportOptions options) { Assert.ArgumentNotNull(options, "options"); var contactManager = Factory.CreateObject("tracking/contactManager", true) as ContactManager; var contactRepository = Factory.CreateObject("tracking/contactRepository", true) as ContactRepository; var numOfImported = 0; var numOfContactExists = 0; var numOfBadContacts = 0; var numOfContactsAddedToState = 0; using (var csvFileReader = new CsvFileReader(options.FileName)) { try { var columnNameFields = csvFileReader.ReadLine(); var contactIdentifierIndex = columnNameFields.FindIndex(h => string.Equals(options.ContactIdentifierField, h)); if (contactIdentifierIndex < 0) { return(string.Empty); } var contactFirstNameIndex = columnNameFields.FindIndex(x => string.Equals(options.ContactFirstNameField, x)); var contactLastNameIndex = columnNameFields.FindIndex(x => string.Equals(options.ContactLastNameField, x)); var contactEmailIndex = columnNameFields.FindIndex(x => string.Equals(options.ContactEmailField, x)); var valueFields = csvFileReader.ReadLine(); while (valueFields != null) { var contactIdentifier = valueFields[contactIdentifierIndex]; if (string.IsNullOrWhiteSpace(contactIdentifier)) { numOfBadContacts++; } else { var leaseOwner = new LeaseOwner("AddContacts-" + Guid.NewGuid(), LeaseOwnerType.OutOfRequestWorker); LockAttemptResult <Contact> lockAttemptResult; var contact = contactManager.LoadContactReadOnly(contactIdentifier); if (contact != null) { numOfContactExists++; lockAttemptResult = contactRepository.TryLoadContact(contact.ContactId, leaseOwner, TimeSpan.FromSeconds(3)); if (lockAttemptResult.Status == LockAttemptStatus.Success) { contact = lockAttemptResult.Object; contact.ContactSaveMode = ContactSaveMode.AlwaysSave; } else { Log.Error("Cannot lock contact! " + lockAttemptResult.Status, this); } } else { contact = contactRepository.CreateContact(ID.NewID); contact.Identifiers.Identifier = contactIdentifier; contact.System.Value = 0; contact.System.VisitCount = 0; contact.ContactSaveMode = ContactSaveMode.AlwaysSave; lockAttemptResult = new LockAttemptResult <Contact>(LockAttemptStatus.Success, contact, leaseOwner); } UpdateContactPersonalInfo(contact, contactFirstNameIndex, valueFields, contactLastNameIndex); UpdateContactEmailAddress(contactEmailIndex, contact, valueFields); if ((lockAttemptResult.Status != LockAttemptStatus.AlreadyLocked) && (lockAttemptResult.Status != LockAttemptStatus.NotFound)) { if (contact.AutomationStates().IsInEngagementPlan(options.PlanId)) { contact.AutomationStates().MoveToEngagementState(options.PlanId, options.StateId); Log.Info( string.Format("Move contact: {0} to engagement plan stateId: {1}", contact.ContactId, options.StateId), this); } else { contact.AutomationStates().EnrollInEngagementPlan(options.PlanId, options.StateId); Log.Info( string.Format("Enrolled contact: {0} to engagement plan stateId: {1}", contact.ContactId, options.StateId), this); } contactRepository.SaveContact(contact, new ContactSaveOptions(true, leaseOwner, null)); numOfContactsAddedToState++; } else { Log.Error( string.Format("Failed to enroll contact: {0} in engagement plan stateId: {1}", contact.ContactId, options.StateId), this); } numOfImported++; } valueFields = csvFileReader.ReadLine(); } } catch (Exception ex) { Log.Error(ex.Message, ex, this); } } Log.Info( string.Format( "Import Contacts Finished: Imported: {0}, Contact Exists: {1}, Bad Contact Data: {2}, Added to State: {3}", numOfImported, numOfContactExists, numOfBadContacts, numOfContactsAddedToState), GetType()); return(numOfImported + "|" + numOfContactExists + "|" + numOfBadContacts + "|" + "|" + numOfContactsAddedToState + "|"); }