使用Microsoft Graph删除日历内的许多事件,并使用Microsoft Graph删除某些扩展的属性值



我知道如何使用 Microsoft Graph 在特定日历中显示所有事件:

var oEvents = await _graphClient
                    .Me
                    .Calendars[oCalendar.Id]
                    .Events
                    .Request()
                    .GetAsync();
if(oEvents?.Count > 0)
{
    foreach(Event oEvent in oEvents)
    {
        string strEventInfo = $"Subject: {oEvent.Subject}" + Environment.NewLine;
        //strEventInfo += $"Body: {oEvent.BodyPreview}" + Environment.NewLine;
        strEventInfo += $"Times: Start - {oEvent.Start.DateTime} End - {oEvent.End.DateTime}" + Environment.NewLine;
        //strEventInfo += $"Location: {oEvent.Location.DisplayName}" + Environment.NewLine;
        strEventInfo += $"All Day: {oEvent.IsAllDay}" + Environment.NewLine;
        Console.WriteLine(strEventInfo);
    }
}

我现在要找出的是如何 delete 在某个日期范围内的所有事件具有 trucklesoft1 扩展属性具有 clm_midweekmeeting的值

我与大多数在线文档感到困惑,因为它是以我在编码中不使用的格式呈现的,但我还不能很好地锻炼身体。

ASPNET摘要中

public async Task<List<ResultsItem>> DeleteEvent(GraphServiceClient graphClient, string id)
{
    List<ResultsItem> items = new List<ResultsItem>();
    // Delete the event.
    await graphClient.Me.Events[id].Request().DeleteAsync();
    items.Add(new ResultsItem
    {
        // This operation doesn't return anything.
        Properties = new Dictionary<string, object>
        {
            { Resource.No_Return_Data, "" }
        }
    });
    return items;
}

我看到了重要的行:

await graphClient.Me.Events[id].Request().DeleteAsync();

,但我知道有一种方法可以简单地做我需要的事情。我在 DateTime对象(仅日期)中有两个日期(启动/结束)。

正确的方法是什么?

谢谢。

更新

我认为我应该使用以下内容来删除所需的事件:

var oEvents = await _graphClient
                    .Me
                    .Calendars[oCalendar.Id]
                    .Events
                    .Filter(" ?????? ")
                    .Expand(" ????? ")
                    .Request()
                    .GetAsync();

我了解 filter 将指定日期范围,但可以解决其中的内容。而且我知道我们使用扩展与扩展属性有关。我在正确的轨道上吗?

假设我是,这是否意味着我们必须迭代任何返回列表并逐一删除每个事件?听起来批次的想法,但这是另一个问题,因为我也很难获得批处理的示例。

更新2

好吧,我查看了答案中提供的资源和此附加资源,因此我得到了此代码:

// The first thing we need to do is delete the existing events
if(oData.Settings.CalendarEntryType == "CLM_MidweekMeeting")
{
    // We only want events within the desired date range
    string strFilter = String.Format(
        "start/dateTime ge '{0}T00:00' and end/dateTime le '{1}T23:59'",
        oData.Settings.StartDate.ToString("yyyy-MM-dd"),
        oData.Settings.EndDate.ToString("yyyy-MM-dd"));
    // We only want events of the right type
    string strExpand = "singleValueExtendedProperties($filter=id eq '{TruckleSoft1}' and value eq '{CLM_MidweekMeeting}')";

    // Select the events (if any) and delete them
    var oEvents = await _graphClient
                         .Me
                         .Calendars[oData.Settings.CalendarID]
                         .Events
                         .Request()
                         .Filter(strFilter)
                         .Expand(strExpand)
                         .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();
        }
    }
}

但是我有一个例外:

代码:errorInvalidProperty消息:PropertyId值只能以以下格式之一:'mapipropertypee namespaceguid name propertyname','mapipropertytype namespaceguid ID PropertyId'或" mapipropertytype propertyTag"。

这对我来说意味着当我遵循示例代码并做到时:

// Extended Properties
var extendedProperties = new EventSingleValueExtendedPropertiesCollectionPage();
extendedProperties.Add(new SingleValueLegacyExtendedProperty
{
    Id = String.Format("String {{{0}}} Name {1}", Guid.NewGuid().ToString(), "TruckleSoft1"),
    Value = oData.Settings.CalendarEntryType
});

使用这样的Guid.NewGuid()不是一个好主意,因为它总是会改变的。在我看来,我必须使用发电机手动创建GUID并构建静态字符串。然后,在删除事件并创建事件时,我会使用相同的字符串。正确?

更新3

我更改了我的代码:

private static readonly string _PropertyCLM_MidweekMeeting = "String {~~~~} Name TruckleSoft1";

然后,我调整了查询代码:

// We only want events within the desired date range
string strFilter = String.Format(
    "start/dateTime ge '{0}T00:00' and end/dateTime le '{1}T23:59'",
    oData.Settings.StartDate.ToString("yyyy-MM-dd"),
    oData.Settings.EndDate.ToString("yyyy-MM-dd"));
// We only want events of the right type
string strExpand = $"singleValueExtendedProperties($filter=id eq '{_PropertyCLM_MidweekMeeting}' and value eq 'CLM_MidweekMeeting')";

// Select the events (if any) and delete them
var oEvents = await _graphClient
                     .Me
                     .Calendars[oData.Settings.CalendarID]
                     .Events
                     .Request()
                     .Filter(strFilter)
                     .Expand(strExpand)
                     .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();
    }
}

但是我有一个例外:

代码:错误InvalidurlqueryFilter 消息:查询过滤器包含一个或多个无效的节点。

所以strExpand是:

singleValueExtendedProperties($filter=id eq 'String {~~~~} Name TruckleSoft1' and value eq 'CLM_MidweekMeeting')

怎么了?

删除仅针对单个event,您不能针对集合执行删除。

使用批处理可能是一个可行的解决方案,但请记住,这仍在预览中,并且不受.NET SDK的支持。为了使用它,您需要自己构建和提交HTTP请求(并处理响应)。

由原始海报

更新

此附加资源很有用:获取SingleValuelegacyExtendedProperty

如果您仅对 ID 感兴趣,则使用 expand 。但是,如果您对都感兴趣 ID value ,那么您实际上实际上使用 filter

GET /me/events?$filter=singleValueExtendedProperties/Any(ep: ep/id eq '{id_value}' and ep/value eq '{property_value}')

GUID 在应用程序中也必须是静态的(如更新的问题所述):

private static readonly string _PropertyCLM_MidweekMeeting = "String {~~~~} Name TruckleSoft1";

因此,要在日期范围和扩展属性ID上进行过滤,并且值代码为:

// The first thing we need to do is delete the existing events
if(oData.Settings.CalendarEntryType == "CLM_MidweekMeeting")
{
    // 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'",
        oData.Settings.StartDate.ToString("yyyy-MM-dd"),
        oData.Settings.EndDate.ToString("yyyy-MM-dd"));
    // We only want events of the right type
    string strFilterProperty = $"singleValueExtendedProperties/Any(ep: ep/id eq '{_PropertyCLM_MidweekMeeting}' and ep/value eq 'CLM_MidweekMeeting')";
    string strFilter = strFilterDateRange + " and " + strFilterProperty;
    // Select the events (if any) and delete them
    var oEvents = await _graphClient
                         .Me
                         .Calendars[oData.Settings.CalendarID]
                         .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();
        }
    }
}

最新更新