gpt4 book ai didi

c# - 使用 Microsoft Graph 从日历中删除日期范围内的许多事件和某个扩展属性值

转载 作者:行者123 更新时间:2023-11-30 23:09:06 30 4
gpt4 key购买 nike

我知道如何使用 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);
}
}

我现在想知道的是如何删除某个日期范围内具有TruckleSoft1扩展属性的所有事件值为 CLM_MidweekMeeting

我对大多数在线文档感到困惑,因为它以一种我在编码中不使用的格式呈现,而且我还不能很好地研究如何将其转化为我的需求。

ASPNET Snippets中,它提供了一个删除特定事件的例子:

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 会指定日期范围,但可以计算出其中的内容。我知道我们在扩展属性方面使用 Expand我走在正确的轨道上吗?

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

更新2

好的,我查看了答案中提供的资源和这个 additional resource所以我得到了这段代码:

// 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();
}
}
}

但我得到一个异常(exception):

Code: ErrorInvalidProperty Message: PropertyId values may only be in one of the following formats: 'MapiPropertyType namespaceGuid Name propertyName', 'MapiPropertyType namespaceGuid Id propertyId' or '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();
}
}

但我得到一个异常(exception):

Code: ErrorInvalidUrlQueryFilter Message: The query filter contains one or more invalid nodes.

所以 strExpand 是:

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

怎么了?

最佳答案

删除仅适用于单个事件,您不能对集合执行删除。

使用 batching可能是一个可行的解决方案,但请记住,这仍处于预览阶段并且 not supported by the .NET SDK然而。为了使用它,您需要自己构造和提交 HTTP 请求(并处理响应)。

由原发布者更新

此附加资源很有用:Get singleValueLegacyExtendedProperty

如果您只对 id 感兴趣,那么您可以使用 Expand。但是,如果您对 id value 都感兴趣,那么您实际上使用了过滤器>!

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();
}
}
}

关于c# - 使用 Microsoft Graph 从日历中删除日期范围内的许多事件和某个扩展属性值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46017008/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com