none
Find contacts code for Exchange Server has stopped working RRS feed

  • Question

  • Hi all,

     

    I am using the following code to find the all contacts in the exchange server. The code was working fine at the beginning and I managed to retreived contacts using the code. But, for some reasons it has stopped working and I’m unable to find any contact now. Can someone figure out what went wrong?

     

     

       public static void FindMyContacts(ExchangeServiceBinding binding)

            {

     

                // Create the request message

     

                FindItemType request = new FindItemType();

     

     

     

                // Set up the shape. Think of this as the "select" part of a SQL query. It determines

     

                // which props will be returned

     

                //

     

                request.ItemShape = new ItemResponseShapeType();

     

                request.ItemShape.BaseShape = DefaultShapeNamesType.AllProperties;

     

                // Tell EWS which folder you want to look at

     

                //

     

                DistinguishedFolderIdType contactFolder = new DistinguishedFolderIdType();

     

                contactFolder.Id = DistinguishedFolderIdNameType.contacts;

     

                request.ParentFolderIds = new BaseFolderIdType[] { contactFolder };

     

     

     

                // Shallow traversal

     

                //

     

                request.Traversal = ItemQueryTraversalType.Shallow;

     

                // Make the actual call

     

                //

     

                FindItemResponseType response = binding.FindItem(request);

     

                // since we only looked in a single folder, there will only be one response message

     

                //

     

                FindItemResponseMessageType responseMessage =

     

                        response.ResponseMessages.Items[0] as FindItemResponseMessageType;

     

                if (responseMessage.ResponseCode != ResponseCodeType.NoError)

                {

     

                    throw new Exception("FindItem failed!");

     

                }

     

                else

                {

     

                    // Grab the resulting items and enumerate them

     

                    //

     

                    ItemType[] items = (responseMessage.RootFolder.Item as ArrayOfRealItemsType).Items;

     

                    foreach (ItemType item in items)

                    {

     

                        // A contacts folder will typically have contacts and DLs

     

                        //

     

                        ContactItemType contact = item as ContactItemType;

     

                        if (contact != null)

                        {

     

                            Console.WriteLine("Contact: " + ((contact.FileAs == null) ?

     

                                    "<NULL>" : contact.FileAs));

     

                        }

     

                        else

                        {

     

                            DistributionListType dl = item as DistributionListType;

     

                            Console.WriteLine("Distribution List: " +

     

                                ((dl.DisplayName == null) ? "<NULL>" : dl.DisplayName));

     

                        }

                        Console.ReadKey();

     

                    }

     

                }

     

     

     

            }

     

     

     

    Friday, February 18, 2011 10:38 AM