The default Sitecore Engagement Plan currently supports the following option to assign users/contacts to an engagement plan state
- Add from CSV File
- Add a Sitecore user
- Add all Members of a Sitecore Role
- Add a Segment
Out off all those options only the segment option have the capability of assigning existing contacts to an engagement plan state.
However, what if we would like to import contacts from a CSV file there’s no option to do so, the Add from CSV file will import the data as Sitecore users instead of contacts.
We would need to modify the UI in order to achieve the functionality that we’re aiming for. To do so first we would need to figure which file to modify.
As the UI is using Silverlight it’s a bit difficult to figure which file we need to modify, however searching for the “Welcome to the Import Contacts Wizard” text under the /sitecore folder reveals that this file seems to be the correct one
1 |
\sitecore\shell\Applications\MarketingAutomation\Dialogs\ImportVisitors.xml |
Further inspecting the code inside that file and tinkering with it a bit confirms that this is the correct file to modify.
We will not modify the existing Sitecore file, instead we will use the Override folder in order to apply our modification to the existing XML control. To do this copy and paste the existing ImportVisitors.xml file to the override folder
1 |
\sitecore\shell\Override\Applications\MarketingAutomation\Dialogs\ImportVisitors.xml |
You can put the file directly inside the Override folder, but it’s good practice to replicate the original folder paths so you know which file you’re overriding. After we now know which file to modify we can start modifying the default behaviour of that XML control.
The modified ImportVisitors.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 |
<?xml version="1.0" encoding="utf-8" ?> <control xmlns:def="Definition" xmlns="http://schemas.sitecore.net/Visual-Studio-Intellisense"> <ImportVisitors> <WizardForm CodeBeside="TC.SitecoreEngagementPlanUI.Shell.Applications.MarketingAutomations.Dialogs,TC.SitecoreEngagementPlanUI"> <style type="text/css"> th.scListviewViewDetailsHeader { display:none; } #RolesBox div { padding-bottom: 2px; } input { font-family: verdana; font-size: 9pt; } .orderedList{ list-style-type: decimal; list-style-position: inside; } .orderedList > li{ list-style-position: inside; padding-left: 8px; list-style-type: square; } </style> <WizardFormFirstPage ID="FirstPage" Icon="People/32x32/users_into.png"> <Border Class="scWizardWelcomeTitle"> <Literal Text="Welcome to the Import Users/Contacts Wizard" /> </Border> <Literal Text="This wizard helps you:" /> <ul> <li class="scWizardBullet"> <Literal Text="Select a CSV file that contains visitor information." /> </li> <li class="scWizardBullet"> <Literal Text="Then do one of the followings:" /> <ol> <li class="scWizardBullet"> <Literal Text="1. Import as a user" /> <ul class="orderedList"> <li class="scWizardBullet"> <Literal Text="Map the information in the file to the fields in the user profile." /> </li> <li class="scWizardBullet"> <Literal Text="Select a domain for new visitors." /> </li> <li class="scWizardBullet"> <Literal Text="Add visitors to the selected domain." /> </li> <li class="scWizardBullet"> <Literal Text="Add visitors to a state." /> </li> </ul> </li> <li class="scWizardBullet"> <Literal Text="2. Import as a contact" /> <ul class="orderedList"> <li class="scWizardBullet"> <Literal Text="Map the information in the file to contact fields" /> </li> <li class="scWizardBullet"> <Literal Text="Add visitors to a state." /> </li> </ul> </li> </ol> </li> </ul> </WizardFormFirstPage> <WizardFormPage ID="ChooseAction" Header="Choose action" Text="Choose to import as a user or contact"> <WizardFormPadding> <GridPanel Width="100%" Valign="top"> <Label Header="Import Action:" GridPanel.VAlign="top" /> <Space Height="16" /> <Radiobutton ID="ImportUsers" Name="ChooseAction" Value="ImportUsers" Header="Import as Users"></Radiobutton> <Literal Text="Importing as Users will import the visitor as a registered User in Sitecore" Style="font-weight:bold;" GridPanel.Colspan="2"/> <Space Height="8" /> <Radiobutton ID="ImportContacts" Name="ChooseAction" Value="ImportContacts" Header="Import as Contacts"></Radiobutton> <Literal Text="Importing as Contacts will import the visitor as a Contact in the Sitecore xDB" Style="font-weight:bold;" GridPanel.Colspan="2"/> </GridPanel> </WizardFormPadding> </WizardFormPage> <WizardFormPage ID="SelectFile" Header="File to Import" Text="Select the CSV file that contains the visitor information that you want to import. This file must be in .csv format." Icon="People/32x32/users_into.png"> <WizardFormPadding> <GridPanel Width="100%" VAlign="top" Cellspacing="4" > <Label Header="File Name:" GridPanel.VAlign="top" /> <GridPanel GridPanel.VAlign="top" Columns="3" Width="100%"> <Edit ID="Filename" GridPanel.Width="100%" Width="100%" ReadOnly="true" /> <Space Width="6px" /> <Border Width="75px" GridPanel.Width="75px"> <Border Style="position: relative;"> <Frame ID="UploadFileFrame" Height="26px" Style="position: relative; -moz-opacity:0; filter:alpha(opacity: 0); opacity: 0; z-index: 2;" SourceUri="/sitecore/shell/Applications/MarketingAutomation/Dialogs/UploadFile.aspx"/> <button class="scButton" type="button" Style="position: absolute; top: 1px; left: 0px; z-index: 1;"> <Literal Text="Browse" /> </button> </Border> </Border> </GridPanel> </GridPanel> </WizardFormPadding> </WizardFormPage> <WizardFormPage ID="Fields" Header="Fields" Text="Select the fields that you want to import and the fields that you want to map them to." Icon="People/32x32/users_into.png"> <WizardFormPadding> <GridPanel Height="100%" Width="100%"> <GridPanel Width="100%" VAlign="top" Cellspacing="8" Style="padding-left: 4px;"> <Label Header="User Name:" /> <EditableCombobox ID="UserName" SelectOnly="true" Width="100%" /> </GridPanel> <Scrollbox ID="FieldsSection" GridPanel.Height="100%" Border="0" Style="background: transparent;"> <GridPanel Width="100%" VAlign="top" Cellspacing="8" Columns="3" > <Label Header="From:" GridPanel.Width="50%" /> <Label Header="To:" GridPanel.Width="50%" /> <Border Height="16" Width="16" /> </GridPanel> </Scrollbox> </GridPanel> </WizardFormPadding> </WizardFormPage> <WizardFormPage ID="DomainAndRole" Header="Domain and Role" Text="Select the security domain and role (optional) that you want to add new visitors to." Icon="People/32x32/users_into.png" > <WizardFormPadding> <GridPanel Width="100%" VAlign="top" > <Label Header="Domain:" /> <Space Height="4"/> <EditableCombobox ID="DomainCombobox" SelectOnly="true" Width="100%" Visible="false" /> <GridPanel ID="DomainPanel" GridPanel.VAlign="top" Columns="3" Width="100%" Visible="false"> <Edit ID="DomainInput" GridPanel.Width="100%" Width="100%" ReadOnly="true" /> <Space Width="6px" /> <Button Header="Select" Click="SelectDomain_Click" /> </GridPanel> <Space Height="8"/> <Label Header="Role (optional):" /> <Space Height="4"/> <GridPanel GridPanel.VAlign="top" Columns="3" Width="100%"> <Edit ID="RoleInput" GridPanel.Width="100%" Width="100%" /> <Space Width="6px" /> <Button Header="Select" Click="SelectRole_Click" /> </GridPanel> <Space Height="34"/> <Label Header="If a record in the file contains a user name that already exists:" /> <Space Height="4"/> <GridPanel Cellspacing="0"> <Radiobutton ID="SkipProperties" Name="Conflict" Value="SkipProperties" Header="Do not modify profile fields." Checked="true" /> <Radiobutton ID="OverwriteProperties" Name="Conflict" Value="OverwriteProperties" Header="Overwrite existing profile fields." /> <Radiobutton ID="StoreProperties" Name="Conflict" Value="StoreProperties" Header="Only store data in empty profile fields." /> </GridPanel> </GridPanel> </WizardFormPadding> </WizardFormPage> <WizardFormPage ID="ContactFields" Header="Contact Fields" Text="Select the field that you want to identify the contact as" Icon="People/32x32/users_into.png"> <WizardFormPadding> <GridPanel Width="100%" VAlign="top" Cellspacing="8" Style="padding-left: 4px;"> <Label Header="Contact Identifier (required):" /> <EditableCombobox ID="ContactIdentifier" SelectOnly="true" Width="100%" /> <Space Height="16" /> <Label Header="First Name:" /> <EditableCombobox ID="ContactFirstName" SelectOnly="true" Width="100%" /> <Space Height="16" /> <Label Header="Last Name:" /> <EditableCombobox ID="ContactLastName" SelectOnly="true" Width="100%" /> <Space Height="16" /> <Label Header="Contact Email:" /> <EditableCombobox ID="ContactEmail" SelectOnly="true" Width="100%" /> </GridPanel> </WizardFormPadding> </WizardFormPage> <WizardFormPage ID="Importing" Header="Importing Contacts" Text="Please wait while the wizard imports visitors." Icon="People/32x32/users_into.png"> <WizardFormPadding> <GridPanel Width="100%" VAlign="Top"> <Space Height="64" /> <Border Align="center"> <ThemedImage Src="People/32x32/users_into.png" Width="48" Height="48" /> </Border> <Space Height="16" /> <Border Align="center"> <ThemedImage Src="Images/progress.gif" Width="94" Height="17" /> </Border> <Space Height="16" /> <Border Align="center" > <Literal Text="Importing..." Style="font-weight:bold"/> </Border> </GridPanel> </WizardFormPadding> </WizardFormPage> <WizardFormLastPage ID="Finish" Icon="People/32x32/users_into.png"> <GridPanel Width="100%"> <Literal Text="The visitor information has been imported successfully." /> <Border> <Space Height="14px" /> </Border> <Border ID="Results"> <GridPanel Columns="2" Cellspacing="6" Valign="top"> <Literal Text="Importing" Style="font-weight:bold;" GridPanel.Colspan="2"/> <Literal ID="LiteralUsersImported" Text="Users imported:" Style="padding-left: 14px;"/> <Literal ID="NumImported" /> <Border GridPanel.Colspan="2"> <Space Height="16px" /> </Border> <Literal ID="SkippedText" Text="Records skipped:" GridPanel.Colspan="2" Style="padding-left: 14px;" /> <Literal ID="LiteralUserExists" Text="User exists:" Style="padding-left: 28px;"/> <Literal ID="NumUserExists" /> <Literal ID="LiteralBadUserName" Text="Invalid user name:" Style="padding-left: 28px;"/> <Literal ID="NumBadUserName" /> <Literal ID="LiteralBrokenStructure" Text="Broken stucture:" Style="padding-left: 28px;"/> <Literal ID="NumBroken" /> <Border GridPanel.Colspan="2"> <Space Height="16px" /> </Border> <Literal Text="Adding to state" Style="font-weight:bold" GridPanel.Colspan="2"/> <Literal Text="Contacts added to state:" Style="padding-left: 14px;"/> <Literal ID="NumAddedToState" /> </GridPanel> </Border> </GridPanel> </WizardFormLastPage> </WizardForm> </ImportVisitors> </control> |
The code beside file
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 |
using System; using System.Collections.Generic; using System.Web; using Sitecore; using Sitecore.Analytics; using Sitecore.Analytics.Automation.MarketingAutomation; using Sitecore.Analytics.Data; using Sitecore.Analytics.DataAccess; using Sitecore.Analytics.Model; using Sitecore.Analytics.Model.Entities; using Sitecore.Analytics.Tracking; using Sitecore.Configuration; using Sitecore.Data; using Sitecore.Diagnostics; using Sitecore.Globalization; using Sitecore.IO; using Sitecore.Jobs; using Sitecore.Resources; using Sitecore.Web.UI.HtmlControls; using Sitecore.Web.UI.Sheer; namespace TC.SitecoreEngagementPlanUI.Shell.Applications.MarketingAutomations.Dialogs { public class ImportVisitorsForm : Sitecore.Shell.Applications.MarketingAutomation.Dialogs.ImportVisitorsForm { protected EditableCombobox ContactEmail; protected EditableCombobox ContactFirstName; protected EditableCombobox ContactIdentifier; protected EditableCombobox ContactLastName; protected Radiobutton ImportContacts; protected Radiobutton ImportUsers; protected Literal LiteralBadUserName; protected Literal LiteralBrokenStructure; protected Literal LiteralUserExists; protected Literal LiteralUsersImported; protected ImportActionType ImportAction { get { if (ImportUsers.Checked) return ImportActionType.ImportAsUser; if (ImportContacts.Checked) return ImportActionType.ImportAsContact; return ImportActionType.Unknown; } } protected override bool ActivePageChanging(string page, ref string newpage) { Assert.ArgumentNotNull(page, "page"); Assert.ArgumentNotNull(newpage, "newpage"); switch (page) { case "ChooseAction": if (ImportAction == ImportActionType.Unknown) { SheerResponse.Alert("Please select one of the action available"); return false; } break; case "SelectFile": if (newpage.Equals("Fields") && !ReadFile()) return false; if ((ImportAction == ImportActionType.ImportAsContact) && newpage.Equals("Fields")) newpage = "ContactFields"; break; case "Fields": if (newpage.Equals("DomainAndRole") && !CheckFields()) return false; break; case "DomainAndRole": if (newpage.Equals("ContactFields")) { if (!CheckDomainAndRole()) return false; StartImport(); newpage = "Importing"; } break; case "ContactFields": if (newpage.Equals("Importing")) { if (ImportAction == ImportActionType.ImportAsContact) { if (ContactIdentifier.Value.Equals(FormatDefaultText(Translate.Text("select field")))) { SheerResponse.Alert("Contact Identifier field must have a valid value"); return false; } StartImportingContacts(); } } else if (newpage.Equals("DomainAndRole")) { if (ImportAction == ImportActionType.ImportAsContact) newpage = "SelectFile"; } break; case "Importing": if (newpage.Equals("Finish")) { if (ImportAction == ImportActionType.ImportAsContact) { LiteralUserExists.Text = "Contact exists:"; LiteralBadUserName.Text = "Invalid contact:"; LiteralUsersImported.Text = "Contacts imported:"; LiteralBrokenStructure.Visible = false; NumBroken.Visible = false; } if (ImportAction == ImportActionType.ImportAsUser) { LiteralUserExists.Text = "User exists:"; LiteralBadUserName.Text = "Invalid user name:"; LiteralUsersImported.Text = "Users Imported:"; LiteralBrokenStructure.Visible = true; NumBroken.Visible = true; } } break; } return base.ActivePageChanging(page, ref newpage); } public new void Next() { var index = Pages.IndexOf(Active) + 1; if (index >= Pages.Count) Active = Pages[Pages.Count - 1]; else Active = Pages[index]; } private void InitializeFields() { for (var index = ParentControl.Controls.Count - 1; index >= 0; --index) { var control = ParentControl.Controls[index]; if (!string.IsNullOrEmpty(control.ID) && (control.ID.StartsWith("Field") || control.ID.StartsWith("Property") || control.ID.StartsWith("DelSection"))) ParentControl.Controls.Remove(control); } InitializeUserNameField(); InitializeContactFields(); var prevIdPostfix = string.Empty; foreach (var str in FieldList) { var f = str; var defProperty = PropertyList.Find(p => string.Equals(f, p, StringComparison.OrdinalIgnoreCase)); if (!string.IsNullOrEmpty(defProperty)) prevIdPostfix = AddRow(prevIdPostfix, defProperty); } AddRow(prevIdPostfix); SheerResponse.Refresh(FieldsSection); } private void InitializeContactFields() { ContactIdentifier.List = FieldList; ContactIdentifier.Value = FormatDefaultText(Translate.Text("select field")); ContactFirstName.List = FieldList; ContactFirstName.Value = FormatDefaultText(Translate.Text("select field")); ContactLastName.List = FieldList; ContactLastName.Value = FormatDefaultText(Translate.Text("select field")); ContactEmail.List = FieldList; ContactEmail.Value = FormatDefaultText(Translate.Text("select field")); } protected void StartImportingContacts() { var importOptions = new ContactImportOptions { FileName = RealFilename, PlanId = Tracker.DefinitionDatabase.GetItem(StateId).ParentID, StateId = StateId, ContactIdentifierField = ContactIdentifier.Value, ContactFirstNameField = ContactFirstName.Value, ContactLastNameField = ContactLastName.Value, ContactEmailField = ContactEmail.Value }; StartJob("Import Contacts", "DoImportContacts", this, importOptions); CheckImport(); } 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 + "|"; } private void UpdateContactPersonalInfo(Contact contact, int contactFirstNameIndex, List<string> valueFields, int contactLastNameIndex) { var contactPersonalInfoFacet = contact.GetFacet<IContactPersonalInfo>("Personal"); if (contactFirstNameIndex >= 0) contactPersonalInfoFacet.FirstName = valueFields[contactFirstNameIndex]; if (contactLastNameIndex >= 0) contactPersonalInfoFacet.Surname = valueFields[contactLastNameIndex]; } private void UpdateContactEmailAddress(int contactEmailIndex, Contact contact, List<string> valueFields) { if (contactEmailIndex >= 0) { var contactEmailFacet = contact.GetFacet<IContactEmailAddresses>("Emails"); contactEmailFacet.Preferred = "Preferred"; if (contactEmailFacet.Entries.Contains("Preferred")) { var preferredEmailAddress = contactEmailFacet.Entries["Preferred"]; preferredEmailAddress.SmtpAddress = valueFields[contactEmailIndex]; } else { var preferredEmailAddress = contactEmailFacet.Entries.Create("Preferred"); preferredEmailAddress.SmtpAddress = valueFields[contactEmailIndex]; } } } protected class ContactImportOptions { public string FileName { get; set; } public ID PlanId { get; set; } public ID StateId { get; set; } public string ContactIdentifierField { get; set; } public string ContactFirstNameField { get; set; } public string ContactLastNameField { get; set; } public string ContactEmailField { get; set; } } protected enum ImportActionType { Unknown, ImportAsUser, ImportAsContact } #region Methods from base class protected new bool ReadFile() { if (string.IsNullOrEmpty(RealFilename)) { SheerResponse.Alert(Translate.Text("Select a file first.")); return false; } if (!FileUtil.FileExists(RealFilename)) { SheerResponse.Alert(Translate.Text("'{0}' file does not exist.", (object) RealFilename)); return false; } if (!RealFilename.Equals(LastFile)) using (var csvFileReader = new CsvFileReader(RealFilename)) { FieldList = csvFileReader.ReadLine(); LastFile = RealFilename; InitializeFields(); } return true; } protected new void CheckImport() { var str = Context.ClientPage.ServerProperties["job"] as string; var job = !string.IsNullOrEmpty(str) ? JobManager.GetJob(Handle.Parse(str)) : null; if (job == null) Next(); else if (job.IsDone) { if (job.Status.Result != null) UpdateForm(job.Status.Result.ToString()); Next(); } else SheerResponse.Timer("CheckImport", 300); } private void UpdateForm(string results) { if (string.IsNullOrEmpty(results)) return; var strArray = results.Split('|'); if (strArray.Length < 5) return; NumImported.Text = strArray[0]; NumUserExists.Text = strArray[1]; NumBadUserName.Text = strArray[2]; NumBroken.Text = strArray[3]; NumAddedToState.Text = strArray[4]; SheerResponse.Refresh(Results); } private void AddDelSection(string idPostfix) { Assert.ArgumentNotNull(idPostfix, "idPostfix"); var imageBuilder = new ImageBuilder { Src = "Applications/16x16/delete2.png", Width = 16, Height = 16, Style = "cursor: pointer" }; var literal = new Literal { Text = imageBuilder.ToString() }; var border1 = new Border(); border1.ID = "DelSection" + idPostfix; border1.Click = "DelSection_Click"; var border2 = border1; border2.Controls.Add(literal); ParentControl.Controls.Add(border2); } private void AddFieldCombobox(string idPostfix, string defValue) { Assert.ArgumentNotNull(idPostfix, "idPostfix"); var editableCombobox1 = new EditableCombobox(); editableCombobox1.ID = "Field" + idPostfix; editableCombobox1.SelectOnly = true; editableCombobox1.List = FieldList; var editableCombobox2 = editableCombobox1; if (!string.IsNullOrEmpty(defValue)) foreach (var str in FieldList) if (defValue.Equals(str, StringComparison.OrdinalIgnoreCase)) { editableCombobox2.Value = str; break; } if (string.IsNullOrEmpty(editableCombobox2.Value)) editableCombobox2.Value = HttpUtility.HtmlEncode(FormatDefaultText(Translate.Text("select to add field"))); ParentControl.Controls.Add(editableCombobox2); } private void AddPropertyCombobox(string idPostfix, string defValue) { Assert.ArgumentNotNull(idPostfix, "idPostfix"); var editableCombobox1 = new EditableCombobox(); editableCombobox1.ID = "Property" + idPostfix; editableCombobox1.SelectOnly = true; editableCombobox1.List = PropertyList; var editableCombobox2 = editableCombobox1; if (string.IsNullOrEmpty(defValue)) editableCombobox2.Value = HttpUtility.HtmlEncode(FormatDefaultText(Translate.Text("select property"))); else foreach (var str in PropertyList) if (defValue.Equals(str, StringComparison.OrdinalIgnoreCase)) { editableCombobox2.Value = str; break; } ParentControl.Controls.Add(editableCombobox2); } private string AddRow(string prevIdPostfix) { return AddRow(prevIdPostfix, string.Empty); } private string AddRow(string prevIdPostfix, string defProperty) { var uniqueId = Control.GetUniqueID(string.Empty); if (!string.IsNullOrEmpty(prevIdPostfix)) AddDelSection(prevIdPostfix); AddFieldCombobox(uniqueId, defProperty); AddPropertyCombobox(uniqueId, defProperty); return uniqueId; } private string FormatDefaultText(string text) { Assert.ArgumentNotNull(text, "text"); return "<" + text + ">"; } private void InitializeUserNameField() { UserName.List = FieldList; UserName.Value = FormatDefaultText(Translate.Text("select field")); } private void StartJob(string name, string method, object helper, params object[] args) { Assert.ArgumentNotNull(name, "name"); Assert.ArgumentNotNull(method, "method"); Assert.ArgumentNotNull(helper, "helper"); var job = JobManager.Start(new JobOptions(name, method, Context.Site.Name, helper, method, args) { EnableSecurity = false, AfterLife = TimeSpan.FromSeconds(3.0), WriteToLog = true }); try { Context.ClientPage.ServerProperties["job"] = job.Handle.ToString(); } catch (Exception ex) { Log.Error(ex.Message, ex, this); } } #endregion } } |
The end result
Source Code is available in GitHub