OUT-401: Doublecheck if contact for given address already exists OUT-401
authorThomas
Thu, 15 Feb 2018 13:05:06 +0100
branchOUT-401
changeset 2006f2b0f06a8a5b
parent 2005 d5fa428e94ea
child 2007 31da7a34305e
OUT-401: Doublecheck if contact for given address already exists
PEPIdentity.cs
     1.1 --- a/PEPIdentity.cs	Thu Feb 15 10:27:00 2018 +0100
     1.2 +++ b/PEPIdentity.cs	Thu Feb 15 13:05:06 2018 +0100
     1.3 @@ -1623,20 +1623,48 @@
     1.4          /// <returns>The created contact or null if an error occured.</returns>
     1.5          private static Outlook.ContactItem CreateContact(string address, string userName, string addressType = null)
     1.6          {
     1.7 +            bool contactExists = false;
     1.8              Outlook.ContactItem contact = null;
     1.9  
    1.10 +            // Doublecheck to make sure there is not already a contact for this address
    1.11 +            Outlook.NameSpace ns = null;
    1.12 +            Outlook.Folder folder = null;
    1.13 +            Outlook.Items items = null;
    1.14              try
    1.15              {
    1.16 -                contact = Globals.ThisAddIn.Application.CreateItem(Outlook.OlItemType.olContactItem);
    1.17 -                contact.Email1Address = address;
    1.18 -                contact.FullName = userName;
    1.19 -                contact.Email1AddressType = addressType;
    1.20 -                contact.Save();
    1.21 +                ns = Globals.ThisAddIn.Application.Session;
    1.22 +                folder = ns.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderContacts) as Outlook.Folder;
    1.23 +                items = folder.Items;
    1.24 +                contactExists = (items.Restrict(string.Format("[Email1Address]='{0}' Or [Email2Address]='{0}' Or [Email3Address]='{0}'", address)) != null);
    1.25              }
    1.26              catch (Exception ex)
    1.27              {
    1.28 -                contact = null;
    1.29 -                Log.Error("CreateContact: Error creating contact. " + ex.ToString());
    1.30 +                contactExists = false;
    1.31 +                Log.Error("CreateContact: Error doublechecking existence of contact with same address. " + ex.ToString());
    1.32 +            }
    1.33 +            finally
    1.34 +            {
    1.35 +                ns = null;
    1.36 +                folder = null;
    1.37 +                items = null;
    1.38 +            }
    1.39 +
    1.40 +            // If contact doesn't exist, create it
    1.41 +            if (contactExists == false)
    1.42 +            {
    1.43 +                try
    1.44 +                {
    1.45 +                    contact = Globals.ThisAddIn.Application.CreateItem(Outlook.OlItemType.olContactItem);
    1.46 +                    contact.Email1Address = address;
    1.47 +                    contact.FullName = userName;
    1.48 +                    contact.Email1AddressType = addressType;
    1.49 +                    contact.Save();
    1.50 +                }
    1.51 +                catch (Exception ex)
    1.52 +                {
    1.53 +                    contact = null;
    1.54 +                    Log.Error("CreateContact: Error creating contact. " + ex.ToString());
    1.55 +                }
    1.56              }
    1.57  
    1.58              return contact;
    1.59 @@ -1742,10 +1770,19 @@
    1.60                      UserName = recipient?.Name?.Trim()
    1.61                  };
    1.62  
    1.63 -                // Get the unique user ID if possible (Assume no contact is available)
    1.64 +                // Get the unique user ID if possible
    1.65                  if (string.IsNullOrEmpty(newIdent.Address) == false)
    1.66                  {
    1.67 -                    newIdent.UserId = Globals.ThisAddIn.GetUserId(newIdent.Address);
    1.68 +                    Outlook.ContactItem contact = null;
    1.69 +
    1.70 +                    if (addContact)
    1.71 +                    {
    1.72 +                        contact = PEPIdentity.CreateContact(newIdent.Address, newIdent.UserName);
    1.73 +                    }
    1.74 +
    1.75 +                    newIdent.UserId = Globals.ThisAddIn.GetUserId(newIdent.Address, contact);
    1.76 +
    1.77 +                    contact = null;
    1.78                  }
    1.79              }
    1.80