dalin
dalin

Reputation: 43

How to use $value using Microsoft Graph library in C#

I am trying to receive some .eml attachments from some emails. Based on the documentation from https://learn.microsoft.com/en-us/graph/outlook-get-mime-message I need to use: GET /users/{id}/messages/{id}/attachments/{id}/$value

The problem here is that I don't know how to do this using Microsoft.Graph library in C#. I don't know to append that "$value" to the call. Below I have attached the C# structure that I am currently using to get attachments for a specific email. Any advice could help. Thanks.

return await _graphServiceClient.Me.Messages[emailId].Attachments.Request().GetAsync()

Upvotes: 4

Views: 1692

Answers (1)

Jason Johnston
Jason Johnston

Reputation: 17692

The SDK doesn't currently support this in a straight-forward manner. Typically to get the /$value segment appended to the generated request URL, you access a Content property on the request builder. The problem is that the generic IAttachmentRequestBuilder doesn't implement this property, only the FileAttachmentRequestBuilder does.

So to get this to work with the current SDK, you need to do it like this:

var msgId = "message-id";
var attId = "attachment-id";

var attachmentRequestBuilder = client.Me.Messages[msgId].Attachments[attId];
var fileRequestBuilder = new FileAttachmentRequestBuilder(
    attachmentRequestBuilder.RequestUrl, client);

Console.WriteLine($"Request URL: {fileRequestBuilder.Content.Request().RequestUrl}");
var stream = await fileRequestBuilder.Content.Request().GetAsync();

using(var reader = new StreamReader(stream))
{
    Console.WriteLine("Attachment contents:");
    while (!reader.EndOfStream)
    {
        var line = reader.ReadLine();
        Console.WriteLine(line);
    }
}

I've let the SDK folks know about this.

Upvotes: 5

Related Questions