{"id":25363,"date":"2020-10-30T13:21:02","date_gmt":"2020-10-30T13:21:02","guid":{"rendered":"https:\/\/www.inogic.com\/blog\/?p=25363"},"modified":"2020-10-30T13:21:02","modified_gmt":"2020-10-30T13:21:02","slug":"match-theme-of-dynamics-365-app-in-power-apps-control-framework-part-2","status":"publish","type":"post","link":"https:\/\/www.inogic.com\/blog\/2020\/10\/match-theme-of-dynamics-365-app-in-power-apps-control-framework-part-2\/","title":{"rendered":"Match theme of Dynamics 365 App in Power Apps Control Framework \u2013 Part 2"},"content":{"rendered":"<p style=\"text-align: justify;\">In the <a href=\"https:\/\/www.inogic.com\/blog\/2020\/10\/match-the-theme-of-dynamics-365-app-in-power-apps-control-framework-part-1\/\" target=\"_blank\" rel=\"noopener noreferrer\">last blog<\/a>, we saw how we could match the theme of the Dynamics 365 App in PCF when we know what should be the color scheme at the time of designing.<\/p>\n<p style=\"text-align: justify;\">In this blog, we will see how we can apply the theme to the Fluent UI component dynamically based on the main color set in the Dynamics 365 App Theme record.<\/p>\n<h2><strong>Retrieve theme record<\/strong><\/h2>\n<p style=\"text-align: justify;\">\nTo know what the main color is set in the theme record, we would need to retrieve the theme record first. Use webApi.retrieveMultipleRecords method in the updateView method of the PCF and on success, render the react component. Pass the object of the theme record to the root main component.<\/p>\n<blockquote><p>public updateView(context: ComponentFramework.Context&lt;IInputs&gt;): void {<\/p>\n<p>\/\/retrieve theme<\/p>\n<p>let options = &#8220;?$filter=isdefaulttheme eq true&#8221;;<\/p>\n<p>this._context.webAPI.retrieveMultipleRecords(&#8220;theme&#8221;, options).then((result) =&gt; {<\/p>\n<p>this._themeRecord = result.entities[0];<\/p>\n<p>\/\/ Add code to update control view<\/p>\n<p>ReactDOM.render(<\/p>\n<p>React.createElement(App, { context: this._context, themeRecord: this._themeRecord }), this._container);<\/p>\n<p>}, (error) =&gt; {<\/p>\n<p>});<\/p><\/blockquote>\n<p>This theme records contains the maincolor value.<\/p>\n<p><a href=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2020\/10\/Match-theme-of-Dynamics-365-App-in-Power-Apps-Control-Framework-\u2013-Part-2-img1.png\" target=\"_blank\" rel=\"noopener noreferrer\"><img decoding=\"async\" class=\"aligncenter wp-image-25358 size-full\" style=\"border: 1px solid #0a0a0a; padding: 1px; margin: 1px;\" src=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2020\/10\/Match-theme-of-Dynamics-365-App-in-Power-Apps-Control-Framework-\u2013-Part-2-img1.png\" alt=\"Match theme of Dynamics 365 App in Power Apps Control Framework \u2013 Part 2\" width=\"384\" height=\"274\" srcset=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2020\/10\/Match-theme-of-Dynamics-365-App-in-Power-Apps-Control-Framework-\u2013-Part-2-img1.png 384w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2020\/10\/Match-theme-of-Dynamics-365-App-in-Power-Apps-Control-Framework-\u2013-Part-2-img1-300x214.png 300w\" sizes=\"(max-width: 384px) 100vw, 384px\" \/><\/a><\/p>\n<h2><strong>Generate Fluent UI Palette color slots<\/strong><\/h2>\n<p style=\"text-align: justify;\">We now have maincolor but do not have the palette color slots. We want different shades of the main color, which we saw, in our <a href=\"https:\/\/www.inogic.com\/blog\/2020\/10\/match-the-theme-of-dynamics-365-app-in-power-apps-control-framework-part-1\/\" target=\"_blank\" rel=\"noopener noreferrer\">previous blog<\/a>; we copied the color slots from <a href=\"https:\/\/fabricweb.z5.web.core.windows.net\/pr-deploy-site\/refs\/heads\/master\/theming-designer\/index.html\" target=\"_blank\" rel=\"noopener noreferrer\">Fluent UI Theme designer<\/a>.<br \/>\nTo generate color slots programmatically, we would need to use <a href=\"https:\/\/docs.microsoft.com\/en-us\/javascript\/api\/office-ui-fabric-react\/themegenerator?view=office-ui-fabric-react-latest\" target=\"_blank\" rel=\"noopener noreferrer\">ThemeGenerator<\/a> Class.\u00a0 I have created a ThemeProvider.ts as a separate file with the below code.<\/p>\n<blockquote><p>import\u00a0{\u00a0loadTheme\u00a0}\u00a0from\u00a0&#8216;@uifabric\/styling&#8217;;<\/p>\n<p>import\u00a0{<\/p>\n<p>IThemeRules,<\/p>\n<p>ThemeGenerator,<\/p>\n<p>themeRulesStandardCreator,<\/p>\n<p>}\u00a0from\u00a0&#8216;office-ui-fabric-react\/lib\/ThemeGenerator&#8217;;<\/p>\n<p>import\u00a0{\u00a0getColorFromString\u00a0}\u00a0from\u00a0&#8216;office-ui-fabric-react\/lib\/utilities\/color\/getColorFromString&#8217;;<\/p>\n<p>import\u00a0{\u00a0IColor\u00a0}\u00a0from\u00a0&#8216;office-ui-fabric-react\/lib\/utilities\/color\/interfaces&#8217;;<\/p>\n<p>&nbsp;<\/p>\n<p>export\u00a0default\u00a0class\u00a0ThemeProvider\u00a0{<\/p>\n<p>private\u00a0themeRules:\u00a0IThemeRules;<\/p>\n<p>constructor()\u00a0{<\/p>\n<p>const\u00a0themeRules\u00a0=\u00a0themeRulesStandardCreator();<\/p>\n<p>ThemeGenerator.insureSlots(this.themeRules,\u00a0false);<\/p>\n<p>ThemeGenerator.setSlot(themeRules.backgroundColor,\u00a0getColorFromString(&#8216;#ffffff&#8217;)\u00a0||\u00a0&#8220;&#8221;,\u00a0false,\u00a0true,\u00a0true);<\/p>\n<p>ThemeGenerator.setSlot(themeRules.foregroundColor,\u00a0getColorFromString(&#8216;#000000&#8217;)\u00a0||\u00a0&#8220;&#8221;,\u00a0false,\u00a0true,\u00a0true);<\/p>\n<p>this.themeRules\u00a0=\u00a0themeRules;<\/p>\n<p>}<\/p>\n<p>public\u00a0loadThemeForColor(hexColor:\u00a0string):\u00a0void\u00a0{<\/p>\n<p>let\u00a0col:\u00a0IColor\u00a0=\u00a0{\u00a0str:\u00a0&#8220;#0078D4&#8221;,\u00a0r:\u00a00,\u00a0g:\u00a0120,\u00a0b:\u00a0212,\u00a0hex:\u00a0&#8220;0078d4&#8221;,\u00a0h:\u00a00,\u00a0s:\u00a00,\u00a0v:\u00a00\u00a0};<\/p>\n<p>const\u00a0newColor:\u00a0IColor\u00a0=\u00a0getColorFromString(hexColor)\u00a0||\u00a0col;<\/p>\n<p>&nbsp;<\/p>\n<p>const\u00a0themeRules\u00a0=\u00a0this.themeRules;<\/p>\n<p>ThemeGenerator.setSlot(themeRules.primaryColor,\u00a0newColor.str\u00a0||\u00a0&#8220;&#8221;,\u00a0false,\u00a0true,\u00a0true);<\/p>\n<p>this.themeRules\u00a0=\u00a0themeRules;<\/p>\n<p>const\u00a0theme\u00a0=\u00a0ThemeGenerator.getThemeAsJson(this.themeRules);<\/p>\n<p>loadTheme({<\/p>\n<p>&#8230;{\u00a0palette:\u00a0theme\u00a0},<\/p>\n<p>isInverted:\u00a0false,<\/p>\n<p>});<\/p>\n<p>}<\/p>\n<p>}<\/p><\/blockquote>\n<p style=\"text-align: justify;\">This ThemeProvider.ts has \u201cloadThemeForColor\u201d method, which accepts the color string and then generates theme and loads it.<\/p>\n<h2><strong>Load theme in the root component<\/strong><\/h2>\n<p style=\"text-align: justify;\">Import the above-created \u201cThemeProvider\u201d class in the main root component and then call \u201cloadThemeForColor\u201d method by passing the maincolor that we have retrieved from the Dynamics 365 Theme record. The theme loaded in the root component will get applied to all the child components.<\/p>\n<blockquote><p>import\u00a0ThemeProvider\u00a0from\u00a0&#8220;..\/ThemeProvider&#8221;<\/p><\/blockquote>\n<p><a href=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2020\/10\/Match-theme-of-Dynamics-365-App-in-Power-Apps-Control-Framework-\u2013-Part-2-img3.png\" target=\"_blank\" rel=\"noopener noreferrer\"><img decoding=\"async\" class=\"aligncenter wp-image-25360 size-full\" style=\"border: 1px solid #0a0a0a; padding: 1px; margin: 1px;\" src=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2020\/10\/Match-theme-of-Dynamics-365-App-in-Power-Apps-Control-Framework-\u2013-Part-2-img3.png\" alt=\"Match theme of Dynamics 365 App in Power Apps Control Framework \u2013 Part 2\" width=\"1040\" height=\"410\" srcset=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2020\/10\/Match-theme-of-Dynamics-365-App-in-Power-Apps-Control-Framework-\u2013-Part-2-img3.png 1040w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2020\/10\/Match-theme-of-Dynamics-365-App-in-Power-Apps-Control-Framework-\u2013-Part-2-img3-300x118.png 300w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2020\/10\/Match-theme-of-Dynamics-365-App-in-Power-Apps-Control-Framework-\u2013-Part-2-img3-768x303.png 768w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2020\/10\/Match-theme-of-Dynamics-365-App-in-Power-Apps-Control-Framework-\u2013-Part-2-img3-1024x404.png 1024w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2020\/10\/Match-theme-of-Dynamics-365-App-in-Power-Apps-Control-Framework-\u2013-Part-2-img3-660x260.png 660w\" sizes=\"(max-width: 1040px) 100vw, 1040px\" \/><\/a><\/p>\n<p style=\"text-align: justify;\">Call \u201cloadThemeForColor\u201d method before any app code. Here I have called this function in the root component\u2019s constructor.<\/p>\n<blockquote><p>\u00a0\u00a0\u00a0\u00a0constructor(props:\u00a0AppProps)\u00a0{<\/p>\n<p>super(props);<\/p>\n<p>\/\/get\u00a0main\u00a0theme\u00a0color<\/p>\n<p>let\u00a0mainColor\u00a0=\u00a0props.themeRecord[&#8220;maincolor&#8221;];<\/p>\n<p>&nbsp;<\/p>\n<p>let\u00a0themeProvider\u00a0=\u00a0new\u00a0ThemeProvider();<\/p>\n<p>themeProvider.loadThemeForColor(mainColor);<\/p>\n<p>}<\/p><\/blockquote>\n<p><a href=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2020\/10\/Match-theme-of-Dynamics-365-App-in-Power-Apps-Control-Framework-\u2013-Part-2-img3.png\" target=\"_blank\" rel=\"noopener noreferrer\"><img decoding=\"async\" class=\"aligncenter wp-image-25360 size-full\" style=\"border: 1px solid #0a0a0a; padding: 1px; margin: 1px;\" src=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2020\/10\/Match-theme-of-Dynamics-365-App-in-Power-Apps-Control-Framework-\u2013-Part-2-img3.png\" alt=\"Match theme of Dynamics 365 App in Power Apps Control Framework \u2013 Part 2\" width=\"1040\" height=\"410\" srcset=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2020\/10\/Match-theme-of-Dynamics-365-App-in-Power-Apps-Control-Framework-\u2013-Part-2-img3.png 1040w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2020\/10\/Match-theme-of-Dynamics-365-App-in-Power-Apps-Control-Framework-\u2013-Part-2-img3-300x118.png 300w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2020\/10\/Match-theme-of-Dynamics-365-App-in-Power-Apps-Control-Framework-\u2013-Part-2-img3-768x303.png 768w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2020\/10\/Match-theme-of-Dynamics-365-App-in-Power-Apps-Control-Framework-\u2013-Part-2-img3-1024x404.png 1024w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2020\/10\/Match-theme-of-Dynamics-365-App-in-Power-Apps-Control-Framework-\u2013-Part-2-img3-660x260.png 660w\" sizes=\"(max-width: 1040px) 100vw, 1040px\" \/><\/a><\/p>\n<p>This way we can dynamically set the theme for the Fluent UI components.<\/p>\n<h2><strong>Test the app<\/strong><\/h2>\n<p style=\"text-align: justify;\">Deploy the control to Dynamics 365 environment, change the default theme main color, and publish.<\/p>\n<p><a href=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2020\/10\/Match-theme-of-Dynamics-365-App-in-Power-Apps-Control-Framework-\u2013-Part-2-img4.png\" target=\"_blank\" rel=\"noopener noreferrer\"><img decoding=\"async\" class=\"aligncenter wp-image-25361 size-full\" style=\"border: 1px solid #0a0a0a; padding: 1px; margin: 1px;\" src=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2020\/10\/Match-theme-of-Dynamics-365-App-in-Power-Apps-Control-Framework-\u2013-Part-2-img4.png\" alt=\"Match theme of Dynamics 365 App in Power Apps Control Framework \u2013 Part 2\" width=\"542\" height=\"329\" srcset=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2020\/10\/Match-theme-of-Dynamics-365-App-in-Power-Apps-Control-Framework-\u2013-Part-2-img4.png 542w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2020\/10\/Match-theme-of-Dynamics-365-App-in-Power-Apps-Control-Framework-\u2013-Part-2-img4-300x182.png 300w\" sizes=\"(max-width: 542px) 100vw, 542px\" \/><\/a><\/p>\n<p style=\"text-align: justify;\">Our component will read this main color form the theme record and will apply the generated theme.<\/p>\n<p><a href=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2020\/10\/Match-theme-of-Dynamics-365-App-in-Power-Apps-Control-Framework-\u2013-Part-2-img5.png\" target=\"_blank\" rel=\"noopener noreferrer\"><img decoding=\"async\" class=\"aligncenter wp-image-25357 size-full\" style=\"border: 1px solid #0a0a0a; padding: 1px; margin: 1px;\" src=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2020\/10\/Match-theme-of-Dynamics-365-App-in-Power-Apps-Control-Framework-\u2013-Part-2-img5.png\" alt=\"Match theme of Dynamics 365 App in Power Apps Control Framework \u2013 Part 2\" width=\"514\" height=\"444\" srcset=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2020\/10\/Match-theme-of-Dynamics-365-App-in-Power-Apps-Control-Framework-\u2013-Part-2-img5.png 514w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2020\/10\/Match-theme-of-Dynamics-365-App-in-Power-Apps-Control-Framework-\u2013-Part-2-img5-300x259.png 300w\" sizes=\"(max-width: 514px) 100vw, 514px\" \/><\/a><br \/>\nIn some cases, you may require to modify individual components style. As mentioned in the <a href=\"https:\/\/www.inogic.com\/blog\/2020\/10\/match-the-theme-of-dynamics-365-app-in-power-apps-control-framework-part-1\/\" target=\"_blank\" rel=\"noopener noreferrer\">previous blog<\/a><strong>,<\/strong> we can modify individual component style using <a href=\"https:\/\/github.com\/microsoft\/fluentui\/wiki\/Component-Styling\" target=\"_blank\" rel=\"noopener noreferrer\">https:\/\/github.com\/microsoft\/fluentui\/wiki\/Component-Styling<\/a><\/p>\n<h2><strong>Conclusion<\/strong><\/h2>\n<p style=\"text-align: justify;\">We can apply the theme to the Fluent UI component dynamically based on the main color set in the Dynamics 365 App Theme record.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In the last blog, we saw how we could match the theme of the Dynamics 365 App in PCF when we know what should be the color scheme at the time of designing. In this blog, we will see how we can apply the theme to the Fluent UI component dynamically based on the main\u2026 <span class=\"read-more\"><a href=\"https:\/\/www.inogic.com\/blog\/2020\/10\/match-theme-of-dynamics-365-app-in-power-apps-control-framework-part-2\/\">Read More &raquo;<\/a><\/span><\/p>\n","protected":false},"author":13,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[16,19,2053],"tags":[545,592,1169,1941,1337,1338,1710],"class_list":["post-25363","post","type-post","status-publish","format-standard","hentry","category-dynamics-365","category-dynamics-crm","category-power-apps-portals","tag-dynamics-365-crm","tag-dynamics-crm","tag-microsoft-power-apps","tag-pcf-control","tag-power-apps","tag-power-apps-dynamics-365-crm","tag-theme"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/posts\/25363","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/users\/13"}],"replies":[{"embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/comments?post=25363"}],"version-history":[{"count":0,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/posts\/25363\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/media?parent=25363"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/categories?post=25363"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/tags?post=25363"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}