Exchange Webservice Managed API - Find items by extended properties

12,762

here's a samplecode how to create an appointment with the customid and find it after saving:

ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2007_SP1);
        service.AutodiscoverUrl("[email protected]");

        ExtendedPropertyDefinition def = new ExtendedPropertyDefinition(DefaultExtendedPropertySet.PublicStrings, "AppointmenId", MapiPropertyType.String);

        Guid testid = Guid.NewGuid ();

        Appointment appointment = new Appointment(service);
        appointment.Subject = "Test";
        appointment.Start = DateTime.Now.AddHours(1);
        appointment.End = DateTime.Now.AddHours(2);
        appointment.SetExtendedProperty(def, testid.ToString());
        appointment.Save(WellKnownFolderName.Calendar);

        SearchFilter filter = new SearchFilter.IsEqualTo(def, testid.ToString());

        FindItemsResults<Item> fir = service.FindItems(WellKnownFolderName.Calendar, filter, new ItemView(10));

hope this helps you...

Share:
12,762
Jacob
Author by

Jacob

Updated on June 04, 2022

Comments

  • Jacob
    Jacob almost 2 years

    I have tried to use extended properties on appointments with EWS, but I can not seem to find the appointments again. The set property part is equal to the one shown in this question:

    How to Update an Appointment from Exchange Web Service Managed API 2.0 in ASP.NET

    When I try to retrieve the appointment, I have followed these examples:

    http://msdn.microsoft.com/en-us/uc14trainingcourse_5l_topic3#_Toc254008129 http://msdn.microsoft.com/en-us/library/exchange/dd633697(v=exchg.80).aspx

    But I never get any appointments returned when I make a lookup.

    Here is the code for the lookup:

            ItemView view = new ItemView(10);
    
            // Get the GUID for the property set.
            Guid MyPropertySetId = new Guid("{" + cGuid + "}");
    
            // Create a definition for the extended property.
            ExtendedPropertyDefinition extendedPropertyDefinition =
              new ExtendedPropertyDefinition(MyPropertySetId, "AppointmentID", MapiPropertyType.String);
    
            view.PropertySet =
             new PropertySet(
                   BasePropertySet.IdOnly,
                   ItemSchema.Subject,
                   AppointmentSchema.Start,
                   AppointmentSchema.End, extendedPropertyDefinition);
    
            SearchFilter filter = new SearchFilter.Exists(extendedPropertyDefinition);
    
            FindItemsResults<Item> findResults = service.FindItems(WellKnownFolderName.Inbox, filter,
                view);
    

    Any help is greatly appreciated.

    Edit: When I try to create the property like the documentation shows:

    http://msdn.microsoft.com/en-us/library/exchange/dd633654(v=exchg.80).aspx

    It fails because its a Guid im adding as property value. :-/

    Edit again: Just tried getting all appointments for today, and getting the property from the appointment I just created, and it says the same as I stored, without the {}, so it must be somthing with the filter.

    Edit once again* It has somthing to do with

     ExtendedPropertyDefinition extendedProperty = new ExtendedPropertyDefinition(
    

    if I use:

     new ExtendedPropertyDefinition(
                    DefaultExtendedPropertySet.Appointment,
                    "AppointmentID",
                    MapiPropertyType.String);
    

    It finds all the appointments with properties, but if I search for a specific one:

     Guid MyPropertySetId = new Guid("{" + cGuid + "}");
    
     ExtendedPropertyDefinition extendedProperty =
                new ExtendedPropertyDefinition(
                    MyPropertySetId,
                    "AppointmentID",
                    MapiPropertyType.String);
    

    Then nothing is found.

  • Jacob
    Jacob about 11 years
    Yes, I was searching in the wrong folder, but it still finds nothing :-/
  • Jürgen Hoffmann
    Jürgen Hoffmann about 11 years
    hmm... maybe it's because of the way how your create your propertydefinition. try this code: ExtendedPropertyDefinition propdef = new ExtendedPropertyDefinition(Microsoft.Exchange.WebServices.Da‌​ta.DefaultExtendedPr‌​opertySet.PublicStri‌​ngs, name, MapiPropertyType.String); for adding and searching. I had some Problems using the Guid-way too. This one is easier and works perferct in my code.
  • Jacob
    Jacob about 11 years
    This is the code I am using for setting the Guid. The second part retrieves the guid from an appointment. My problem is I need to find the specific appointment when only having the Guid.
  • Ahmad ElMadi
    Ahmad ElMadi about 11 years
    well, when you find the appointments from certain dates to others, then you apply an if case searching among them using the Guid which you extract if it matches or not.
  • Jürgen Hoffmann
    Jürgen Hoffmann about 11 years
    your welcome... maybe next time you can help me ;) can you please set the "was helpfull" ;)
  • Stephan
    Stephan about 8 years
    According to stackoverflow.com/questions/25187481/… you should always use your own property set id, to avoid collision with properties set by other vendors. Still love your quick solution!