title: Exchange Web Services: Batching with FindItemsResult<Item>
body: <p>I'm writing an app to process email attachments, using Exchange Web Services.</p>
  
  <p>The general structure of my problem area is as follows:</p>
  
  <pre><code>public static void Main()
  {
      FindItemsResults&lt;Item&gt; findResults = FindItems();
  
      foreach (Item item in findResults)
      {
          DoSomethingWithItem(item);
      }
  }
  
  public static FindItemsResults&lt;Item&gt; FindItems()
  {
              FindItemsResults&lt;Item&gt; findResults;
  
              ItemView view = new ItemView(10);    //batching 10 at a time
              view.OrderBy.Add(ItemSchema.DateTimeReceived, SortDirection.Ascending);
              view.PropertySet = new PropertySet(
                  BasePropertySet.IdOnly,
                  ItemSchema.Subject,
                  ItemSchema.DateTimeReceived);
  
              findResults = service.FindItems(
                  WellKnownFolderName.Inbox,
                  new SearchFilter.SearchFilterCollection(
                      LogicalOperator.Or,
                      new SearchFilter.ContainsSubstring(ItemSchema.Subject, Properties.Settings.Default.EmailSubject)),
                  view);
  
              //return set of emails
              return findResults;
  }
  </code></pre>
  
  <p>At first, this looked OK - it processed my earlier test examples perfectly well. But when I start testing with bulk loads, I realised that it was only processing the first 10 items, since I was specifying a batch size of 10 items (<code>ItemView view = new ItemView(10</code>)), but I wasn't checking for further batches.</p>
  
  <p>I could have simply increased the batch size, but a quick google later, I found a better example:</p>
  
  <pre><code>public static FindItemsResults&lt;Item&gt; FindItems()
  {
              FindItemsResults&lt;Item&gt; findResults;
  
              ItemView view = new ItemView(10, 0, OffsetBasePoint.Beginning); 
              view.OrderBy.Add(ItemSchema.DateTimeReceived, SortDirection.Ascending);
              view.PropertySet = new PropertySet(
                  BasePropertySet.IdOnly,
                  ItemSchema.Subject,
                  ItemSchema.DateTimeReceived);
  
              do
              {
                  findResults = service.FindItems(
                      WellKnownFolderName.Inbox,
                      new SearchFilter.SearchFilterCollection(
                          LogicalOperator.Or,
                          new SearchFilter.ContainsSubstring(ItemSchema.Subject, Properties.Settings.Default.EmailSubject)),
                      view);
  
                  //any more batches?
                  if (findResults.NextPageOffset.HasValue)
                  {
                      view.Offset = findResults.NextPageOffset.Value;
                  }
              }
              while (findResults.MoreAvailable);
  
              return findResults;
  }
  </code></pre>
  
  <p>This loops through as many emails as I care to throw at it, but for reasons I can't yet understand, the foreach loop now only processes the first item in findResults.</p>
  
  <p>Even though findResults contains more than one item (<code>findResults.Items.Count</code> > 1), with my second example, <code>findResults.MoreAvailable</code> = false. Fair enough, I've looped through the batches earlier, so it makes sense that I'm looking at the end of the last batch. </p>
  
  <p>But how do I reset findResults so that it will process the whole lot? I tried setting findResults.MoreAvailable but it is readonly... What am I missing?</p>
  
  <p><strong>CONCLUSION:</strong></p>
  
  <p>OK, so I can either process the items one batch at a time, or I can add each item in each batch to a List and process them later, as I currently do.</p>
  
  <p>There is not a lot between them; I've initially started using a List, but I'll consider the choice further.</p>