I'm currently developing a solution which is retrieving e-mails via the Microsoft Graph API. In november 2015 Microsoft stated it is ready for production and I've read in another forum post that if you start now on developing using a Microsoft API, you should use the Graph API, since it is the future.
Everything is going well except for one thing and that is the following.
I must retrieve e-mails. Inside these e-mails there are of course attachments. These attachments come in some variaties. fileAttachment (images, documents etc.), referenceAttachments and itemAttachments (outlook-item). The issue here is with the itemAttachments. An itemAttachment can be anything from an appointment to another message. The problem here is that I'm not able to get and retrieve the contentBytes in some way which is working for fileAttachments. A related object to itemAttachment is outlookItem. There is also a page with a description made for this outlookItem, but the examples and the details are missing.
The user rights are set to Mail.Read and Mail.ReadWrite.
Links:
General overview: http://graph.microsoft.io/docs/overview/overview
Get outlookItem (empty?):
Example call and response I get. Please note the types of the attachments.
https://graph.microsoft.com/v1.0 /users/ /messages/ /attachments
{
"#odata.context": "link",
"value": [
{
"#odata.type": "#microsoft.graph.fileAttachment",
"id": "AAMkAGU2NmIwMTcxLTljYzUtNGRiMi1hZjczLTllNzhiZDRiNWZlZABGAAAAPAD_Lx_gimDGRqSr98J_O_e6BwDcWyYHlO7rS5_XpLHCx6NSAAIMC0V-AADcWyYHlO7rS5_XpLHCx6NSAAIMC6RgAAABEgAQAGhN_vm1RlBPt7V4N9a89UY=",
"lastModifiedDateTime": "2016-01-13T14:25:33Z",
"name": "image001.png",
"contentType": "image/png",
"size": 5077,
"isInline": true,
"contentId": "image001.png#01D14E16.A3A32480",
"contentLocation": null,
"contentBytes": "iVBORw0KGgoAAAANSUhEUgAAAKAAAACCCAIAAABOyVRHAAAAAXNSR0IArs4c6QAAEndJREFUeF7tXQ1QFFe2bkbU... (truncated)"
},
{
"#odata.type": "#microsoft.graph.fileAttachment",
"id": "AAMkAGU2NmIwMTcxLTljYzUtNGRiMi1hZjczLTllNzhiZDRiNWZlZABGAAAAPAD_Lx_gimDGRqSr98J_O_e6BwDcWyYHlO7rS5_XpLHCx6NSAAIMC0V-AADcWyYHlO7rS5_XpLHCx6NSAAIMC6RgAAABEgAQAFnSLgIC5wZOosmLtBWK8gE=",
"lastModifiedDateTime": "2016-01-13T14:25:34Z",
"name": "image002.png",
"contentType": "image/png",
"size": 3722,
"isInline": true,
"contentId": "image002.png#01D14E16.A3A32480",
"contentLocation": null,
"contentBytes": "iVBORw0KGgoAAAANSUhEUgAAAPoAAABSCAYAAAB9o8m+AAAAGXRFWHRTb... (truncated)"
},
{
"#odata.type": "#microsoft.graph.fileAttachment",
"id": "AAMkAGU2NmIwMTcxLTljYzUtNGRiMi1hZjczLTllNzhiZDRiNWZlZABGAAAAPAD_Lx_gimDGRqSr98J_O_e6BwDcWyYHlO7rS5_XpLHCx6NSAAIMC0V-AADcWyYHlO7rS5_XpLHCx6NSAAIMC6RgAAABEgAQANOuw7m8sW1Ot3MivYQ5OYU=",
"lastModifiedDateTime": "2016-01-13T14:25:24Z",
"name": "Knipsel.PNG",
"contentType": null,
"size": 7641,
"isInline": false,
"contentId": null,
"contentLocation": null,
"contentBytes": "iVBORw0KGgoAAAANSUhEUgAAAKAAAACCCAYAAADBq8MQAAA... (truncated)"
},
{
"#odata.type": "#microsoft.graph.itemAttachment",
"id": "AAMkAGU2NmIwMTcxLTljYzUtNGRiMi1hZjczLTllNzhiZDRiNWZlZABGAAAAPAD_Lx_gimDGRqSr98J_O_e6BwDcWyYHlO7rS5_XpLHCx6NSAAIMC0V-AADcWyYHlO7rS5_XpLHCx6NSAAIMC6RgAAABEgAQAPEUC740tjtAlNTe8NpopUI=",
"lastModifiedDateTime": "2016-01-14T15:55:07Z",
"name": "RE: Test met plaatje",
"contentType": null,
"size": 36972,
"isInline": false
}
]
}
I've tried to change the GET-statement by pasting the attachment id with or without the messages path and the expand feature (which is only supported one level deep), but I can't seem te find the solution.
Something I've found is this question, which is kind of the same, however it is for the office365 unified API. How to retrieve ItemAttachment contents from Office 365 REST API?.
So, the question: How can I retrieve the contents of an outlookItem via the Microsoft Graph API? And how do I know what to expect? Can anybody help me getting past this obstacle.
Use $expand option:
GET https://graph.microsoft.com/v1.0/me/messages('AAMkADA1M-zAAA=')/attachments('AAMkADA1M-CJKtzmnlcqVgqI=')/?$expand=microsoft.graph.itemattachment/item
Response:
HTTP/1.1 200 OK
Content-type: application/json
{
"#odata.context":"https://graph.microsoft.com/v1.0/$metadata#users('d1a2fae9-db66-4cc9-8133-2184c77af1b8')/messages('AAMkADA1M-zAAA%3D')/attachments/$entity",
"#odata.type":"#microsoft.graph.itemAttachment",
"id":"AAMkADA1MCJKtzmnlcqVgqI=",
"lastModifiedDateTime":"2017-07-21T00:20:34Z",
"name":"Reminder - please bring laptop",
"contentType":null,
"size":32005,
"isInline":false,
"item#odata.context":"https://graph.microsoft.com/v1.0/$metadata#users('d1a2fae9-db66-4cc9-8133-2184c77af1b8')/messages('AAMkADA1M-zAAA%3D')/attachments('AAMkADA1M-CJKtzmnlcqVgqI%3D')/microsoft.graph.itemAttachment/item/$entity",
"item":{
"#odata.type":"#microsoft.graph.message",
"id":"",
"createdDateTime":"2017-07-21T00:20:41Z",
"lastModifiedDateTime":"2017-07-21T00:20:34Z",
"receivedDateTime":"2017-07-21T00:19:55Z",
"sentDateTime":"2017-07-21T00:19:52Z",
"hasAttachments":false,
"internetMessageId":"<BY2PR15MB05189A084C01F466709E414F9CA40#BY2PR15MB0518.namprd15.prod.outlook.com>",
"subject":"Reminder - please bring laptop",
"importance":"normal",
"conversationId":"AAQkADA1MzMyOGI4LTlkZDctNDkzYy05M2RiLTdiN2E1NDE3MTRkOQAQAMG_NSCMBqdKrLa2EmR-lO0=",
"isDeliveryReceiptRequested":false,
"isReadReceiptRequested":false,
"isRead":false,
"isDraft":false,
"webLink":"https://outlook.office365.com/owa/?ItemID=AAMkADA1M3MTRkOQAAAA%3D%3D&exvsurl=1&viewmodel=ReadMessageItem",
"body":{
"contentType":"html",
"content":"<html><head>\r\n</head>\r\n<body>\r\n</body>\r\n</html>"
},
"sender":{
"emailAddress":{
"name":"Adele Vance",
"address":"AdeleV#contoso.onmicrosoft.com"
}
},
"from":{
"emailAddress":{
"name":"Adele Vance",
"address":"AdeleV#contoso.onmicrosoft.com"
}
},
"toRecipients":[
{
"emailAddress":{
"name":"Alex Wilbur",
"address":"AlexW#contoso.onmicrosoft.com"
}
}
],
"ccRecipients":[
{
"emailAddress":{
"name":"Adele Vance",
"address":"AdeleV#contoso.onmicrosoft.com"
}
}
]
}
}
Source: https://developer.microsoft.com/en-us/graph/docs/api-reference/v1.0/api/attachment_get#request-2
The official documentation: https://graph.microsoft.io/en-us/docs/api-reference/beta/api/attachment_get.htm . Use valid Bearer authentication access code, and check for appropriate Graph API permissions on the Azure management portal. Attachment is based64 encoded string, coming in the contentBytes field. Correct Uri for loading list of a message attachments is: https://graph.microsoft.com/beta/me/messages/[ message Id ]/attachments. Sample code to call attachments endpoint is below:
using (var client = new HttpClient())
{
using (var request = new HttpRequestMessage(HttpMethod.Get,
"https://graph.microsoft.com/beta/me/messages/..id../attachments"))
{
request.Headers.Authorization =
new AuthenticationHeaderValue("Bearer", "...valid access token...");
using (HttpResponseMessage response = await client.SendAsync(request))
{
if (response.StatusCode == HttpStatusCode.OK)
{
result = await response.Content.ReadAsStringAsync();
var json = JObject.Parse(result);
}
}
}
}
Get attachment using MS Graph API for Java:
First build the graph client. Sample code
ClientSecretCredential clientSecretCredential = new ClientSecretCredentialBuilder()
.clientId(yourClientId).clientSecret(yourClientSecret)
.tenantId(yourTenantId).build();
TokenCredentialAuthProvider tokenCredAuthProvider = new TokenCredentialAuthProvider(clientSecretCredential);
GraphServiceClient<Request> gClient = GraphServiceClient.builder().authenticationProvider(tokenCredAuthProvider)
.buildClient();
Either get all the attachments for a message-
FileAttachment fa = (FileAttachment) graphClient.users(id).messages(messageId).attachments().buildRequest().get();
Or get a particular attachment by passing the attachment id:
FileAttachment fa = (FileAttachment) graphClient.users(id).messages(messageId).attachments(attachmentId).buildRequest().get();
//Copy file attachment into a File from byte stream using FileUtils.
FileUtils.writeByteArrayToFile(new File(yourFileLocation), fa.contentBytes);
You can also similarly either get all users, messages or attachments or pass particular id to get a unique result for these entities.
Related
I'm trying to get DriveItem activities on OneDrive for business using this Graph API https://learn.microsoft.com/en-us/graph/api/itemactivitystat-getactivitybyinterval?view=graph-rest-1.0&tabs=csharp
But the Activity property of the returned response is always null and I can not get what I need (activities on the specific DriveItem, like edits, comments etc.)
Definitely there are some activities for that driveItem, webhook is triggered on item change and I'm able to track changes using delta API: https://learn.microsoft.com/en-us/graph/delta-query-overview.
The code I'm using is like this:
var request = new GraphServiceClient(new DelegateAuthenticationProvider(msg =>
{
msg.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
return Task.CompletedTask;
}))
.Drives[driveId].Items[driveItemId]
.GetActivitiesByInterval(start, end, "day")
.Request();
var resultPage = await request.GetAsync();
The full response with Activities == null is below:
{
"Access": {
"ActionCount": 3,
"ActorCount": 1,
"AdditionalData": {
"timeSpentInSeconds": {
"ValueKind": 4
}
},
"ODataType": null
},
"Create": null,
"Delete": null,
"Edit": null,
"EndDateTime": "2022-02-12T23:59:59+00:00",
"IncompleteData": {
"MissingDataBeforeDateTime": null,
"WasThrottled": false,
"AdditionalData": {
"resultsPending": {
"ValueKind": 6
},
"notSupported": {
"ValueKind": 6
}
},
"ODataType": null
},
"IsTrending": null,
"Move": null,
"StartDateTime": "2022-02-12T00:00:00+00:00",
"Activities": null,
"ActivitiesNextLink": null,
"Id": null,
"ODataType": "#microsoft.graph.itemActivityStat",
"AdditionalData": {
"aggregationInterval": {
"ValueKind": 3
}
}
}
Is there a way to get a list of activities for the specific OneDrive item through API (comments, edits, etc.)?
The only way of getting user activities I've found so far is to use this SharePoint site list API instead of OneDrive API directly: https://learn.microsoft.com/en-us/graph/api/list-get?view=graph-rest-1.0&tabs=csharp
,where {list-id} is user's SharePoint site list named userActivityFeedHiddenList.
The API response is a bit cryptic, but it's possible to see who commented what on a document and when.
Response examples can be found here https://charleslakes.com/2020/09/28/onedrive-hidden-list-user-activity/
I am using Microsoft Search API to search for files in SharePoint, objective is to fetch temporary url ("#microsoft.graph.downloadUrl") amoung other fields. I have used below search request,
Url - POST request: https://graph.microsoft.com/v1.0/search/query
Body:
{
"requests": [
{
"entityTypes": [
"driveItem"
],
"query": {
"queryString": "search-value",
"query": "path:\"https://{company}.sharepoint.com/sites/{site}/shared\""
},
"fields": [
"name",
"webUrl",
"#microsoft.graph.downloadUrl"
]
}
]
}
The result returned is below, and it is missing "#microsoft.graph.downloadUrl" field.
"moreResultsAvailable": false,
"hits": [
{
"hitId": "01CDLPULQVGFRNVHYTJVFL3NPPXIGOUDUS",
"rank": 1,
"summary": "string;#<c0>search-value</c0> <c0>search-value</c0> Internal 1 0 search-value Graph explorer {DA623115<ddd/>",
"resource": {
"#odata.type": "#microsoft.graph.driveItem",
"name": "word.docx",
"webUrl": "https://company.sharepoint.com/sites/{site}/Cases/search-value/word.docx"
}
},
Search API can retrieve only properties but not instance attributes like "#microsoft.graph.downloadUrl".
Properties
Instance attributes
I think there's no need to use the REST API. To get the temporary #microsoft.graph.downloadUrl download link I used the following to get the value:
var selectedFile = files.value[0];
var downloadLink = selectedFile["#microsoft.graph.downloadUrl"];
Found at this link: https://github.com/OneDrive/onedrive-api-docs/issues/547
I hope this helps
Using the C# calling SDK for Graph, I can join a bot to a meeting, and add participants by GUID from the same tenant. Where this falls apart though is adding a participant from PSTN/direct routing. If I have a regular user add a participant, I see this update:
"#odata.type": "#microsoft.graph.participant",
"info": {
"#odata.type": "#microsoft.graph.participantInfo",
"identity": {
"#odata.type": "#microsoft.graph.identitySet",
"phone": {
"#odata.type": "#microsoft.graph.identity",
"id": "+19054181287",
"identityProvider": "None"
}
},
"endpointType": "default"
},
Seems reasonable enough, so I tried adding a participant like this:
InvitationParticipantInfo target = new InvitationParticipantInfo { Identity = new IdentitySet(), };
target.Identity.SetInAdditionalData("phone", new Identity() { Id = targetID });
target.Identity.AdditionalData["phone"].SetInAdditionalData("tenantId", m_tenantID);
target.Identity.AdditionalData["phone"].SetInAdditionalData("identityProvider", "None");
target.EndpointType = EndpointType.Default;
await call1.Participants.InviteAsync(new List<InvitationParticipantInfo>() { target });
Which generates a request that looks like this (interesting that the identityProvider changed):
"participants": [
{
"#odata.type": "#microsoft.graph.invitationParticipantInfo",
"identity": {
"#odata.type": "#microsoft.graph.identitySet",
"phone": {
"#odata.type": "#microsoft.graph.identity",
"id": "+19054181287",
"tenantId": "8c6ae172-a3ea-4f50-994d-a0256822697f",
"identityProvider": "(Generic: l3QstqQ1)"
}
},
"endpointType": "default"
}
And that comes back with this:
Action failed: 81bd4426-7200-4c50-acba-f17b7a987322
exception: Code: 7505
Message: Request authorization tenant mismatch.
Scenario Id: 46ffbff3-bf9f-4b6d-a639-974c07b959dc
Inner error:
AdditionalData:
date: 2020-10-13T18:45:30
request-id: 5d152942-dc10-4baa-bb28-48bf991f944b
client-request-id: 83f84370-3708-4e3c-a34d-2f69bd8eb8a7
Given that the meeting organizer has permissions for PSTN dialout, as do invitees, is there a reason that a bot shouldn't be able to add a participant like this? What's interesting is that something very similar does work for redirecting/transferring incoming PSTN calls. The "Request authorization tenant mismatch" seems like a red herring (since it's all the same tenant)-is this just a case of "not supported right now"? I know PSTN calls from bots are (still) not officially supported, but I was curious whether anyone had a way to make this work?
I am building some utilities to automate aspects of Microsoft Teams at my company. One thing we are trying is automating scheduling/creation of Online Meetings under various circumstances. Overall this is working fine, but I can't figure out how to get / attach telephone call-in information for the calls we're creating.
Here's an example POST /app/onlineMeetings:
{
"meetingType": "meetNow",
"participants": {
"organizer": {
"identity": {
"user": {
"id": "<user-id>"
}
}
}
},
"subject": "Personal Room"
}
And here's what a typical response looks like:
{
"#odata.context": "https://graph.microsoft.com/beta/$metadata#app/onlineMeetings/$entity",
"joinUrl": "<join-url>",
"subject": "Personal Room",
"isCancelled": false,
"meetingType": "MeetNow",
"accessLevel": "SameEnterprise",
"id": "<meeting-id>",
"audioConferencing": null,
"meetingInfo": null,
"participants": {
"organizer": {
"upn": "<user-name>",
"sipProxyAddress": "<user-name>",
"identity": {
}
},
"attendees": []
},
"chatInfo": {}
}
As you can see, the audioConferencing key is null. If a user accesses the joinUrl, they can join the call and audio conferencing information is displayed at that time -- but I can't figure out how to get it out in advance (e.g. to send in an email).
Also note that since this is not a VTC-enabled meeting, the id can't be used to issue a new GET request for additional information, as discussed here
Whenever i tried to attach attachment with TFS WorkItem via REST call, attachment size is 0KB.
First I upload an attachment in Attachment Store using below code.
https://{instance}/DefaultCollection/_apis/wit/attachments?api-version=1.0&filename="{fileName}"
I send data in bytes array through rest call. and after this i attach that attachment with workitem.
Attaching attachment is success but size of an attachment is zero KB
Is there is an issue with TFS or something i am doing wrong?
I am using C# language for programming and REST Sharp for accessing VSTS APIs
Dim restClient = New RestClient("Server URL")
restClient.Authenticator = New HttpBasicAuthenticator("UserId", "Password")
Dim request = New RestRequest("API_Name", Method.POST)
request.AlwaysMultipartFormData = False
request.AddParameter(String.Format("{0}; charset=utf-8", contentType), File.ReadAllBytes(filePath), ParameterType.RequestBody)
request.RequestFormat = DataFormat.Json
Dim response As IRestResponse = restClient.Execute(request)
Return response
I am sending file data in bytes.
Attaching Attachment with WorkItem.
Dim restClient = New RestClient(ACCESS_URL)
restClient.Authenticator = New HttpBasicAuthenticator(USER_NAME, PASSWORD)
Dim request = New
RestRequest("CollectionName}/_apis/wit/workitems/{WorkItem_ID}", Method.PATCH)
request.AddParameter("application/json-patch+json; charset=utf-8",
"post_Data", ParameterType.RequestBody)
request.RequestFormat = DataFormat.Json
Dim response As IRestResponse = restClient.Execute(request)
Return response
Post_Data is an json string which take this type of data
[{
"op": "add",
"path": "/relations/-",
"value": {
"rel": "AttachedFile",
"url": "AttachementURI",
}]
You missed "attributes" section in the Post_Data, try with below:
[{
"op": "add",
"path": "/relations/-",
"value": {
"rel": "AttachedFile",
"url": "AttachementURI",
"attributes": {
}
}]
I can reproduce this issue when keep the { file-contents } as empty.
So, make sure you have specified the { file-contents }.
To attach a file to a work item, upload the attachment to the
attachment store, then attach it to the work item. See Add an attachment for details.
Upload an attachment:
POST https://{instance}/DefaultCollection/_apis/wit/attachments?api-version={version}&filename=Spec.txt
Content-Type: application/octet-stream
{ file-contents }
Add an attachment for specific work item:
PATCH https://fabrikam-fiber-inc.visualstudio.com/DefaultCollection/_apis/wit/workitems/299?api-version=1.0
Content-Type: application/json-patch+json
[
{
"op": "test",
"path": "/rev",
"value": 3
},
{
"op": "add",
"path": "/fields/System.History",
"value": "Adding the necessary spec"
},
{
"op": "add",
"path": "/relations/-",
"value": {
"rel": "AttachedFile",
"url": "https://fabrikam-fiber-inc.visualstudio.com/DefaultCollection/_apis/wit/attachments/098a279a-60b9-40a8-868b-b7fd00c0a439?fileName=Spec.txt",
"attributes": {
"comment": "Spec for the work"
}
}
}
]
See below C# sample to upload and add the attachment for a work item:
C# (UploadTextFile method)
C# (AddAttachment method)