Search code examples
c#wpfoutlookcontactitem

Receiving (dragged and) dropped Outlook contacts in C#?


I'm developing an application that needs to perform some processing on the user's Outlook contacts. I'm currently accessing the list of Outlook contacts by iterating over the result of MAPIFolder.Items.Restrict(somefilter), which can be found in Microsoft.Office.Interop.Outlook.

In my application, my user needs to choose several contacts to apply a certain operation on. I would like to add a feature that will allow the user to drag a contact from Outlook and drop it on a certain ListBox in the UI (I work in WPF but this is probably is more generic issue).

I'm very new to C# and WPF - how can I:

  1. Receive a dropped item on a ListBox
  2. Verify it's a ContactItem (or something that wraps ContactItem)
  3. Cast the dropped item to a ContactItem so I can process it

Thanks


Solution

  • I tried this with a TextBox (no difference with a ListBox in practice).

    Summary :

    Searching in all outlook contacts for the one recieved dragged as text. The search here is based on the person's FullName.

    condition(s):

    When you drag a contact, it must show the FullName when selected in outlook. The only catch is when two persons have the same full names!! If it's the case you can try to find a unique identifier for a person by combining ContactItem properties and searching them in the dragged text.

    private void textBox1_DragDrop(object sender, DragEventArgs e)
    {
        if (e.Data.GetData("Text") != null)
        {                
            ApplicationClass app;
            MAPIFolder mapif;
            string contactStr;
    
            contactStr = e.Data.GetData("Text").ToString();
    
            app = new ApplicationClass();                
    
            mapif = app.GetNamespace("MAPI").GetDefaultFolder(OlDefaultFolders.olFolderContacts);                
    
            foreach (ContactItem tci in mapif.Items)
            {
                if (contactStr.Contains(tci.FullName))
                {
                    draggedContact = tci; //draggedContact is a global variable for example or a property...
                    break;
                }                    
            }
    
            mapif = null;
    
            app.Quit;
            app = null;
            GC.Collect();
        }
    }
    

    of course this code is to be organized-optimized, it's only to explain the method used.

    You can try using the Explorer.Selection property combined with GetData("Text") [to ensure it's coming from Outlook, or you can use GetData("Object Descriptor") in the DragOver Event, decode the memory stream, search for "outlook", if not found cancel the drag operation] And why not drag multiple contacts!

    private void textBox1_DragDrop(object sender, DragEventArgs e)
    {
        if (e.Data.GetData("Text") != null)
        {
            ApplicationClass app;
            Explorer exp;
            List<ContactItem> draggedContacts;                
            string contactStr;
    
            contactStr = e.Data.GetData("Text").ToString();
    
            draggedContacts = new List<ContactItem>();
    
            app = new ApplicationClass();
            exp = app.ActiveExplorer();
            if (exp.CurrentFolder.DefaultItemType == OlItemType.olContactItem)
            {
                if (exp.Selection != null)
                {
                    foreach (ContactItem ci in exp.Selection)
                    {
                        if (contactStr.Contains(ci.FullName))
                        {
                            draggedContacts.Add(ci);
                        }
                    }
                }
            }
    
            app = null;
            GC.Collect();
        }
    }