Search code examples
exchangewebservicesews-managed-api

EWS Managed - Is it possible to get full "From" details using "FindItems"?


I'm using the Exchange Web Services Managed API to search a message folder using FindItems. The code I'm using is this:

var search = new SearchFilter.ContainsSubstring(
    ItemSchema.Subject,
    "subject I want");

ItemView searchView = new ItemView(9999);
searchView.PropertySet = new PropertySet(
   BasePropertySet.IdOnly,
   ItemSchema.Subject,
   ItemSchema.DateTimeReceived,
   EmailMessageSchema.From);
searchView.OrderBy.Add(
   ItemSchema.DateTimeReceived,
   SortDirection.Descending);
searchView.Traversal = ItemTraversal.Shallow;

var searchResults = _service.FindItems(
   folderToSearch.Id,
   search,
   searchView);

The search works fine, and the properties I've specified in the searchView.PropertySet do get returned. The problem is that it doesn't return all of the details of From.

I iterate searchResults, and cast the items to type EmailMessage or PostItem as appropriate, to access the From property, which returns an EmailAddress object. On that object, the Name property is set, but Address is null.

If I then bind the item, like:

var boundItem = Item.Bind(_service, message.Id);
var boundItemEmail = boundItem as EmailMessage;

Then boundItemEmail.From.Address is not null, it returns the senders email address.

The trouble is, binding a message can be a rather time-consuming process, compared to the much faster FindItems operation.


Solution

  • You should be able to use the LoadPropertiesForItems method to get the From property, it's faster than binding to individual messages.