使用 Microsoft Graph 筛选两个扩展属性
Filtering on two extended properties with Microsoft Graph
我的一些日历活动有两个扩展属性:
// 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
});
}
在其他代码中我想过滤这些事件:
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}')";
那么如何筛选具有上述扩展属性两者的事件?
这个 的答案真的很有帮助。我把事情复杂化了!
所以:
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;
}
关键是简单地使用 and
并将两个单独的过滤器拼接在一起。
我的一些日历活动有两个扩展属性:
// 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
});
}
在其他代码中我想过滤这些事件:
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}')";
那么如何筛选具有上述扩展属性两者的事件?
这个
所以:
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;
}
关键是简单地使用 and
并将两个单独的过滤器拼接在一起。