daily pastebin goal
2%
SHARE
TWEET

Untitled

a guest Nov 18th, 2017 53 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System;
  2. using System.Linq;
  3. using Sitecore.Analytics.Model;
  4. using Sitecore.Analytics.Tracking;
  5. using Sitecore.Data;
  6. using Sitecore.Diagnostics;
  7. using Sitecore.Modules.EmailCampaign;
  8. using Sitecore.SecurityModel;
  9. using Sitecore.WFFM.Abstractions.Actions;
  10. using Sitecore.WFFM.Abstractions.Analytics;
  11. using Sitecore.WFFM.Abstractions.Shared;
  12.  
  13. namespace WffmCustomization
  14. {
  15.     public class AddContactToContactList : Sitecore.WFFM.Actions.SaveActions.AddContactToContactList
  16.     {
  17.         private const string RootListPath = "/sitecore/system/Modules/E-mail Campaign Manager/System/Root List";
  18.         private const string ManagerRootsFieldName = "Manager Roots";
  19.  
  20.         private readonly IAnalyticsTracker _analyticsTracker;
  21.  
  22.         public AddContactToContactList(IAnalyticsTracker analyticsTracker, IContactRepository contactRepository) : base(analyticsTracker, contactRepository)
  23.         {
  24.             _analyticsTracker = analyticsTracker;
  25.         }
  26.  
  27.         public override void Execute(ID formId, AdaptedResultList adaptedFields, ActionCallContext actionCallContext = null, params object[] data)
  28.         {
  29.             Assert.ArgumentNotNull(adaptedFields, "adaptedFields");
  30.             Assert.IsNotNullOrEmpty(ContactsLists, "Empty contact list.");
  31.             Assert.IsNotNull(_analyticsTracker.CurrentContact, "Tracker.Current.Contact");
  32.  
  33.             if (!adaptedFields.IsTrueStatement(ExecuteWhen))
  34.                 return;
  35.  
  36.             var lists = ContactsLists.Split(',').Select(x => ID.Parse(x).ToString()).ToArray();
  37.  
  38.             using (new SecurityDisabler())
  39.             {
  40.                 Contact currentContact = _analyticsTracker.CurrentContact;
  41.  
  42.                 if (currentContact.Identifiers.IdentificationLevel != ContactIdentificationLevel.Known)
  43.                 {
  44.                     var emailAddresses = currentContact.GetFacet<Sitecore.Analytics.Model.Entities.IContactEmailAddresses>("Emails");
  45.                     if (emailAddresses.Entries.Contains("Preferred"))
  46.                     {
  47.                         var email = emailAddresses.Entries["Preferred"];
  48.                         _analyticsTracker.Current.Session.Identify(email.SmtpAddress);
  49.                     }
  50.                 }
  51.  
  52.                 var recipientId = new Sitecore.Modules.EmailCampaign.Xdb.XdbContactId(currentContact.ContactId);
  53.  
  54.                 var rootList = (Sitecore.Context.ContentDatabase ?? Sitecore.Context.Database).GetItem(RootListPath);
  55.  
  56.                 Assert.IsNotNull(rootList, "Empty root list.");
  57.  
  58.                 var managerRootId = (rootList[ManagerRootsFieldName] ?? string.Empty).Split(new[] { '|' }, StringSplitOptions.RemoveEmptyEntries).FirstOrDefault();
  59.  
  60.                 Assert.IsNotNullOrEmpty(managerRootId, "Empty manager root id.");
  61.  
  62.                 Log.Info($"AddContactToContactList [{currentContact.ContactId}, {ContactsLists}, {managerRootId}]", this);
  63.  
  64.                 ClientApi.UpdateSubscriptions(recipientId, lists, new string[] { }, managerRootId, false);
  65.             }
  66.         }
  67.     }
  68. }
  69.    
  70. <configuration xmlns:patch="http://www.sitecore.net/xmlconfig/" xmlns:set="http://www.sitecore.net/xmlconfig/set/">
  71.   <sitecore>
  72.     <wffm>
  73.       <actions>
  74.         <addContactToContactList  type="Sitecore.WFFM.Actions.SaveActions.AddContactToContactList, Sitecore.WFFM.Actions" >
  75.           <patch:attribute name="type">WffmCustomization.AddContactToContactList, WffmCustomization</patch:attribute>
  76.         </addContactToContactList>
  77.       </actions>
  78.     </wffm>
  79.   </sitecore>
  80. </configuration>
RAW Paste Data
Top