none
Outlook 2007 - C# How to Move Large Number of Contacts from a Folder? RRS feed

  • General discussion

  • Hi Guys

    I have some functionality which after a user clicks on a button, will iterate through each Contact Item in the Contacts folder, and move only the contacts that meet a specific criteria.

    The Problem I have is that the process seems to complete OK without any exception, but not all of the Contacts that should have moved do actually move over to the second Contacts Folder.

    Example:

    Contacts Folder: 2575

    Contacts Moved: 784

    Now When I click the button again, it will move another subset but not the entire list of Contacts I would expect to move.

    Code

    Microsoft.Office.Interop.Outlook.Items contactItems = this.Application.Application.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderContacts).Items;
                for (int i = contactItems.Count; i >= 1; i--)
                {
                    var item = contactItems[i];
                    try
                    {
                        //Contact Item
                        var contact = item as Microsoft.Office.Interop.Outlook.ContactItem;
                        if (contact != null)
                        {
                            if (String.IsNullOrEmpty(contact.CustomerID))
                            {
                                contact.Move(newContactsFolder);
                            }
                            else
                            {
                                if (!contact.CustomerID.StartsWith("Custom:"))
                                {
                                    contact.Move(newContactsFolder);
                                }
                            }
                        }
                        else
                        {
                            //Distribution List Item
                            var dist = item as Microsoft.Office.Interop.Outlook.DistListItem;
                            if (dist != null)
                            {
                                dist.Move(newContactsFolder);
                            }
                        }
                    }
                    catch (System.Exception ex)
                    {
                        EventLog.WriteEntry("Addin", ex.Message, EventLogEntryType.Error);
                    }
    
                    Thread.Sleep(100);
                }


    Any help would be appreciated, 

    I am planning to thread each contract move and manage this with a thread pool , however if you have any tips on speeding things up , please let me know?

    Tuesday, March 4, 2014 9:25 AM

All replies

  • Hello soledev,

    Did you try to debug the code? Were all Outlook items that meet your criteria moved to folder? May be the problem in the criteria? For example, some items may keep CustomerID empty. Is this the case?

    Anyway, I've noticed that you don't release underlying COM objects in the code instantly. This is particularly important if your add-in attempts to enumerate a lot of Outlook items in a collection that is stored on a Microsoft Exchange Server. Use System.Runtime.InteropServices.Marshal.ReleaseComObject to release an Outlook object when you have finished using it. Then set a variable to Nothing in Visual Basic (null in C#) to release the reference to the object. You can read more about this in the Systematically Releasing Objects article in MSDN.

    The Outlook object model is not thread safe and use Single Threaded Apartments (STA). So, you need to work with Outlook objects in the single thread only. That's why I'd recommend fetching the required data from Outlook objects and run secondary threads working only with that data, not Outlook objects.

    Finally, the Move method of the ContactItem returns an object that represents the item which has been moved to the designated folder. It should be released too.

    Tuesday, March 4, 2014 10:01 AM
  • I have found the problem, it was due to the fact that the action was triggered from the main application thread, I threaded this out which allowed the main Outlook process to continue as normal. This has resolved the issue.

    Tuesday, March 4, 2014 4:11 PM