Using SearchFilter set to ConstainsSubstring causes exception

I am developing an Outlook add-in that communicates with EWS (in this case Office 365) and am trying to delete appointments by category. Each appointment has two categories. I am using SearchFilter to get a FindItemResults collection which I then use to delete appointments.

When the SearchFilter is set to IsEqualTo everything works but items are only found if the category named in the search filer comes first of the two. Setting the SearchFilter to ContainsSubstring ought to fix this but EWS returns an exception on the last line of the code block below.

searchFilterCollection.Add(new SearchFilter.IsEqualTo(ItemSchema.Categories, category.Trim()));
searchFilter = new SearchFilter.SearchFilterCollection(LogicalOperator.And, searchFilterCollection.ToArray());

//Find items to delete
ItemView view = new ItemView(500);
view.PropertySet = new PropertySet(BasePropertySet.IdOnly, AppointmentSchema.Subject, AppointmentSchema.Start);
findResults = exchangeService.FindItems(WellKnownFolderName.Calendar, searchFilter, view);

The error details are:

Error at InternalThrowIfNecessary : Microsoft.Exchange.WebServices.Data.ServiceResponseException An internal server error occurred. The operation failed. <o:p></o:p>

Message: An internal server error occurred. The operation failed.
Site: InternalThrowIfNecessary
Stack Trace:    at Microsoft.Exchange.WebServices.Data.ServiceResponse.InternalThrowIfNecessary()
   at Microsoft.Exchange.WebServices.Data.ServiceResponse.ThrowIfNecessary()
   at Microsoft.Exchange.WebServices.Data.MultiResponseServiceRequest`1.Execute()
   at Microsoft.Exchange.WebServices.Data.ExchangeService.FindItems[TItem](IEnumerable`1 parentFolderIds, SearchFilter searchFilter, String queryString, ViewBase view, Grouping groupBy, ServiceErrorHandling errorHandlingMode)<o:p></o:p>

   at Microsoft.Exchange.WebServices.Data.ExchangeService.FindItems(FolderId parentFolderId, SearchFilter searchFilter, ItemView view)<o:p></o:p>

   at Microsoft.Exchange.WebServices.Data.ExchangeService.FindItems(WellKnownFolderName parentFolderName, SearchFilter searchFilter, ItemView view)<o:p></o:p>

   at GreenhillSoftware.ACalWinFormLib.Logic.ExchangeServiceManager.DeleteAppointmentsInExchange(String category, DateTime start, DateTime end, Form frm, String staffCode) in c:\VS Projects\Outlook Add-ins\ACal4\ACalWinFormLib\Logic\clsExchangeServiceManager.cs:line 864<o:p></o:p>

May 13th, 2015 7:42am

The categories property is a multivalued string so a Substring won't work on these type of properties. The Categories property is indexed so the best way to handle this is to use an AQS query string eg

     ItemView iv = new ItemView(1000);
      String AQS = "System.Category:red OR System.Category:green";
      FindItemsResults<Item> fiItems = null;
      do
      {
        fiItems = service.FindItems(WellKnownFolderName.Calendar, AQS, iv);
        foreach (Item itItem in fiItems.Items) {
          Console.WriteLine(itItem.Subject);
        }
        iv.Offset += fiItems.Items.Count;
      } while (fiItems.MoreAvailable == true);

Cheers
Glen

Free Windows Admin Tool Kit Click here and download it now
May 13th, 2015 10:41pm

That's great. Thanks very much Glen. It works nicely.

I have one problem. The code is for an Outlook add-in that contacts Exchange. An increasing number of users will be on Office 365 (which is what I test on) but I have to allow for some users still being on Exchange 2007 and I see this method only works for Ex 2010 and better.

I have now written a simple method that simply cycles through all the appointments in a user's calendar and checks the categories and deletes the appointment if it is a match without using a filter. This will work on all versions. I presume this way of doing things is slower.

So I have two questions

1. Am I right in presuming that the filtered method (AQS) is to be preferred because of performance?

2. Is there a way to tell which version of Exchange the service has connected to?

I am thinking if I could detect the Exchange version I could then select the appropriate Delete method.

Thanks

May 14th, 2015 5:24am

1. Yes and also on that particular property because its a multi valued string its generally the only method that will work correctly.

2. After you have made a few successful requests the ServerInfo property in the ExchangeService class will return the version on the Server https://msdn.microsoft.com/en-us/library/microsoft.exchange.webservices.data.exchangeservicebase.serverinfo(v=exchg.80).aspx

Cheers
Glen

Free Windows Admin Tool Kit Click here and download it now
May 15th, 2015 1:41am

Got it. Thanks for your help.
May 15th, 2015 3:39am

1. Yes and also on that particular property because its a multi valued string its generally the only method that will work correctly.

2. After you have made a few successful requests the ServerInfo property in the ExchangeService class will return the version on the Server https://msdn.microsoft.com/en-us/library/microsoft.exchange.webservices.data.exchangeservicebase.serverinfo(v=exchg.80).aspx

Cheers
Glen

  • Marked as answer by Mike VE 23 hours 51 minutes ago
Free Windows Admin Tool Kit Click here and download it now
May 15th, 2015 5:38am

1. Yes and also on that particular property because its a multi valued string its generally the only method that will work correctly.

2. After you have made a few successful requests the ServerInfo property in the ExchangeService class will return the version on the Server https://msdn.microsoft.com/en-us/library/microsoft.exchange.webservices.data.exchangeservicebase.serverinfo(v=exchg.80).aspx

Cheers
Glen

  • Marked as answer by Mike VE Friday, May 15, 2015 7:37 AM
May 15th, 2015 5:38am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics