Question

I need to search for a substring in a user's Calendar appointments. I don't have any other information about the appointment (GUID, Start Date, etc.). I just know that a particular substring is in the body.

I've read a couple articles on how to get the body of an appointment, but they search by the GUID or the subject. I'm trying to use the code below to search for a substring in the body, but I get an error that I can't use the Body in FindItems.

Is there a way to do this? Assuming there's no way for me to get any other info from the appointment, is there another approach I can take?

        //Variables
        ItemView view = new ItemView(10);
        view.PropertySet = new PropertySet(EmailMessageSchema.Body);

        SearchFilter sfSearchFilter;
        FindItemsResults<Item> findResults;

        foreach (string s in substrings)
        {
            //Search for messages with body containing our permURL
            sfSearchFilter = new SearchFilter.ContainsSubstring(EmailMessageSchema.Body, s);
            findResults = service.FindItems(WellKnownFolderName.Calendar, sfSearchFilter, view);

            if (findResults.TotalCount != 0)
            {
                Item appointment = findResults.FirstOrDefault();
                appointment.SetExtendedProperty(extendedPropertyDefinition, s);
             }
Was it helpful?

Solution

So it turns out you are able to search the body, but you can't return the body with FindItems. You have to load it later if you want to use it. So instead of setting my property set to the body, I set it to IdOnly and then set the SearchFilter to traverse the body of the ItemSchema.

        //Return one result--there should only be one in this case
        ItemView view = new ItemView(1);
        view.PropertySet = new PropertySet(BasePropertySet.IdOnly);

        //variables
        SearchFilter sfSearchFilter;
        FindItemsResults<Item> findResults;

        //for each string in list
        foreach (string s in permURLs)
        {
            //Search ItemSchema.Body for the string
            sfSearchFilter = new SearchFilter.ContainsSubstring(ItemSchema.Body, s);
            findResults = service.FindItems(WellKnownFolderName.Calendar, sfSearchFilter, view);

            if (findResults.TotalCount != 0)
            {
                Item appointment = findResults.FirstOrDefault();
                appointment.SetExtendedProperty(extendedPropertyDefinition, s);
                ...
                appointment.Load(new PropertySet(ItemSchema.Body));
                string strBody = appointment.Body.Text;
            }
         }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top