0

Currently using the outlook v2.0 , which is really new for me and I have come upon an unforeseen issue. Currently I have my authentication and am able to create my outlook client with out a problem. However, after making a test run on a mailbox that currently have 250 emails I see that this api only retrieves 10. Looking to see if anyone has ran into this issue when using the Outlook api v 2.0

Code

    private static async Task<OutlookServicesClient> CreateOutlookClientAsync(AuthenticationContext authenticationContext)
    {

        OutlookServicesClient outlookClient = null;
        try
        {
            outlookClient = new OutlookServicesClient(
                new Uri(CrmPrototype.Helpers.AuthHelper.OutlookAPIEndpoint),
                async () =>
                    await GetTokenHelperAsync(authenticationContext, CrmPrototype.Helpers.AuthHelper.OutlookAuthenticationEndpoint)
                    );                
            return outlookClient;
        }
        catch (Exception ex)
        {
            // TODO Log
            return outlookClient;
        }
    }
    private static async Task<GraphServiceClient> CreateGraphClientAsync(AuthenticationContext authenticationContext)
    {
        GraphServiceClient graphClient = null;
        try
        {
            graphClient = new GraphServiceClient(
            new DelegateAuthenticationProvider(
                async (requestMessage) =>
                {
                    string accessToken = await GetTokenHelperAsync(authenticationContext, CrmPrototype.Helpers.AuthHelper.OutlookAuthenticationEndpoint);

                    // Append the access token to the request.
                    requestMessage.Headers.Authorization = new AuthenticationHeaderValue("bearer", accessToken);
                }));
            return graphClient;
        }
        catch (Exception ex)
        {
            // TODO Log
            return graphClient;
        }
    }
    private static async Task<string> GetTokenHelperAsync(AuthenticationContext context, string resourceId)
    {
        string accessToken = null;
        try
        {
            X509Certificate2 certificate = new X509Certificate2(CrmPrototype.Helpers.AuthHelper.devCertPath, CrmPrototype.Helpers.AuthHelper.devCertKey, X509KeyStorageFlags.MachineKeySet);
            ClientAssertionCertificate clientAssertionCert = new ClientAssertionCertificate(CrmPrototype.Helpers.AuthHelper.devClientId, certificate);                
            AuthenticationResult result = null;
            result = await context.AcquireTokenAsync(resourceId, clientAssertionCert);
            accessToken = result.AccessToken;
            return accessToken;
        }
        catch (Exception ex)
        {
            // TODO Log
            return accessToken;
        }
    }
    public static async Task<IMessageCollection> GetEmails(string emailBox)
    {
        IMessageCollection emails = null;
        AuthenticationContext authenticationContext = new AuthenticationContext(CrmPrototype.Helpers.AuthHelper.devTenant);
        try
        {
            var outlookClient = await CreateOutlookClientAsync(authenticationContext);
            var mail_Box = await outlookClient.Users[emailBox].MailFolders["Inbox"].Messages.OrderByDescending(m => m.ReceivedDateTime).ExecuteAsync();
            var messages = mail_Box.CurrentPage; << only gets 10 emails at a time

            foreach (var message in messages)
            {
                var stop = 0;
            }
            return emails;
        }
        catch (Exception ex)
        {
            // TODO Log
            return emails;
        }
    }   

Watch Results

enter image description here

EasyE
  • 560
  • 5
  • 26

1 Answers1

1

The OutlookREST API as the name indicates is a REST api. You are using an SDK that forges the http web requests on your behalf. Personally I forge the requests manually even if I work with C#. Even when using an SDK, my advice is that you need to look at the actual requests generated and sent to the servers hosting the api. I suggest that you use a tool like fiddler.

Having said that, the default parameter for the $top ODATA parameter is set to 10. That is why you have 10 items but do not worry you can fetch them all by calling the next pagination. You can see the actual continuation url in the _continuation member. Note the values $top and $skip parameters: this means that with this request you are fetching 10 more items skipping the 10 first items.

Generally, you won't be able to fetch all items from an unbounded source with one call. You need a pagination mechanism somewhere. However, you can increase the page size by changing the value for the $top parameter. Using the .NET SDK, from this page it looks like you should use the Take method from LINQ.

Benoit Patra
  • 4,355
  • 5
  • 30
  • 53
  • Thank you for the response Benoit, I am familiar with paging in JavaScript which is done very easy because its lax restraints, but I can get my head wrapped around it, can you point me towards an example, or article? Thank you once again. – EasyE Jan 19 '17 at 12:13
  • I don' t understand what you need precisely but this documentation on Outlook REST API ODATA params https://msdn.microsoft.com/en-us/office/office365/api/complex-types-for-mail-contacts-calendar#OdataQueryParams will help you. – Benoit Patra Jan 19 '17 at 13:33