0

I am trying to upgrade en existing application that reads Exchange 2003 using WebDAV. The mail server is to be upgraded to Exchange 2013, so I am checking how I can use EWS.

I have a problem in that although I know the inbox has unread items with attachments, the query I am running against the FindItems object is returning empty...

Here is my code snippet:

private static void GetAttachments(ExchangeService service)
{
    // Return a single item.
    ItemView view = new ItemView(100);
    ServicePointManager.ServerCertificateValidationCallback = CertificateValidationCallBack;
    ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010_SP2);// .Exchange2007_SP1);

    service.UseDefaultCredentials = true;
    service.AutodiscoverUrl("bbtest@bocuk.local", RedirectionUrlValidationCallback);

    ItemView view = new ItemView(1);

    string querystring = "HasAttachments:true Subject:'ATTACHMENT TEST' Kind:email";

    // Find the first email message in the Inbox that has attachments. 
    // This results in a FindItem operation call to EWS.
    FindItemsResults<Item> results = service.FindItems(WellKnownFolderName.Inbox, querystring, view);

    //FindItemsResults<Item> results = service.FindItems(WellKnownFolderName.Inbox, new ItemView(50));

    if (results.TotalCount > 0)
        {
            // looping through all the emails
            for (Int16 iDx = 0; iDx < results.TotalCount-1; iDx++)
            {

                EmailMessage email = results.Items[iDx] as EmailMessage;

                if (email.IsRead == false) { 

                    // Request all the attachments on the email message. This results in a GetItem operation call to EWS.
                    email.Load(new PropertySet(EmailMessageSchema.Attachments));

                    foreach (Attachment attachment in email.Attachments)
                    {
                        if (attachment is FileAttachment)
                        {
                            FileAttachment fileAttachment = attachment as FileAttachment;

What I am supposed to be doing is reading all the unread emails in the target inbox (only one Exchange server) and taking the attachments on disk so I can then add them as attachments as new cases on SalesForce.

Where am I going wrong?

Also, this line:

ItemView view = new ItemView(100);

was:

ItemView view = new ItemView(1);

Surely that will only look for one email item, right?

Our Man in Bananas
  • 5,809
  • 21
  • 91
  • 148

1 Answers1

1

I submitted the following XML and got the expected results. The easiest way to figure out what is going on is to look at the XML.

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
               xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types">
 <soap:Header>
    <t:RequestServerVersion Version="Exchange2010_SP2"/>
  </soap:Header>
<soap:Body>
    <FindItem xmlns="http://schemas.microsoft.com/exchange/services/2006/messages"
               xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types"
              Traversal="Shallow">
      <ItemShape>
        <t:BaseShape>IdOnly</t:BaseShape>
      </ItemShape>
      <ParentFolderIds>
        <t:DistinguishedFolderId Id="inbox"/>
     </ParentFolderIds>
      <QueryString>HasAttachments:true Subject:'ATTACHMENT TEST' Kind:email</QueryString>
    </FindItem>
  </soap:Body>
</soap:Envelope>
Michael Mainer
  • 3,387
  • 1
  • 13
  • 32
  • @Micael Mainer - Microsoft: can I ask how you submitted the xml? – Our Man in Bananas Mar 12 '14 at 17:21
  • 1
    You can use Fiddler, WSFetch, or any number of Web request tools to submit the request. Oh wait, your using the EWS Managed API so you can turn tracing on the ExchangeService object. http://msdn.microsoft.com/en-us/library/dn495632(v=exchg.150).aspx – Michael Mainer Mar 14 '14 at 19:23
  • thanks, any thouhts on [SO: searchFilter not working properly with EWS FindItems method call](http://stackoverflow.com/questions/22559704/searchfilter-not-working-properly-with-ews-finditems-method-call) ? – Our Man in Bananas Mar 21 '14 at 15:10