microsoftgraph / msgraph-sdk-java

Microsoft Graph SDK for Java
https://docs.microsoft.com/en-us/graph/sdks/sdks-overview
MIT License
372 stars 125 forks source link

Not able to use '$value' to retrive item attachment content using Graph SDK #733

Closed SandhyaMadhuri closed 3 years ago

SandhyaMadhuri commented 3 years ago

Expected behavior

The below article provides a way to retrieve MIME content of Item attachment https://docs.microsoft.com/en-us/graph/api/attachment-get?view=graph-rest-1.0&tabs=http GET /users/{id | userPrincipalName}/messages/{id}/attachments/{id}/$value I am trying to achieve the same using GRAPHSDK .Tried couple of ways but did not work. Can you guide on how can we retrive MIME content of Item Attachments using GRAPH SDK Able to retrieve json content of Item Attachment using $expand using SDK But want to retrieve MIME content of Item attchmnet using $value using SDK

Actual behavior

Steps to reproduce the behavior

AB#9102

baywet commented 3 years ago

Hi @SandhyaMadhuri , Thanks for reaching out. Have you tried the following:

final InputStream result = graphClient.me().messages("id").attachments("id").content().buildRequest().get();

final BufferedReader r = new BufferedReader(new InputStreamReader(result));
final StringBuilder content = new StringBuilder();
String line;
while ((line = r.readLine()) != null) {
     content.append(line).append('\n');
}
SandhyaMadhuri commented 3 years ago

Thanks @baywet for quick response I do not see content() method available on attachments.I am using 1.4.0 version of graph SDK.Is it available in latest version.Is there any other way to achieve in 1.4 version grapherror

baywet commented 3 years ago

Please upgrade to version 3.3.0, version 1.4.0 has been out of support for a long time now.

SandhyaMadhuri commented 3 years ago

Thanks @baywet. I am trying to read an item attachment which has nested attachments(file/item) using expand in SDK and able to reterive attachments inside this item attachment in additionalDataManager of itemAtachment.item My use case is to build Message using that data and add it newly created item attachment more like itemattachment.item = new newGraphMessage ; So ,Uiing gson I converted that json to Message Object Now I am not able to see attachments in Message object created

String data = item.additionalDataManager().toString(); Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().enableComplexMapKeySerialization().create(); Message newGraphMessage = gson.fromJson(data, Message.class); To be precise,newGraphMessage doesnt hav attachments although 'data' has the attachments JSOn array values

Is this issue fixed in latest version? Is there a way to achieve it using existing version?

baywet commented 3 years ago

Since v3.X released, we're not providing support for 1.X and providing limited support for 2.X. Dozens of bugfixes and improvements have been made between 1.X and 3.X. While it's hard to answer your specific questions, you should definitively upgrade your project to benefit those improvements, and to get support if you still run into issues.

Regarding my previous answer, a cast is necessary to get the content method, this is due to a quirk in how the Outlook APIs are defined:

final InputStream result = ((FileAttachmentRequestBuilder)graphClient.me().messages("id").attachments("id")).content().buildRequest().get();

final BufferedReader r = new BufferedReader(new InputStreamReader(result));
final StringBuilder content = new StringBuilder();
String line;
while ((line = r.readLine()) != null) {
     content.append(line).append('\n');
}
SandhyaMadhuri commented 3 years ago

Thnak @baywet Do not see content() method for ItemAttachmentRequestBuilder Is it not supported or Can I use FileAttachmentRequstbuilder for retrieveing both File and Item atatchment content by passing appropraite attachmentId

baywet commented 3 years ago

Again, you probably need to update to the latest version first.

baywet commented 3 years ago

@SandhyaMadhuri thanks for closing the issue. Can you confirm this was solved by upgrading for other readers?

PlayPratz commented 3 years ago

I am on Graph SDK v3.5.0, but the following code block suggested by @baywet does not compile.

final InputStream result = ((FileAttachmentRequestBuilder)graphClient.me().messages("id").attachments("id")).content().buildRequest().get();

The error is Cannot cast from AttachmentRequestBuilder to FileAttachmentRequestBuilder

baywet commented 3 years ago

Hi @PlayPratz , Thanks for the additional information. Can you try the following then?

final InputStream result = new FileAttachmentStreamRequestBuilder(graphClient.me().messages("id").attachments("id").buildRequest().getRequestUrl().toString() + "/$value", graphClient, null).buildRequest().get();
PlayPratz commented 3 years ago

Hi @baywet

If we are bound to use the FileAttachmentStreamRequestBuilder constructor, I think the following would be a cleaner approach:

final InputStream result = new FileAttachmentRequestBuilder(graphClient.me().messages("id").attachments("id").getRequestUrl(), graphClient, null).content().buildRequest().get();

However, I think there should be an easier way to achieve the same. Probably something like:

final InputStream result = graphClient.me().messages("id").fileAttachments("id").content().buildRequest().get();
baywet commented 3 years ago

Hi @PlayPratz , In this case you need to use the FileAttachmentRequestBuilder as it has the content method but the stream one doesn't.

The SDK is generated automatically from the service metadata. And the service (plus its metadata) doesn't support OData Casting. This means the SDK generator cannot generate fluent API paths to get to the content download (as fileAttachment inherits from the attachment class, and the collection returns attachments, not file attachment).

This is something we've already funneled back to the team to fix, without success so far. I logged an issue here for visibility.

You can additionally request this feature

PlayPratz commented 3 years ago

Hi @baywet,

Sorry for the mistake. I have corrected my earlier comment for anyone who reaches this thread for help.

I'll keep a look out for any updates on the issue you have logged. Thanks for the help.