gpt4 book ai didi

c# - 是否可以创建带有附件的日历事件?

转载 作者:太空宇宙 更新时间:2023-11-03 12:05:46 25 4
gpt4 key购买 nike

我想使用 Microsoft Graph 创建一个日历事件,这很有效,但不幸的是,我无法向该事件添加附件。事件已创建但没有附件。没有报错。

这是我的代码:

DateTimeTimeZone start = new DateTimeTimeZone
{
TimeZone = TimeZoneInfo.Local.Id,
DateTime = dateTimePicker1.Value.ToString("o"),
};

DateTimeTimeZone end = new DateTimeTimeZone
{
TimeZone = TimeZoneInfo.Local.Id,
DateTime = dateTimePicker2.Value.ToString("o"),
};

Location location = new Location
{
DisplayName = "Thuis",
};

byte[] contentBytes = System.IO.File
.ReadAllBytes(@"C:\test\sample.pdf");

var ev = new Event();

FileAttachment fa = new FileAttachment
{
ODataType = "#microsoft.graph.fileAttachment",
ContentBytes = contentBytes,
ContentType = "application/pdf",
Name = "sample.pdf",
IsInline = false,
Size = contentBytes.Length
};

ev.Attachments = new EventAttachmentsCollectionPage();
ev.Attachments.Add(fa);

ev.Start = start;
ev.End = end;
ev.IsAllDay = false;
ev.Location = location;
ev.Subject = textBox2.Text;

var response = await graphServiceClient
.Users["user@docned.nl"]
.Calendar
.Events
.Request()
.AddAsync(ev);

最佳答案

似乎仍然不支持在单个 请求中创建事件和附件 (a similar issue)

作为解决方法,可以先创建一个没有附件的事件,然后将附件添加到其中(需要向服务器发出两次请求),例如:

var ev = new Event
{
Start = start,
End = end,
IsAllDay = false,
Location = location,
Subject = subject
};

//1.create an event first
var evResp = await graphServiceClient.Users[userId].Calendar.Events.Request().AddAsync(ev);

byte[] contentBytes = System.IO.File.ReadAllBytes(localPath);
var attachmentName = System.IO.Path.GetFileName(localPath);
var fa = new FileAttachment
{
ODataType = "#microsoft.graph.fileAttachment",
ContentBytes = contentBytes,
ContentType = MimeMapping.GetMimeMapping(attachmentName),
Name = attachmentName,
IsInline = false
};

//2. add attachments to event
var faResp = await graphServiceClient.Users[userId].Calendar.Events[evResp.Id].Attachments.Request().AddAsync(fa);

关于c# - 是否可以创建带有附件的日历事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55075870/

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