microsoft-graph-apiextended-properties

Filtering on two extended properties with Microsoft Graph


Some of my calendar events have two extended properties:

// Extended Properties
var extendedProperties = new EventSingleValueExtendedPropertiesCollectionPage();
extendedProperties.Add(new SingleValueLegacyExtendedProperty
{
    Id = _Property_TruckleSoft1,
    Value = oSettings.CalendarEntryType
});

if(!string.IsNullOrEmpty(oSettings.ScheduleType))
{
    extendedProperties.Add(new SingleValueLegacyExtendedProperty
    {
        Id = _Property_TruckleSoft2,
        Value = oSettings.ScheduleType
    });
}

In other code I want to filter for these events:

string strFilterProperty = $"singleValueExtendedProperties/Any(ep: ep/id eq '{eventTypeTag}' and ep/value eq '{oData.Settings.CalendarEntryType}')";

string strFilterProperty = $"singleValueExtendedProperties/Any(ep: ep/id eq '{scheduleTypeTag}' and ep/value eq '{oData.Settings.ScheduleType}')";

So how do I filter for events that have both of the above extended properties?


Solution

  • This answer to this question really helped. I was over complicating things!

    So:

    public async Task<bool> DeleteExistingEvents(string idCalendar, DateTime dateStart, DateTime dateEnd, string typeEvent, string typeSchedule = "")
    {
        try
        {
            // We only want events within the desired date range
            string strFilterDateRange = String.Format("start/dateTime ge '{0}T00:00' and end/dateTime le '{1}T23:59'",
                dateStart.ToString("yyyy-MM-dd"),
                dateEnd.ToString("yyyy-MM-dd"));
    
            // We only want events of the right type
            string strFilterProperty = $"singleValueExtendedProperties/Any(ep: ep/id eq '{_Property_TruckleSoft1}' and ep/value eq '{typeEvent}')";
    
            string strFilter = strFilterDateRange + " and " + strFilterProperty;
    
            if(typeSchedule != "")
            {
                strFilterProperty = $"singleValueExtendedProperties/Any(ep: ep/id eq '{_Property_TruckleSoft2}' and ep/value eq '{typeSchedule}')";
                strFilter += " and " + strFilterProperty;
            }
    
            // Select the events (if any) and delete them
            var oEvents = await _graphClient
                                    .Me
                                    .Calendars[idCalendar]
                                    .Events
                                    .Request()
                                    .Filter(strFilter)
                                    .GetAsync();
            if (oEvents?.Count > 0)
            {
                foreach (Event oEvent in oEvents)
                {
                    // Delete the event (Do I need to use the specific calendar events list?)
                    await _graphClient.Me.Events[oEvent.Id].Request().DeleteAsync();
                }
            }
        }
        catch (Exception ex)
        {
            SimpleLog.Log(ex);
            return false;
        }
    
        return true;
    }
    

    The key was to simply use and and stitch the two separate filters together.