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