locked
EWS using BindToItems() retrieves inconsistent Item RRS feed

  • Question

  • hello,

    i've a set of mailboxes from where i need to extract the items within a specific range of time. In order to speed up the process, and do not make a call per each Item, i'm using the BindToItems() method, with which with one EWS call you could retrieve more than one items. At the moment i've set the batch size to 250. With most of the emailboxes it works fine. but with some specific mailboxes i'm getting a

    Object reference not set to an instance of an object

    when i'm trying to do the following line:

    foreach (Attachment attachment in message.Attachments)


    always at some specific index of the Items collection, like 59 for email1, 124 email2, 12 email3 and etc

    Here the code i'm using:

    FindItemsResults<Item> findResults = service.FindItems(results.Folders.Single().Id, searchFilterCollection, view);
    ServiceResponseCollection<GetItemResponse> items = service.BindToItems(findResults.Select(r => r.Id), propertySet);
    foreach (GetItemResponse item in items){
       EmailMessage message = (EmailMessage)item.Item;
       foreach (Attachment attachment in message.Attachments){
            //do something
       } 
    }

    Here the properties selected:

    PropertySet propertySet = new PropertySet();
            propertySet.Add(ItemSchema.ConversationId);
            propertySet.Add(ItemSchema.Culture);
            propertySet.Add(ItemSchema.DateTimeCreated);
            propertySet.Add(ItemSchema.DateTimeReceived);
            propertySet.Add(ItemSchema.DateTimeSent);
            propertySet.Add(ItemSchema.EffectiveRights);
            propertySet.Add(ItemSchema.HasAttachments);
            propertySet.Add(ItemSchema.Id);
            propertySet.Add(ItemSchema.Importance);
            propertySet.Add(ItemSchema.InReplyTo);
            propertySet.Add(ItemSchema.IsAssociated);
            propertySet.Add(ItemSchema.IsDraft);
            propertySet.Add(ItemSchema.IsFromMe);
            propertySet.Add(ItemSchema.IsResend);
            propertySet.Add(ItemSchema.IsSubmitted);
            propertySet.Add(ItemSchema.IsUnmodified);
            propertySet.Add(ItemSchema.ItemClass);
            propertySet.Add(ItemSchema.LastModifiedName);
            propertySet.Add(ItemSchema.LastModifiedTime);
            propertySet.Add(ItemSchema.ParentFolderId);
            propertySet.Add(ItemSchema.Sensitivity);
            propertySet.Add(ItemSchema.Size);
            propertySet.Add(ItemSchema.Attachments);
            propertySet.Add(EmailMessageSchema.ConversationIndex);
            propertySet.Add(EmailMessageSchema.ConversationTopic);
            propertySet.Add(EmailMessageSchema.From);
            propertySet.Add(EmailMessageSchema.InternetMessageId);
            propertySet.Add(EmailMessageSchema.IsRead);
            propertySet.Add(EmailMessageSchema.References);
            propertySet.Add(EmailMessageSchema.Sender);
            propertySet.Add(EmailMessageSchema.CcRecipients);
            propertySet.Add(EmailMessageSchema.ToRecipients);
            propertySet.Add(EmailMessageSchema.ReceivedBy);
            propertySet.Add(EmailMessageSchema.ReceivedRepresenting);
            propertySet.Add(EmailMessageSchema.ReplyTo);
            propertySet.Add(EmailMessageSchema.IsResponseRequested);

    Adding this lines int he code, i was able to isolate the error.

    if (item.Result != ServiceResult.Success)
                {
                    Console.WriteLine("ERROR CODE: " + item.ErrorCode);
                    Console.WriteLine("ERROR DETAILS: " + item.ErrorDetails);
                    Console.WriteLine("ERROR MESSAGE: " + item.ErrorMessage);
                    Console.WriteLine("ERROR PROPERTIES: " + item.ErrorProperties);
                    Console.ReadKey();
                }

    this is the result:

    ERROR CODE: ErrorInternalServerTransientError
    ERROR DETAILS: System.Collections.Generic.Dictionary`2[System.String,System.String]
    ERROR MESSAGE: An internal server error occurred. Try again later.
    ERROR PROPERTIES: System.Collections.ObjectModel.Collection`1[Microsoft.Exchange.WebServices.Data.PropertyDefinitionBase]

    Thank you in advance for the help
    • Edited by aless239 Tuesday, September 24, 2019 11:18 AM
    Tuesday, September 24, 2019 11:17 AM