0

I am trying to figure out a way of returning messages from a sub folder in outlook office 365 api. Everything seems to point to this;

HttpResponseMessage response = await client.GetAsync("https://outlook.office365.com/api/v1.0/me/folders/inbox/childfolders/Odata/messages");

But I always get a bad request returned.

Here is my resource.

MSDN

Thanks Scott

Jason Johnston
  • 17,194
  • 2
  • 20
  • 34
scottsanpedro
  • 1,142
  • 2
  • 12
  • 28

2 Answers2

0
public void EnsureConnectionValid()
    {
        if (AuthenticationContext == null)
        {
            AuthenticationContext = new AuthenticationContext(authority);
            AuthenticationResult = AuthenticationContext.AcquireToken(resource, clientId, new Uri(redirectUri), PromptBehavior.Auto);
        }
    }

    public async Task<string> GetFolderId(string Path)
    {
        EnsureConnectionValid();
        var client = new HttpClient();
        client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", AuthenticationResult.AccessToken);
        var restCommand = "https://outlook.office365.com/api/v1.0/me/folders/Inbox/childfolders?$filter=DisplayName eq " + "'" + Path + "'";
        HttpResponseMessage response = await client.GetAsync(restCommand);
        response.EnsureSuccessStatusCode();
        string jsonMessage;
        using (var responseStream = await response.Content.ReadAsStreamAsync())
        {
            jsonMessage = new StreamReader(responseStream).ReadToEnd();
        }
        var folderObject = JObject.Parse(jsonMessage)["value"].ToObject<FoldersList[]>();
        return folderObject.Select(r => r.Id).SingleOrDefault();
    }
scottsanpedro
  • 1,142
  • 2
  • 12
  • 28
0

The URL syntax is:

https://outlook.office365.com/api/v1.0/me/folders/<FOLDER ID>/Messages

So you need to get the ID for the folder you want to query. For example, if it's a subfolder of the Inbox, you could do a GET to:

https://outlook.office365.com/api/v1.0/me/folders/inbox/childfolders

And you'd get back something like:

{
  "@odata.context": "https://outlook.office365.com/api/v1.0/$metadata#Me/Folders('inbox')/ChildFolders",
  "value": [
    {
      "@odata.id": "https://outlook.office365.com/api/v1.0/Users('JasonJ@contoso.com')/Folders('AAMkADNhMjcxM2U5LWY2MmItNDRjYy05YzgwLWQwY2FmMTU1MjViOAAuAAAAAAC_IsPnAGUWR4fYhDeYtiNFAQCDgDrpyW-uTL4a3VuSIF6OAAAeY0W3AAA=')",
      "Id": "AAMkADNhMjcxM2U5LWY2MmItNDRjYy05YzgwLWQwY2FmMTU1MjViOAAuAAAAAAC_IsPnAGUWR4fYhDeYtiNFAQCDgDrpyW-uTL4a3VuSIF6OAAAeY0W3AAA=",
      "ParentFolderId": "AAMkADNhMjcxM2U5LWY2MmItNDRjYy05YzgwLWQwY2FmMTU1MjViOAAuAAAAAAC_IsPnAGUWR4fYhDeYtiNFAQCDgDrpyW-uTL4a3VuSIF6OAAAAAAEMAAA=",
      "DisplayName": "New Subfolder",
      "ChildFolderCount": 0
    }
  ]
}

Then take the value of the Id field and plug it into the URL:

https://outlook.office365.com/api/v1.0/me/folders/AAMkADNhMjcxM2U5LWY2MmItNDRjYy05YzgwLWQwY2FmMTU1MjViOAAuAAAAAAC_IsPnAGUWR4fYhDeYtiNFAQCDgDrpyW-uTL4a3VuSIF6OAAAeY0W3AAA=/Messages
Jason Johnston
  • 17,194
  • 2
  • 20
  • 34