{"id":5752,"date":"2021-01-12T06:00:24","date_gmt":"2021-01-12T14:00:24","guid":{"rendered":"https:\/\/officedevblogs.wpengine.com\/?p=5752"},"modified":"2021-11-17T12:45:34","modified_gmt":"2021-11-17T20:45:34","slug":"microsoft-graph-mailbag-taking-advantage-of-mgt-get-component-in-the-microsoft-graph-toolkit","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/microsoft365dev\/microsoft-graph-mailbag-taking-advantage-of-mgt-get-component-in-the-microsoft-graph-toolkit\/","title":{"rendered":"Microsoft Graph Mailbag &#8211; Taking advantage of mgt-get component in the Microsoft Graph Toolkit"},"content":{"rendered":"<p>In today\u2019s Microsoft Graph Mailbag post we will cover one of the most flexible components of the Microsoft Graph Toolkit, the mgt-get component! If you want to learn more about the Microsoft Graph Toolkit and get a primer on its capabilities, we suggest revisiting a blog series written by the team and community members called <a href=\"http:\/\/aka.ms\/mgtlap\">A Lap around Microsoft Graph Toolkit<\/a>.<\/p>\n<p>Please be sure to follow this blog series using\u00a0<a href=\"https:\/\/aka.ms\/MSGraphMailbag\">https:\/\/aka.ms\/MSGraphMailbag<\/a>\u00a0or with RSS using\u00a0<a href=\"https:\/\/developer.microsoft.com\/graph\/blogs\/feed\/?tag=MSGraphMailbag\">https:\/\/developer.microsoft.com\/graph\/blogs\/feed\/?tag=MSGraphMailbag<\/a>.<\/p>\n<h2>Getting started with the Microsoft Graph Toolkit mgt-get component<\/h2>\n<p>Unlike the other components of the Microsoft Graph Toolkit, the mgt-get component allows developers to build custom Microsoft Graph queries and their own HTML to render the data that is returned by Microsoft Graph. The component does not provide any default UI and requires you to write a template from scratch.<\/p>\n<p>To get started, we recommend you take a look at the <a href=\"https:\/\/developer.microsoft.com\/microsoft-365\/blogs\/a-lap-around-microsoft-graph-toolkit-day-2-zero-to-hero\/\">Zero to Hero<\/a> blog post from the <a href=\"http:\/\/aka.ms\/mgtlap\">Lap around Microsoft Graph Toolkit<\/a> blog series to get your environment ready.<\/p>\n<h2>Building your first custom component with mgt-get<\/h2>\n<p>Let us start by creating a very simple custom component using mgt-get. This first component will display the first name, last name, and email of the currently logged in user.<\/p>\n<p>The component will require 2 main attributes: the resource to query and the necessary scopes.<\/p>\n<pre class=\"\">&lt;mgt-get resource=\"\/me\" scopes=\"User.Read\"&gt;\n  &lt;template&gt;\n    Hello, {{ givenName }} {{ surname }}! Your email is {{ mail }}\n  &lt;\/template&gt;\n&lt;\/mgt-get&gt;<\/pre>\n<p>As you can see, the result returned by the \/me endpoint of Microsoft Graph is available within your template thanks to the use of brackets. The result is \u00a0simple but powerful HTML rendered with the data provide by Microsoft Graph.<\/p>\n<p><img decoding=\"async\" class=\" wp-image-5753 aligncenter\" src=\"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-content\/uploads\/sites\/73\/2021\/01\/2020-01-12-Image1.jpg\" alt=\"\" width=\"721\" height=\"105\" srcset=\"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-content\/uploads\/sites\/73\/2021\/01\/2020-01-12-Image1.jpg 845w, https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-content\/uploads\/sites\/73\/2021\/01\/2020-01-12-Image1-300x44.jpg 300w, https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-content\/uploads\/sites\/73\/2021\/01\/2020-01-12-Image1-768x112.jpg 768w\" sizes=\"(max-width: 721px) 100vw, 721px\" \/><\/p>\n<h2>List content with mgt-get<\/h2>\n<p>Not only can you display a specific object when utilizing the mgt-get component, but also any list of data. For example, if you want to display a list of messages as part of your template, you need to be able to format and display all your messages in a certain way. The template will support different operations like <a href=\"https:\/\/docs.microsoft.com\/en-us\/graph\/toolkit\/customize-components\/templates#conditional-rendering\">conditional formatting<\/a> and <a href=\"https:\/\/docs.microsoft.com\/graph\/toolkit\/customize-components\/templates#looping\">looping<\/a>. This will help us to render a template in a way that brings some logic and control. With the following example, it is possible to display all the messages that are in the currently logged in user\u2019s inbox.<\/p>\n<pre class=\"\">&lt;mgt-get resource=\"\/me\/mailFolders\/Inbox\/messages\" scopes=\"Mail.Read\"&gt;\n  &lt;template&gt;\n    &lt;ul&gt;\n      &lt;li data-for=\"email in value\"&gt;\n        {{ email.sender.emailAddress.address }} : {{ email.subject }}\n      &lt;\/li&gt;\n    &lt;\/ul&gt;\n  &lt;\/template&gt;\n&lt;\/mgt-get&gt;<\/pre>\n<p>This template will list all the emails as a simple unordered list and will allow you to fully style it.<\/p>\n<p><img decoding=\"async\" class=\" wp-image-5754 aligncenter\" src=\"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-content\/uploads\/sites\/73\/2021\/01\/2020-01-12-Image2.jpg\" alt=\"\" width=\"625\" height=\"167\" srcset=\"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-content\/uploads\/sites\/73\/2021\/01\/2020-01-12-Image2.jpg 737w, https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-content\/uploads\/sites\/73\/2021\/01\/2020-01-12-Image2-300x80.jpg 300w\" sizes=\"(max-width: 625px) 100vw, 625px\" \/><\/p>\n<h2>Advanced scenarios with mgt-get<\/h2>\n<h3>Debugging<\/h3>\n<p>While you are developing templates, you will want to peek at the data that is returned by Microsoft Graph. You can use template capabilities to leverage a helper method to display the raw object directly in the UI while you are developing.<\/p>\n<pre class=\"\">&lt;mgt-get resource=\"\/me\" scopes=\"User.Read\"&gt;\n  &lt;template&gt;\n    {{ this }}\n  &lt;\/template&gt;\n&lt;\/mgt-get&gt;<\/pre>\n<h3><img decoding=\"async\" class=\" wp-image-5755 aligncenter\" src=\"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-content\/uploads\/sites\/73\/2021\/01\/2020-01-12-Image3.jpg\" alt=\"\" width=\"753\" height=\"115\" srcset=\"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-content\/uploads\/sites\/73\/2021\/01\/2020-01-12-Image3.jpg 975w, https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-content\/uploads\/sites\/73\/2021\/01\/2020-01-12-Image3-300x46.jpg 300w, https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-content\/uploads\/sites\/73\/2021\/01\/2020-01-12-Image3-768x117.jpg 768w\" sizes=\"(max-width: 753px) 100vw, 753px\" \/><\/h3>\n<h3>Converting data<\/h3>\n<p>If you want to bring this capability to the next level, you can also add features to augment the data, to do advanced formatting or to bring any of your custom logic to your templates. In the following example, we use a custom converter to allow logging to the browser console when messages are rendered.<\/p>\n<pre class=\"\">&lt;mgt-get id=\"mgt-get-events\" resource=\"\/me\/mailFolders\/Inbox\/messages\" scopes=\"Mail.Read\"&gt;\n  &lt;template&gt;\n    &lt;ul&gt;\n      &lt;li data-for=\"email in value\"&gt;\n        {{ getFormattedMessage(email) }}\n      &lt;\/li&gt;\n    &lt;\/ul&gt;\n  &lt;\/template&gt;\n&lt;\/mgt-get&gt;\n\n&lt;script&gt;\n  document.querySelector('#mgt-get-events').templateContext = {\n    getFormattedMessage: (message) =&gt; {\n      console.log(message);\n      return `${message.sender.emailAddress.address} : ${message.subject}`;\n    }\n  }\n&lt;\/script&gt;<\/pre>\n<h3>Caching data<\/h3>\n<p>To improve performance and to avoid round trips to Microsoft Graph, the mgt-get component supports caching data which will make a big difference when building custom components with the Microsoft Graph Toolkit. In order to enable caching you only need to specify \u201ccache-enabled\u201d on your component and the component will make sure your calls are cached at the browser level. Using the \u201ccache-invalidation-period\u201d you can also modify how long this item will be cached for the current user.<\/p>\n<pre class=\"\">&lt;mgt-get resource=\"\/me\" scopes=\"User.Read\" cache-enabled=\"true\"&gt;\n  &lt;template&gt;\n    Hello, {{ givenName }} {{ surname }}! Your email is {{ mail }}\n  &lt;\/template&gt;\n&lt;\/mgt-get&gt;<\/pre>\n<h3>Nesting components<\/h3>\n<p>In more complex scenarios, you will want to do subsequent calls to Microsoft Graph to get more details or related information about an item that was retrieved. Luckily, the templating system of the Microsoft Graph Toolkit allows for nesting components. If you want to include a mgt-get component within another mgt-get component, it is possible! For example, you want to find your manager and then discover all the files trending around them. To achieve this, you can nest the following components.<\/p>\n<pre class=\"\">&lt;mgt-get resource=\"\/me\/manager\" scopes=\"User.Read.All\"&gt;\n  &lt;template&gt;\n    Hello, your manager is {{ givenName }} {{ surname }}! Their email is {{ mail }}! The following list is what is trending around them :\n\n    &lt;mgt-get resource=\"\/users\/{{ id }}\/insights\/trending\" scopes=\"Sites.Read.All\" cache-enabled=\"true\"&gt;\n      &lt;template&gt;\n        &lt;ul&gt;\n          &lt;li data-for=\"file in value\"&gt;\n            &lt;a href=\"{{ file.resourceReference.webUrl }}\"&gt;{{ file.resourceVisualization.title }}&lt;\/a&gt;\n          &lt;\/li&gt;\n        &lt;\/ul&gt;\n      &lt;\/template&gt;\n    &lt;\/mgt-get&gt;\n  &lt;\/template&gt;\n&lt;\/mgt-get&gt;<\/pre>\n<h3><img decoding=\"async\" class=\" wp-image-5756 aligncenter\" src=\"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-content\/uploads\/sites\/73\/2021\/01\/2020-01-12-Image4.jpg\" alt=\"\" width=\"845\" height=\"188\" srcset=\"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-content\/uploads\/sites\/73\/2021\/01\/2020-01-12-Image4.jpg 975w, https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-content\/uploads\/sites\/73\/2021\/01\/2020-01-12-Image4-300x67.jpg 300w, https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-content\/uploads\/sites\/73\/2021\/01\/2020-01-12-Image4-768x171.jpg 768w\" sizes=\"(max-width: 845px) 100vw, 845px\" \/><\/h3>\n<h3>Using other mgt components<\/h3>\n<p>Finally, as we discovered it is possible to nest components within other components, one of the great capabilities is to utilize the other Microsoft Graph Toolkit components as part of your custom template. You could for instance leverage the mgt-person component to highlight your manager in a rich way.<\/p>\n<pre class=\"\">&lt;mgt-get resource=\"\/me\/manager\" scopes=\"User.Read.All\" cache-enabled=\"true\"&gt;\n  &lt;template&gt;\n    Hello, your manager is &lt;mgt-person user-id=\"{{ id }}\" view=\"oneline\" show-presence=\"true\" person-card=\"hover\"&gt;&lt;\/mgt-person&gt; &lt;br \/&gt;\n\n    The following list is what is trending around them :\n\n    &lt;mgt-get resource=\"\/users\/{{ id }}\/insights\/trending\" scopes=\"Sites.Read.All\" cache-enabled=\"true\"&gt;\n      &lt;template&gt;\n        &lt;ul&gt;\n          &lt;li data-for=\"file in value\"&gt;\n            &lt;a href=\"{{ file.resourceReference.webUrl }}\"&gt;{{ file.resourceVisualization.title }}&lt;\/a&gt;\n          &lt;\/li&gt;\n        &lt;\/ul&gt;\n      &lt;\/template&gt;\n    &lt;\/mgt-get&gt;\n  &lt;\/template&gt;\n&lt;\/mgt-get&gt;<\/pre>\n<p>&nbsp;<\/p>\n<p><img decoding=\"async\" class=\"size-full wp-image-5757 aligncenter\" src=\"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-content\/uploads\/sites\/73\/2021\/01\/2020-01-12-Image5.jpg\" alt=\"\" width=\"339\" height=\"244\" srcset=\"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-content\/uploads\/sites\/73\/2021\/01\/2020-01-12-Image5.jpg 339w, https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-content\/uploads\/sites\/73\/2021\/01\/2020-01-12-Image5-300x216.jpg 300w\" sizes=\"(max-width: 339px) 100vw, 339px\" \/><\/p>\n<p><img decoding=\"async\" class=\"size-full wp-image-5758 aligncenter\" src=\"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-content\/uploads\/sites\/73\/2021\/01\/2020-01-12-Image6.jpg\" alt=\"\" width=\"255\" height=\"392\" srcset=\"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-content\/uploads\/sites\/73\/2021\/01\/2020-01-12-Image6.jpg 255w, https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-content\/uploads\/sites\/73\/2021\/01\/2020-01-12-Image6-195x300.jpg 195w\" sizes=\"(max-width: 255px) 100vw, 255px\" \/><\/p>\n<h3>Conclusion<\/h3>\n<p>In today&#8217;s post, we looked at ways you can leverage the mgt-get component to build rich end-user experiences with the Microsoft Graph Toolkit. If you have any feedback or suggested features for the Microsoft Graph Toolkit, please open an issue on the <a href=\"https:\/\/aka.ms\/mgt\">GitHub repository<\/a>!<\/p>\n<p>You can also find a working example on the following <a href=\"https:\/\/github.com\/sebastienlevert\/mgt-playground\/tree\/main\/01-mgt-get\">GitHub repository<\/a>!<\/p>\n<p>Today\u2019s post was written by S\u00e9bastien Levert, Sr. Customer and Partner Experience PM with the Microsoft Graph Team.\u00a0 You can follow S\u00e9bastien on Twitter <a href=\"https:\/\/twitter.com\/sebastienlevert\">@sebastienlevert<\/a>.\u00a0 Join us for our next Microsoft Graph Mailbag post Jan 26, 2021.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In today\u2019s Microsoft Graph Mailbag post we will cover one of the most flexible components of the Microsoft Graph Toolkit, the mgt-get component.<\/p>\n","protected":false},"author":69075,"featured_media":25159,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[3],"tags":[34,17],"class_list":["post-5752","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-microsoft-graph","tag-microsoft-graph-toolkit","tag-msgraphmailbag"],"acf":[],"blog_post_summary":"<p>In today\u2019s Microsoft Graph Mailbag post we will cover one of the most flexible components of the Microsoft Graph Toolkit, the mgt-get component.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-json\/wp\/v2\/posts\/5752","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-json\/wp\/v2\/users\/69075"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-json\/wp\/v2\/comments?post=5752"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-json\/wp\/v2\/posts\/5752\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-json\/wp\/v2\/media\/25159"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-json\/wp\/v2\/media?parent=5752"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-json\/wp\/v2\/categories?post=5752"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-json\/wp\/v2\/tags?post=5752"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}