Exercise - Load a user's emails in batches
In this exercise, you'll extend the app to let you load a user's emails in batches of 10 items.
Load emails in batches of 10 items
Start by updating the getEmails()
function to load emails in batches of 10 items. If the next set of emails to load has been defined, it's passed as the function's argument.
In your code editor, open the graph.js file.
Update the
getEmails()
function's signature to accept a single argumentnextLink
:async function getEmails(nextLink) { // ... }
If the
nextLink
has been set, the function should pass it to the SDK to retrieve the data. IfnextLink
hasn't been set, the function should load the initial set of data. Update thegetEmails()
function by replacing thereturn
statement with the followingif
statement:if (nextLink) { return await graphClient .api(nextLink) .get(); } else { return await graphClient .api('/me/messages') .select('subject,receivedDateTime') .orderby('receivedDateTime desc') .top(10) .get(); }
The updated
getEmails()
function should look as follows:async function getEmails(nextLink) { ensureScope('mail.read'); if (nextLink) { return await graphClient .api(nextLink) .get(); } else { return await graphClient .api('/me/messages') .select('subject,receivedDateTime') .orderby('receivedDateTime desc') .top(10) .get(); } }
Extend the template to allow users to load more emails
You've extended the getEmails()
function to load more emails. The next step is to show a button that will let users load more emails if available.
In your code editor, open the index.html file.
Locate the line
<ul id="emails"></ul>
and add the following code immediately after it to add a button that allows users to load more emails.<div id="loadMoreContainer" style="display: none;"> <button onclick="displayEmail();">Load more</button> </div>
Load more emails
With the app updated to let users load more emails, the final step is to add functionality to handle loading more emails.
In your code editor, open the ui.js file.
Before the
displayEmail()
function, define a new variable namednextLink
without assigning a value to it:var nextLink;
In the
displayEmail()
function, update the call to get thegetEmails()
function to include thenextLink
.var emails = await getEmails(nextLink);
Next, after retrieving the data, get the value of the
@odata.nextLink
property. If set, it will indicate that there's more data available for users to show. Add the following code immediately after theif
statement in thedisplayEmail()
function:nextLink = emails['@odata.nextLink'];
At the end of the
displayEmail()
function, after displaying the retrieved emails, scroll to the end of the page so that the user can immediately see the retrieved emails.window.scrollTo({ top: emailsUl.scrollHeight, behavior: 'smooth' });
Finally, check if
nextLink
has been returned and if so, display the button to load more emails.Add the following code to the end of the
displayEmail()
function:if (nextLink) { document.getElementById('loadMoreContainer').style = 'display: block'; }
The complete
displayEmail()
function should look as follows:var nextLink; async function displayEmail() { var emails = await getEmails(nextLink); if (!emails || emails.value.length < 1) { return; } nextLink = emails['@odata.nextLink']; document.getElementById('displayEmail').style = 'display: none'; var emailsUl = document.getElementById('emails'); emails.value.forEach(email => { var emailLi = document.createElement('li'); emailLi.innerText = `${email.subject} (${new Date(email.receivedDateTime).toLocaleString()})`; emailsUl.appendChild(emailLi); }); window.scrollTo({ top: emailsUl.scrollHeight, behavior: 'smooth' }); if (nextLink) { document.getElementById('loadMoreContainer').style = 'display: block'; } }
Run your app
You've extended your app to show a user's emails by using Microsoft Graph in batches of 10 items and let them load more emails. Let's run the app locally.
Preview the web app by executing the following command in the terminal.
npm start
Your browser should be pointing to
http://localhost:8080
.Select the Sign in with Microsoft button to sign in with your Microsoft 365 account.
After you sign in with your account, select the Show email button.
You should see a list of the user's last 10 emails displayed in the app.
If there are more than 10 emails in your mailbox, you'll see a button that allows you to load the next 10 messages.
Stop the Node.js server by selecting CTRL+C in the terminal window.