EWS Service.FindItems() throws an exception when using SearchFilter.IsEqualTo

1.6k views Asked by At

Here is my code for obtaining some calendar items (appointments) from EWS. But this throws an exception all the time.

The Exception:- The property can not be used with this type of restriction.

        private void GetChangedAppointmentInformation(Appointment appointment)
        {
            try
            {
                // Save appointment details into local variables
                id = appointment.Id.ToString();
                body = appointment.Body;
                duration = appointment.Duration;
                end = appointment.End;
                bookingKey = appointment.Subject;
                subject = appointment.Subject;
                location = appointment.Location;


                ItemView view = new ItemView(1000);

                // Create a search filter that filters email based on the existence of the extended property.
                SearchFilter eq = new SearchFilter.IsEqualTo(AppointmentSchema.ICalUid, appointment.ICalUid);

                // Search the Calendar with the defined view and search filter. This results in a FindItem operation call to EWS.
                FindItemsResults<Item> findResults = service.FindItems(WellKnownFolderName.Calendar, eq, view);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error: " + ex.Message);
            }
        }

Can you please advise me on this? I tried MSDN and several other online resources, I'm still trying to figure that out.

1

There are 1 answers

0
Glen Scales On

The error is telling you that the strongly typed property you're trying to use can't be used in a restriction. The best workaround for this is to use the equivalent Extended Property instead eg to search based on a existing appointment something like

    Appointment newAppointment = new Appointment(service);
    newAppointment.Subject = "Test Subject";        
    newAppointment.Start = new DateTime(2012, 03, 27, 17, 00, 0);
    newAppointment.StartTimeZone = TimeZoneInfo.Local;
    newAppointment.EndTimeZone = TimeZoneInfo.Local;
    newAppointment.End = newAppointment.Start.AddMinutes(30);
    newAppointment.Save();
    newAppointment.Body = new MessageBody(Microsoft.Exchange.WebServices.Data.BodyType.Text, "test");
    newAppointment.RequiredAttendees.Add("[email protected]");
    newAppointment.Update(ConflictResolutionMode.AlwaysOverwrite ,SendInvitationsOrCancellationsMode.SendOnlyToAll);
    ExtendedPropertyDefinition CleanGlobalObjectId = new ExtendedPropertyDefinition(DefaultExtendedPropertySet.Meeting, 0x23, MapiPropertyType.Binary);
    PropertySet psPropSet = new PropertySet(BasePropertySet.FirstClassProperties);
    psPropSet.Add(CleanGlobalObjectId);
    newAppointment.Load(psPropSet);
    object CalIdVal = null;
    newAppointment.TryGetProperty(CleanGlobalObjectId, out CalIdVal);
    Folder AtndCalendar = Folder.Bind(service, new FolderId(WellKnownFolderName.Calendar,"[email protected]"));
    SearchFilter sfSearchFilter = new SearchFilter.IsEqualTo(CleanGlobalObjectId, Convert.ToBase64String((Byte[])CalIdVal));
    ItemView ivItemView = new ItemView(1);
    FindItemsResults<Item> fiResults = AtndCalendar.FindItems(sfSearchFilter, ivItemView);
    if (fiResults.Items.Count > 0) {
        //do whatever
    }

Should work okay

Cheers Glen