2

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

3 に答える 3

0

共有リンクを作成することで、イベント内で 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> "; 
于 2021-10-01T16:30:49.487 に答える