{"id":14077,"date":"2019-01-16T12:34:36","date_gmt":"2019-01-16T12:34:36","guid":{"rendered":"https:\/\/www.inogic.com\/blog\/?p=14077"},"modified":"2022-09-02T16:40:33","modified_gmt":"2022-09-02T11:10:33","slug":"using-angular-dynamics-365-crm-part-ii","status":"publish","type":"post","link":"https:\/\/www.inogic.com\/blog\/2019\/01\/using-angular-dynamics-365-crm-part-ii\/","title":{"rendered":"Using Angular in Dynamics 365 CRM \u2013 Part II"},"content":{"rendered":"<h2><strong>Introduction<\/strong><\/h2>\n<p style=\"text-align: justify;\">In our <a href=\"https:\/\/www.inogic.com\/blog\/2019\/01\/using-angular-in-dynamics-365-part-i\/\" target=\"_blank\" rel=\"noopener noreferrer\">previous<\/a> blog, we have seen how to create and deploy a simple hello world Angular application in Dynamics 365. In this blog, we will see how to work with CRM Client API, CRM Web API and how to use JavaScript libraries.<\/p>\n<h2><strong>Work with Client API in Angular Application<\/strong><\/h2>\n<p style=\"text-align: justify;\">The first thing that we would need to do is, add the reference of <strong>ClientGlobalContext.js.aspx.\u00a0<\/strong>in index.html. Refer a screenshot below.<\/p>\n<p><img decoding=\"async\" class=\"aligncenter size-full wp-image-14078\" style=\"border: 1px solid #0a0a0a; padding: 1px; margin: 1px;\" src=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2019\/01\/1Angular-in-Dynamics-365-CRM.png\" alt=\"Angular in Dynamics 365 CRM\" width=\"1082\" height=\"453\" \/><\/p>\n<p style=\"text-align: justify;\">Now let\u2019s use ClientGlobalContext.js.aspx to use some Client API. Open app.component.ts and write a code to get the logged-in user as shown below,<\/p>\n<p><img decoding=\"async\" class=\"aligncenter wp-image-14079\" style=\"border: 1px solid #0a0a0a; padding: 1px; margin: 1px;\" src=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2019\/01\/2Angular-in-Dynamics-365-CRM.png\" alt=\"Angular in Dynamics 365 CRM\" width=\"832\" height=\"345\" \/><\/p>\n<p>When building the application using ng build you will get the below error,<\/p>\n<p><img decoding=\"async\" class=\"aligncenter wp-image-14080\" style=\"border: 1px solid #0a0a0a; padding: 1px; margin: 1px;\" src=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2019\/01\/3Angular-in-Dynamics-365-CRM.png\" alt=\"Angular in Dynamics 365 CRM\" width=\"821\" height=\"102\" \/><\/p>\n<p>To fix this install <a href=\"https:\/\/www.npmjs.com\/package\/@types\/xrm\" target=\"_blank\" rel=\"noopener noreferrer\">@types\/xrm<\/a>. Open terminal and type following command, npm<strong> install &#8211;save @types\/<\/strong>xrm<\/p>\n<p>You will then see the xrm type get added in your node_modules -&gt; @types folder.<\/p>\n<p style=\"padding-left: 90px;\"><img decoding=\"async\" class=\"aligncenter size-full wp-image-14081\" style=\"border: 1px solid #0a0a0a; padding: 1px; margin: 1px;\" src=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2019\/01\/4Angular-in-Dynamics-365-CRM.png\" alt=\"Angular in Dynamics 365 CRM\" width=\"336\" height=\"460\" \/><\/p>\n<p>Add type \u2018xrm\u2019 in \u201ctypes\u201d of tsconfig.app.json file as shown below,<\/p>\n<p><img decoding=\"async\" class=\"aligncenter size-full wp-image-14082\" style=\"border: 1px solid #0a0a0a; padding: 1px; margin: 1px;\" src=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2019\/01\/5Angular-in-Dynamics-365-CRM.png\" alt=\"Angular in Dynamics 365 CRM\" width=\"877\" height=\"550\" \/><\/p>\n<p style=\"text-align: justify;\">Now build the application and update the main.js file in Dynamics 365. This should work now and lets give you the name of logged in user.<\/p>\n<p>So this way you can use Xrm object in Angular application.<\/p>\n<h2><strong>Work with CRM Web API in Angular Application<\/strong><\/h2>\n<p style=\"text-align: justify;\">If you are on Dynamics 365 version 9.x and above then you can directly use <a href=\"https:\/\/docs.microsoft.com\/en-us\/dynamics365\/customer-engagement\/developer\/clientapi\/reference\/xrm-webapi\" target=\"_blank\" rel=\"noopener noreferrer\">Xrm.WebApi<\/a> to perform CRUD operations on CRM data. But if you are on 8.x and below then you would need to add reference of Web API in your Angular Application.<\/p>\n<p>Run \u201cnpm install \u2013save-dev xrm-webapi\u201d command in terminal. This will add xrm-webapi in your current project\u2019s node_modules. Refresh the project folder if xrm-webapi folder not showing in the node-modules folder.<\/p>\n<p style=\"padding-left: 90px;\"><img decoding=\"async\" class=\"aligncenter size-full wp-image-14083\" style=\"border: 1px solid #0a0a0a; padding: 1px; margin: 1px;\" src=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2019\/01\/6Angular-in-Dynamics-365-CRM.png\" alt=\"Angular in Dynamics 365 CRM\" width=\"340\" height=\"291\" \/><\/p>\n<p style=\"text-align: justify;\">To use this import xrm-webapi in your component or in service of angular application. We recommend to initialize or add the reference of web api in service as it may be reused in several parts of Angular Application.<\/p>\n<p>Below is how I have imported the xrm-webapi in app.service.ts.<\/p>\n<p>\/\/app.service.ts<\/p>\n<p>import { Injectable } from &#8216;@angular\/core&#8217;;<\/p>\n<p>import { WebApi } from &#8220;xrm-webapi&#8221;;<\/p>\n<p>@Injectable({<\/p>\n<p>providedIn: &#8216;root&#8217;<\/p>\n<p>})<\/p>\n<p>export class AppService {<\/p>\n<p>webApi: WebApi;<\/p>\n<p>constructor() {<\/p>\n<p>this.webApi = new WebApi(&#8220;9.1&#8221;);<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p><img decoding=\"async\" class=\"aligncenter wp-image-14084\" style=\"border: 1px solid #0a0a0a; padding: 1px; margin: 1px;\" src=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2019\/01\/7Angular-in-Dynamics-365-CRM.png\" alt=\"Angular in Dynamics 365 CRM\" width=\"820\" height=\"462\" \/><\/p>\n<p>Below is how we have used the functions of xrm-webapi in app.component.ts.<\/p>\n<p>\/\/app.component.ts<\/p>\n<p>import { Component } from &#8216;@angular\/core&#8217;;<\/p>\n<p>import { AppService } from &#8220;.\/app.service&#8221;;<\/p>\n<p>@Component({<\/p>\n<p>selector: &#8216;app-root&#8217;,<\/p>\n<p>templateUrl: &#8216;.\/app.component.html&#8217;,<\/p>\n<p>styleUrls: [&#8216;.\/app.component.css&#8217;]\n<p>})<\/p>\n<p>export class AppComponent {<\/p>\n<p>title = &#8216;first-crm-angular-app&#8217;;<\/p>\n<p>loggedInUserName: string;<\/p>\n<p>constructor(public appService: AppService) {<\/p>\n<p>}<\/p>\n<p>ngOnInit() {<\/p>\n<p>let context = Xrm.Utility.getGlobalContext();<\/p>\n<p>this.loggedInUserName = context.userSettings.userName;<\/p>\n<p>console.log(&#8220;Logged in user: &#8221; + this.loggedInUserName);<\/p>\n<p>\/\/retrieve Accounts<\/p>\n<p>this.appService.webApi.retrieveMultiple(&#8220;accounts&#8221;).then(response =&gt; {<\/p>\n<p>console.log(&#8220;Accounts: &#8221; + response.value);<\/p>\n<p>},<\/p>\n<p>error =&gt; {<\/p>\n<p>console.log(&#8220;Error: &#8221; + error.message);<\/p>\n<p>});<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p><img decoding=\"async\" class=\"aligncenter size-full wp-image-14085\" style=\"border: 1px solid #0a0a0a; padding: 1px; margin: 1px;\" src=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2019\/01\/8Angular-in-Dynamics-365-CRM.png\" alt=\"Angular in Dynamics 365 CRM\" width=\"974\" height=\"596\" \/><\/p>\n<p style=\"text-align: justify;\">Build application and upload main.js and vendor.js file in Dynamics 365. Refresh the page and check the console. The Web API should work.<\/p>\n<p><img decoding=\"async\" class=\"aligncenter wp-image-14086\" style=\"border: 1px solid #0a0a0a; padding: 1px; margin: 1px;\" src=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2019\/01\/9Angular-in-Dynamics-365-CRM.png\" alt=\"Angular in Dynamics 365 CRM\" width=\"823\" height=\"877\" \/><\/p>\n<h2><strong>How to use JavaScript library in Angular Application<\/strong><\/h2>\n<p style=\"text-align: justify;\">Suppose you have util.js a common JavaScript library in your system and you want to use it in your Angular application. This is also possible, there are several ways to achieve this. Here we will see the one of the way to use a JavaScript library.\u00a0 You would need to add the reference of your JavaScript library in index.html and to able add this reference in index.html you would need to this util.js file in your Angular Application project directory with structure.<\/p>\n<p>Here is how util.js web resource is created in CRM,<\/p>\n<p>new_scripts\/shared\/util.js<\/p>\n<p><img decoding=\"async\" class=\"aligncenter wp-image-14087\" style=\"border: 1px solid #0a0a0a; padding: 1px; margin: 1px;\" src=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2019\/01\/10Angular-in-Dynamics-365-CRM.png\" alt=\"Angular in Dynamics 365 CRM\" width=\"820\" height=\"683\" \/><\/p>\n<p>Here is how util.js added into Angular Application,<\/p>\n<p style=\"padding-left: 60px;\"><img decoding=\"async\" class=\"aligncenter size-full wp-image-14088\" style=\"border: 1px solid #0a0a0a; padding: 1px; margin: 1px;\" src=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2019\/01\/11Angular-in-Dynamics-365-CRM.png\" alt=\"Angular in Dynamics 365 CRM\" width=\"542\" height=\"374\" \/><\/p>\n<p>Here is how it being referenced in index.html,<\/p>\n<p><img decoding=\"async\" class=\"aligncenter size-full wp-image-14089\" style=\"border: 1px solid #0a0a0a; padding: 1px; margin: 1px;\" src=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2019\/01\/12Angular-in-Dynamics-365-CRM.png\" alt=\"Angular in Dynamics 365 CRM\" width=\"1059\" height=\"337\" \/><\/p>\n<p>Finally, here is how it is being used in component,<\/p>\n<p>\/\/app.component.ts<\/p>\n<p>import { Component } from &#8216;@angular\/core&#8217;;<\/p>\n<p>declare function getText(): string;<\/p>\n<p>@Component({<\/p>\n<p>selector: &#8216;app-root&#8217;,<\/p>\n<p>templateUrl: &#8216;.\/app.component.html&#8217;,<\/p>\n<p>styleUrls: [&#8216;.\/app.component.css&#8217;]\n<p>})<\/p>\n<p>export class AppComponent {<\/p>\n<p>title = &#8216;first-crm-angular-app&#8217;;<\/p>\n<p>constructor() {<\/p>\n<p>}<\/p>\n<p>ngOnInit() {<\/p>\n<p>\/\/call JavaScript Library Function<\/p>\n<p>let text = getText();<\/p>\n<p>console.log(&#8220;JavaScript Function Result: &#8221; + text);<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<h2><strong>Conclusion<\/strong><\/h2>\n<p style=\"text-align: justify;\">In this blog, we have seen how to work with CRM Client API, CRM Web API and how to use JavaScript libraries. In the <a href=\"https:\/\/www.inogic.com\/blog\/2019\/01\/using-angular-in-dynamics-365-crm-part-3\/\" target=\"_blank\" rel=\"noopener noreferrer\">next blog<\/a>, we will see the development build and production build and will see how to increase the development speed as every time when we make any changes into our application we need to upload and publish the Web Resources in CRM to see the changes.<\/p>\n<h2 style=\"text-align: left;\"><div class=\"su-heading su-heading-style-default su-heading-align-center\" id=\"\" style=\"font-size:15px;margin-bottom:5px\"><div class=\"su-heading-inner\">Generate Your Own New Leads Within Microsoft Dynamics 365 CRM<\/div><\/div><\/h2>\n<p><em>Contact us for a <a href=\"https:\/\/www.maplytics.com\/maplytics-download\/?utm_source=highvisits&amp;utm_medium=technicalblog&amp;utm_campaign=hMaplytics\" target=\"_blank\" rel=\"noopener\">demo<\/a> to know more about how <a href=\"https:\/\/www.maplytics.com\/?utm_source=highvisits&amp;utm_medium=technicalblog&amp;utm_campaign=hMaplytics\" target=\"_blank\" rel=\"noopener\">Maplytics<\/a> can help you to generate new leads from within Microsoft Dynamics 365 CRM.<\/em><\/p>\n<p><em><a href=\"https:\/\/www.maplytics.com\/?utm_source=highvisits&amp;utm_medium=technicalblog&amp;utm_campaign=hMaplytics\" target=\"_blank\" rel=\"noopener\">Maplytics<\/a> is a 5-star rated, preferred business app on the <a href=\"https:\/\/appsource.microsoft.com\/en-us\/product\/dynamics-365\/inogic.f6f3c73f-29de-4fa8-a396-87ea8a07b6c4?tab=Overview\" target=\"_blank\" rel=\"noopener\">Microsoft AppSource<\/a> that is Certified for Microsoft Dynamics 365 (CfMD) and comes with powerful features like Appointment Planning, Sales Routing, Territory Management, Heat Maps, Geo-analytical Dashboards and more that empower organizations to add more value to their CRM data, improve sales &amp; service processes, and achieve high ROI.<\/em><\/p>\n<p><em>Get your <a href=\"https:\/\/www.maplytics.com\/maplytics-download\/?utm_source=highvisits&amp;utm_medium=technicalblog&amp;utm_campaign=hMaplytics\" target=\"_blank\" rel=\"noopener\">free trial<\/a> from our Website or <a href=\"https:\/\/appsource.microsoft.com\/en-us\/product\/dynamics-365\/inogic.f6f3c73f-29de-4fa8-a396-87ea8a07b6c4?tab=Overview\" target=\"_blank\" rel=\"noopener\">Microsoft AppSource<\/a>!<\/em><\/p>\n<p><em>&#8216;If data is the new oil, location intelligence is ??\u201d<\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction In our previous blog, we have seen how to create and deploy a simple hello world Angular application in Dynamics 365. In this blog, we will see how to work with CRM Client API, CRM Web API and how to use JavaScript libraries. Work with Client API in Angular Application The first thing that\u2026 <span class=\"read-more\"><a href=\"https:\/\/www.inogic.com\/blog\/2019\/01\/using-angular-dynamics-365-crm-part-ii\/\">Read More &raquo;<\/a><\/span><\/p>\n","protected":false},"author":13,"featured_media":14091,"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":[3,16,19],"tags":[129,130],"class_list":["post-14077","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-angular","category-dynamics-365","category-dynamics-crm","tag-angular-dynamics-365","tag-angular-dynamics-365-crm"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/posts\/14077","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=14077"}],"version-history":[{"count":0,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/posts\/14077\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/media\/14091"}],"wp:attachment":[{"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/media?parent=14077"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/categories?post=14077"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/tags?post=14077"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}