{"id":20846,"date":"2019-10-18T12:16:39","date_gmt":"2019-10-18T12:16:39","guid":{"rendered":"https:\/\/www.inogic.com\/blog\/?p=20846"},"modified":"2019-10-18T12:16:39","modified_gmt":"2019-10-18T12:16:39","slug":"refresh-power-bi-dataset-programmatically-from-dynamics-365-crm","status":"publish","type":"post","link":"https:\/\/www.inogic.com\/blog\/2019\/10\/refresh-power-bi-dataset-programmatically-from-dynamics-365-crm\/","title":{"rendered":"Refresh Power BI Dataset programmatically from Dynamics 365 CRM"},"content":{"rendered":"<h2>Introduction<\/h2>\n<p style=\"text-align: justify;\">Many of you are aware of manual refresh and schedule refresh feature of Power BI. Schedule refresh have refresh limit set based on the type of Power BI license. Users can add power BI report to CRM via CRM dashboard and Power BI dashboard. Also user\u2019s data gets updated frequently and refreshing Power BI dataset each time from Power BI service would be time consuming as every time user needs to go to the Power BI Service for refreshing Power BI report.<\/p>\n<p style=\"text-align: justify;\">Apart from that, user also needs to manually configure schedule refresh for the dataset and specify the refresh timings and time zone in the Power BI Service. So having a button on CRM Dashboard page to automatically refresh the dataset in Power BI Service is more convenient for the user as there will be no further need to switch to the Power BI service for refreshing dataset.<\/p>\n<p style=\"text-align: justify;\">In this blog we will see how to refresh Power BI report from Dynamics 365 CRM.<br \/>\nFor this, we will create a global action in which we will call a custom assembly to make refresh request of Power BI. Now for making call to refresh request of Power BI we have the following two steps:<\/p>\n<p><strong>1<\/strong>. Getting access token from Power BI<br \/>\nIn order to get access token from Power BI Service we need to register a native application in Azure.<br \/>\nPlease check the below link for creating native application in Azure AAD.<\/p>\n<p><a href=\"https:\/\/docs.microsoft.com\/en-us\/azure\/active-directory\/manage-apps\/application-proxy-configure-native-client-application\" target=\"_blank\" rel=\"noopener noreferrer\">https:\/\/docs.microsoft.com\/en-us\/azure\/active-directory\/manage-apps\/application-proxy-configure-native-client-application<\/a><\/p>\n<p style=\"text-align: justify;\">After registering an application, assign required Power BI Service delegated permission to native application.<br \/>\nCopy the client Id from the native application as shown in the below screenshot:<\/p>\n<p><img decoding=\"async\" class=\"aligncenter wp-image-20844\" style=\"border: 1px solid #0a0a0a; padding: 1px; margin: 1px;\" src=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2019\/10\/1Refresh-Power-BI-Dataset-programmatically.png\" alt=\"Refresh Power BI Dataset programmatically\" width=\"834\" height=\"485\" srcset=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2019\/10\/1Refresh-Power-BI-Dataset-programmatically.png 621w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2019\/10\/1Refresh-Power-BI-Dataset-programmatically-300x174.png 300w\" sizes=\"(max-width: 834px) 100vw, 834px\" \/><\/p>\n<p>This Client Id will be used for getting access token from Power BI Service.<\/p>\n<p>Use the below code to get access token from Power BI Service.<\/p>\n<pre class=\"lang:default decode:true \">PowerBIAccessToken token = null;\r\nstring oauthurl = string.Format(\"https:\/\/login.microsoftonline.com\/common\/oauth2\/token\");\r\n\r\nstring reqBody = string.Format(\"client_id={0}&amp;grant_type={1}&amp;resource={2}&amp;username={3}&amp;password={4}\",\r\n\r\nUri.EscapeDataString(clientId), Uri.EscapeDataString(grantType),\r\n\r\nUri.EscapeDataString(resource), Uri.EscapeDataString(userName), Uri.EscapeDataString(password));\r\n\r\ntracingService.Trace(\"Getting Access Token\");\r\n\r\nHttpClient client = new HttpClient();\r\n\r\nHttpContent content = new StringContent(reqBody);\r\n\r\ncontent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue(\"application\/x-www-form-urlencoded\");\r\n\r\nusing (HttpResponseMessage response = await client.PostAsync(oauthurl, content))\r\n\r\n{\r\n\r\nif (response.IsSuccessStatusCode)\r\n\r\n{\r\n\r\nDataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(PowerBIAccessToken));\r\n\r\nStream json = await response.Content.ReadAsStreamAsync();\r\n\r\n\/\/Gets Access Token\r\n\r\ntoken = (PowerBIAccessToken)serializer.ReadObject(json);\r\n\r\n}\r\n\r\n}\r\n\r\n<\/pre>\n<p>By using the above code we will get access token which will be used while making refresh request in Power BI Service.<\/p>\n<p><strong>2. <\/strong>Refresh Power BI Dataset<\/p>\n<p>For making Power BI refresh request, we need dataset Id which need to be refreshed. We can get dataset id from Power BI service.<\/p>\n<p>Now we will use this dataset id to refresh Power BI dataset. Below is the sample code for the same.<\/p>\n<pre class=\"lang:default decode:true \"> HttpClient refreshClient = new HttpClient();\r\n                    refreshClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(\"Bearer\", powerBIToken.access_token);\r\n                    HttpContent content = new StringContent(\"{'notifyOption':''}\", Encoding.UTF8, \"application\/json\");\r\n                    HttpResponseMessage result = refreshClient.PostAsync(\"https:\/\/api.powerbi.com\/v1.0\/myorg\/datasets\/datasetId\/refreshes\", content).Result;\r\n                    {\r\n                        if (result.IsSuccessStatusCode)\r\n                        {\r\n                            tracingService.Trace(\"DataSet Refresh Request Submitted Successfully.\");\r\n                        }\r\n\r\n                    }<\/pre>\n<p>Now we are using datasets under \u2018My Workspace\u2019 but you can also refresh Power BI dataset from other workspace using this request.<\/p>\n<p>First, we need to call this assembly in the global action.<\/p>\n<p>Next we need a JavaScript code to call the Action (global). Given below is the sample code for the same:<\/p>\n<pre class=\"lang:default decode:true \">let request: any = function ()\r\n\r\n{\r\n\r\nthis.getMetadata = function () {\r\n\r\nreturn {\r\n\r\nboundParameter: null,\r\n\r\nparameterTypes: {\r\n\r\n},\r\n\r\noperationType: 0, \/\/ This is an action. Used '0'.\r\n\r\noperationName: \"new_RefreshPowerBIDataset\"\r\n\r\n};\r\n\r\n};\r\n\r\n};\r\n\r\n\u00a0\r\n\r\nvar tempRequest = new request();\r\n\r\n\/\/Execute global action.\r\n\r\nXrm.WebApi.online.execute(tempRequest);<\/pre>\n<p>Now we will add new ribbon button \u2018Refresh Power BI\u2019 on CRM home page as shown below:<\/p>\n<p><img decoding=\"async\" class=\"aligncenter wp-image-20847\" style=\"border: 1px solid #0a0a0a; padding: 1px; margin: 1px;\" src=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2019\/10\/2Refresh-Power-BI-Dataset-programmatically.png\" alt=\"Refresh Power BI Dataset programmatically\" width=\"942\" height=\"497\" srcset=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2019\/10\/2Refresh-Power-BI-Dataset-programmatically.png 722w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2019\/10\/2Refresh-Power-BI-Dataset-programmatically-300x158.png 300w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2019\/10\/2Refresh-Power-BI-Dataset-programmatically-660x347.png 660w\" sizes=\"(max-width: 942px) 100vw, 942px\" \/><\/p>\n<p style=\"text-align: justify;\">We will call the <strong>JavaScript<\/strong> written on button click event to refresh Power BI dataset. So now when you click on the button, one confirmation Dialog will appear asking confirmation to refresh Power BI Dataset. Given below is the screenshot of the same:<\/p>\n<p><img decoding=\"async\" class=\"aligncenter size-full wp-image-20848\" style=\"border: 1px solid #0a0a0a; padding: 1px; margin: 1px;\" src=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2019\/10\/3Refresh-Power-BI-Dataset-programmatically.png\" alt=\"Refresh Power BI Dataset programmatically\" width=\"1440\" height=\"757\" srcset=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2019\/10\/3Refresh-Power-BI-Dataset-programmatically.png 1440w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2019\/10\/3Refresh-Power-BI-Dataset-programmatically-300x158.png 300w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2019\/10\/3Refresh-Power-BI-Dataset-programmatically-768x404.png 768w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2019\/10\/3Refresh-Power-BI-Dataset-programmatically-1024x538.png 1024w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2019\/10\/3Refresh-Power-BI-Dataset-programmatically-660x347.png 660w\" sizes=\"(max-width: 1440px) 100vw, 1440px\" \/><\/p>\n<p>After you click on \u2018<strong>Ok<\/strong>\u2019, Power BI Dataset will start refreshing and you can see the same in the Power BI Service. Once refresh is completed, Refresh Data will also get changed in the Power BI Service.<\/p>\n<p><img decoding=\"async\" class=\"aligncenter size-full wp-image-20849\" style=\"border: 1px solid #0a0a0a; padding: 1px; margin: 1px;\" src=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2019\/10\/4Refresh-Power-BI-Dataset-programmatically.png\" alt=\"Refresh Power BI Dataset programmatically\" width=\"1440\" height=\"759\" srcset=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2019\/10\/4Refresh-Power-BI-Dataset-programmatically.png 1440w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2019\/10\/4Refresh-Power-BI-Dataset-programmatically-300x158.png 300w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2019\/10\/4Refresh-Power-BI-Dataset-programmatically-768x405.png 768w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2019\/10\/4Refresh-Power-BI-Dataset-programmatically-1024x540.png 1024w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2019\/10\/4Refresh-Power-BI-Dataset-programmatically-660x348.png 660w\" sizes=\"(max-width: 1440px) 100vw, 1440px\" \/><\/p>\n<p><img decoding=\"async\" class=\"aligncenter size-full wp-image-20850\" style=\"border: 1px solid #0a0a0a; padding: 1px; margin: 1px;\" src=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2019\/10\/5Refresh-Power-BI-Dataset-programmatically.png\" alt=\"Refresh Power BI Dataset programmatically\" width=\"1440\" height=\"756\" srcset=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2019\/10\/5Refresh-Power-BI-Dataset-programmatically.png 1440w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2019\/10\/5Refresh-Power-BI-Dataset-programmatically-300x158.png 300w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2019\/10\/5Refresh-Power-BI-Dataset-programmatically-768x403.png 768w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2019\/10\/5Refresh-Power-BI-Dataset-programmatically-1024x538.png 1024w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2019\/10\/5Refresh-Power-BI-Dataset-programmatically-660x347.png 660w\" sizes=\"(max-width: 1440px) 100vw, 1440px\" \/><\/p>\n<p>As seen in the above Screenshot, last refresh date for dataset gets updated in the Power BI Service.<\/p>\n<p>Power BI Refresh request will only trigger if you have set Date Source Credentials for the Power BI Service.<\/p>\n<blockquote><p><strong>Note<\/strong>: For Power BI Free license, Daily refresh limit is set to 8 refreshes per day. So once it reaches the maximum refresh limit, it will not refresh the dataset further.<\/p><\/blockquote>\n<h2><strong>Conclusion<\/strong><\/h2>\n<p>In this way, you can easily refresh Power BI Dataset programmatically from Dynamics 365 CRM.<\/p>\n<p><a href=\"https:\/\/www.inogic.com\/product\/development\/microsoft-power-bi\" target=\"_blank\" rel=\"noopener noreferrer\"><img decoding=\"async\" class=\"aligncenter wp-image-20854\" src=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2019\/10\/PowerApps-1.png\" alt=\"\" width=\"820\" height=\"205\" srcset=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2019\/10\/PowerApps-1.png 800w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2019\/10\/PowerApps-1-300x75.png 300w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2019\/10\/PowerApps-1-768x192.png 768w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2019\/10\/PowerApps-1-660x165.png 660w\" sizes=\"(max-width: 820px) 100vw, 820px\" \/><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction Many of you are aware of manual refresh and schedule refresh feature of Power BI. Schedule refresh have refresh limit set based on the type of Power BI license. Users can add power BI report to CRM via CRM dashboard and Power BI dashboard. Also user\u2019s data gets updated frequently and refreshing Power BI\u2026 <span class=\"read-more\"><a href=\"https:\/\/www.inogic.com\/blog\/2019\/10\/refresh-power-bi-dataset-programmatically-from-dynamics-365-crm\/\">Read More &raquo;<\/a><\/span><\/p>\n","protected":false},"author":13,"featured_media":20860,"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,45],"tags":[1965],"class_list":["post-20846","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-dynamics-365","category-power-bi","tag-dynamics-365-power-bi"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/posts\/20846","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=20846"}],"version-history":[{"count":0,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/posts\/20846\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/media\/20860"}],"wp:attachment":[{"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/media?parent=20846"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/categories?post=20846"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/tags?post=20846"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}