Можно ли создавать события календаря с вложениями?

Я хочу создать событие календаря с помощью 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);

3 ответа

Решение

Похоже, что все еще не поддерживается создание события вместе с вложениями в одном запросе ( аналогичная проблема)

В качестве обходного пути, сначала можно создать событие без вложений, а затем добавить в него вложения (требуется два запроса к серверу), например:

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

Я обнаружил, что если вы создадите мероприятие без участников, а затем создадите вложение и обновите событие с помощью участников, они получат электронное письмо о мероприятии со всеми вложениями.

Я использую HTTP, не должно быть проблем, чтобы изменить его на GRAPH SDK.

Это мой код:

var eventContainer = new EventContainer();
eventContainer.Init(); //populate the event with everything except the attendees and attachments        

//Send POST request and get the updated event obj back
eventContainer = GraphUtil.CreateEvent(eventContainer);

//Create a basic attachment
var attachment = new Attachment
{
      ODataType = "#microsoft.graph.fileAttachment",
      contentBytes = Convert.ToBase64String(File.ReadAllBytes(path)),
      name = $"attachment.pdf"
};

//Post request to create the attachment and get updated obj back
attachment = GraphUtil.CreateAttachment(attachment);

//Prepare new content to update the event
var newContent = new
{
       attachments = new List<Attachment> { attachment },
       attendees = New List<Attendee> { attends } //populate attendees here
};

//Patch request containing only the new content get the updated event obj back.
eventContainer = GraphUtil.UpdateEvent(newContent);

Если вы отправите вложение после отправки события, участники смогут видеть вложение только в своем календарном мероприятии, а не в электронном письме о мероприятии, в котором запрашивается их подтверждение.

Вы можете поделиться файлом из OneDrive в рамках мероприятия, создав ссылку для общего доступа.

Если файла нет в OneDrive, сначала необходимо загрузить файл в OneDrive. После этого вы можете создать ссылку для совместного использования и представить вложение участникам (с предоставленным доступом) в теле события.

      public async Task<DriveItem> uploadFileToOneDrive(string eventOwnerEmail, string filePath, string fileName)
{
    // get a stream of the local file
    FileStream fileStream = new FileStream(filePath, FileMode.Open);

    string token = GetGraphToken();

    var graphServiceClient = new GraphServiceClient(new DelegateAuthenticationProvider((requestMessage) =>
    {
        requestMessage
            .Headers
            .Authorization = new AuthenticationHeaderValue("bearer", token);

        return Task.FromResult(0);
    }));


    // upload the file to OneDrive
    var uploadedFile = graphServiceClient.Users[eventOwnerEmail].Drive.Root
                                  .ItemWithPath(fileName)
                                  .Content
                                  .Request()
                                  .PutAsync<DriveItem>(fileStream)
                                  .Result;

    return uploadedFile;
}


public async Task<Permission> getShareLinkOfDriveItem(string eventOwnerEmail, DriveItem _item)
{
    string token = GetGraphToken();

    var graphServiceClient = new GraphServiceClient(new DelegateAuthenticationProvider((requestMessage) =>
    {
        requestMessage
            .Headers
            .Authorization = new AuthenticationHeaderValue("bearer", token);

        return Task.FromResult(0);
    }));    
    
    var type = "view";
    var scope = "anonymous"; 

    var ret = await graphServiceClient.Users[eventOwnerEmail].Drive.Items[_item.Id]
                                .CreateLink(type, scope, null, null, null)
                                .Request()
                                .PostAsync();


    return ret;
}

вы можете вызывать методы из своей программы, как показано ниже:

      var driveItem = uploadFileToOneDrive(eventOwnerEmail, filePath, fileName);
Task<Permission> shareLinkInfo = getShareLinkOfDriveItem(eventOwnerEmail, driveItem);

string shareLink = shareLinkInfo.Result.Link.WebUrl;

вы можете прикрепить общий файл к телу события, как показано ниже:

       body = "<p>You can access the file from the link: <a href = '" + shareLink  + "' >" + driveItem.Name + " </a></p> ";