{"id":2885,"date":"2017-11-23T10:31:41","date_gmt":"2017-11-23T10:31:41","guid":{"rendered":"http:\/\/himbap.com\/blog\/?p=2885"},"modified":"2017-11-28T03:48:16","modified_gmt":"2017-11-28T03:48:16","slug":"virtual-entity-new-way-of-integration-part-2","status":"publish","type":"post","link":"https:\/\/himbap.com\/blog\/?p=2885","title":{"rendered":"Virtual Entity \u2013 New way of integration Part 2"},"content":{"rendered":"<p>This is our second article regarding virtual entity, if you have not checked our earlier article, we will suggest you to <a href=\"https:\/\/himbap.com\/blog\/?p=2860\">check that first here<\/a>.<br \/>\nIn this article we are going to demonstrate about create OData Web API without Entity Framework and hosting it in Azure to consume into Dynamics 365. Let say we have employees data that we want to show into virtual entity, so let\u2019s get started!!<br \/>\nNavigate to Visual Studio and create ASP.NET Web Application project (<strong>File -&gt;New -&gt; select ASP.NET Web Application project under Web template<\/strong>)<br \/>\nNext select Empty template and make sure to select Web API under Add folders:<br \/>\n<a href=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2017\/11\/webapi1.png\"><img decoding=\"async\" loading=\"lazy\" class=\" size-medium wp-image-2886 aligncenter\" src=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2017\/11\/webapi1-300x227.png\" alt=\"webapi1\" width=\"300\" height=\"227\" srcset=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2017\/11\/webapi1-300x227.png 300w, https:\/\/himbap.com\/blog\/wp-content\/uploads\/2017\/11\/webapi1.png 466w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>After that the first thing you need to do is to install NuGet package to get ODATA libraries, so you need to run following command under Package Manager Console (<strong>Tools-&gt;NuGet Package Manager -&gt; Package Manager Console<\/strong>)<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nInstall-Package Microsoft.AspNet.OData\r\n<\/pre>\n<p>And once it is installed, let\u2019s add our Employee class which will hold all properties for Employees under Models folder. We can properties based on our requirement but make sure there is should on provide which should match primary id property in Virtual Entity, so we need to define at least one GUID property which should act as a Key like following:<br \/>\n<a href=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2017\/11\/webapi2.png\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-medium wp-image-2887\" src=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2017\/11\/webapi2-300x162.png\" alt=\"webapi2\" width=\"300\" height=\"162\" srcset=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2017\/11\/webapi2-300x162.png 300w, https:\/\/himbap.com\/blog\/wp-content\/uploads\/2017\/11\/webapi2.png 489w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>Now as we are not using Entity Framework, we need to define our data source class which will hold inline database for our Web APi, so let\u2019s add a new Employeedb class, we can create a new folder under our Web Api project, let\u2019s name it DB and add this class there. We need to add following code under this class:<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nusing ODataWebAPI.Models;\r\nusing System;\r\nusing System.Collections.Generic;\r\nusing System.Linq;\r\nusing System.Web;\r\n\r\nnamespace ODataWebAPI.DB\r\n{\r\n    public class Employeedb\r\n    {\r\n            private static Employeedb instance = null;\r\n            public static Employeedb Instance\r\n            {\r\n                get\r\n                {\r\n                    if (instance == null)\r\n                    {\r\n                        instance = new Employeedb();\r\n                    }\r\n                    return instance;\r\n                }\r\n            }\r\n            public List&lt;Employee&gt; Employees { get; set; }\r\n\r\n            private Employeedb()\r\n            {\r\n                this.Reset();\r\n                this.Initialize();\r\n            }\r\n            public void Reset()\r\n            {\r\n                this.Employees = new List&lt;Employee&gt;();\r\n\r\n            }\r\n            public void Initialize()\r\n            {\r\n                this.Employees.AddRange(new List&lt;Employee&gt;\r\n            {\r\n                new Employee()\r\n                {\r\n                    Empid = Guid.NewGuid(),\r\n                    EName = &quot;Arnav&quot;,\r\n                    Department=&quot;Dynamics 365\/CRM&quot;\r\n\r\n                },\r\n                new Employee()\r\n                {\r\n                    Empid = Guid.NewGuid(),\r\n                    EName = &quot;Diksha&quot;,\r\n                    Department=&quot;Dynamics 365\/CRM&quot;\r\n\r\n                },\r\n                new Employee()\r\n                {\r\n                    Empid = Guid.NewGuid(),\r\n                    EName = &quot;Anil&quot;,\r\n                    Department = &quot;Dynamics 365 Operation&quot;\r\n                },\r\n                new Employee()\r\n                {\r\n                    Empid = Guid.NewGuid(),\r\n                    EName = &quot;Sunil&quot;,\r\n                    Department = &quot;Dynamics 365 Operation&quot;\r\n                }\r\n            });\r\n            }\r\n        }\r\n    }\r\n<\/pre>\n<p>Now let\u2019s add controller for our Web Api, we are going to implement just one method Get in our controller, to add controller follow <strong>Right Click on Controller-&gt;Add-&gt;Controller<\/strong>.<br \/>\n<a href=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2017\/11\/webapi3.png\"><img decoding=\"async\" loading=\"lazy\" class=\" size-medium wp-image-2888 aligncenter\" src=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2017\/11\/webapi3-300x182.png\" alt=\"webapi3\" width=\"300\" height=\"182\" srcset=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2017\/11\/webapi3-300x182.png 300w, https:\/\/himbap.com\/blog\/wp-content\/uploads\/2017\/11\/webapi3-624x378.png 624w, https:\/\/himbap.com\/blog\/wp-content\/uploads\/2017\/11\/webapi3.png 693w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>Under add controller dialog select our employee class and click on Add.<br \/>\nDelete all existing code and just add following code:<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nusing System;\r\nusing System.Collections.Generic;\r\nusing System.Data;\r\nusing System.Linq;\r\nusing System.Net;\r\nusing System.Net.Http;\r\nusing System.Web.Http;\r\nusing System.Web.Http.ModelBinding;\r\nusing System.Web.Http.OData;\r\nusing System.Web.Http.OData.Query;\r\nusing System.Web.Http.OData.Routing;\r\nusing ODataWebAPI.Models;\r\nusing Microsoft.Data.OData;\r\nusing ODataWebAPI.DB;\r\n\r\nnamespace ODataWebAPI.Controllers\r\n{\r\n    \/*\r\n    The WebApiConfig class may require additional changes to add a route for this controller. Merge these statements into the Register method of the WebApiConfig class as applicable. Note that OData URLs are case sensitive.\r\n\r\n    using System.Web.Http.OData.Builder;\r\n    using System.Web.Http.OData.Extensions;\r\n    using ODataWebAPI.Models;\r\n    ODataConventionModelBuilder builder = new ODataConventionModelBuilder();\r\n    builder.EntitySet&lt;Employee&gt;(&quot;Employees&quot;);\r\n    config.Routes.MapODataServiceRoute(&quot;odata&quot;, &quot;odata&quot;, builder.GetEdmModel());\r\n    *\/\r\n    public class EmployeesController : ODataController\r\n    {\r\n        public IHttpActionResult Get()\r\n        {\r\n            return Ok(Employeedb.Instance.Employees.AsQueryable());\r\n        }\r\n    }\r\n}\r\n<\/pre>\n<p>Now finally we need to set routing for our Web Api, so let\u2019s add following code under our WebApiConfig.cs file.<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nusing System;\r\nusing Microsoft.OData.Edm;\r\nusing System.Web.Http;\r\nusing System.Web.OData.Batch;\r\nusing System.Web.OData.Builder;\r\nusing System.Web.OData.Extensions;\r\nusing ODataWebAPI.Models;\r\n\r\nnamespace ODataWebAPI\r\n{\r\n    public static class WebApiConfig\r\n    {\r\n        public static void Register(HttpConfiguration config)\r\n        {\r\n            config.MapODataServiceRoute(&quot;odata&quot;, null, GetEdmModel());\r\n            config.Count().Filter().OrderBy().Expand().Select().MaxTop(null);\r\n            \/\/config.MapODataServiceRoute(&quot;odata&quot;, null, GetEdmModel(), new DefaultODataBatchHandler(GlobalConfiguration.DefaultServer));\r\n            config.EnsureInitialized();\r\n        }\r\n        private static IEdmModel GetEdmModel()\r\n        {\r\n            ODataConventionModelBuilder builder = new ODataConventionModelBuilder();\r\n            builder.Namespace = &quot;Demos&quot;;\r\n            builder.ContainerName = &quot;DefaultContainer&quot;;\r\n            builder.EntitySet&lt;Employee&gt;(&quot;Employees&quot;);\r\n\r\n            var edmModel = builder.GetEdmModel();\r\n            return edmModel;\r\n        }\r\n    }\r\n}\r\n<\/pre>\n<p>Now we are ready to build our project and deploy it to Azure, if you don\u2019t have Azure subscription, you can setup a quick Azure <a href=\"https:\/\/azure.microsoft.com\/en-in\/offers\/ms-azr-0044p\/\">trial from here<\/a><br \/>\nRight click on our project and select Publish under popup menu. We need to select Microsoft Azure App Service, click on Publish button, if you are new to Azure, you can access rich Azure resource <a href=\"https:\/\/azure.microsoft.com\/en-in\/resources\/\">here to get started<\/a>. If you are deploying to Azure for the first time it will ask you for signin use your windows Azure credentials. Once logged in, you can select your subscription, Resource Group, AppServicePlan and finally click on Create.<br \/>\n<a href=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2017\/11\/webapi4.png\"><img decoding=\"async\" loading=\"lazy\" class=\" size-medium wp-image-2889 aligncenter\" src=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2017\/11\/webapi4-300x193.png\" alt=\"webapi4\" width=\"300\" height=\"193\" srcset=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2017\/11\/webapi4-300x193.png 300w, https:\/\/himbap.com\/blog\/wp-content\/uploads\/2017\/11\/webapi4-624x402.png 624w, https:\/\/himbap.com\/blog\/wp-content\/uploads\/2017\/11\/webapi4.png 782w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>And once deployment is over make sure to browse below URL to get metadata and see if you are able to get it correctly.<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nYourAzureURL\/$metadata\r\n<\/pre>\n<p>You should be able to see metadata like below:<br \/>\n<a href=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2017\/11\/virtual6.png\"><img decoding=\"async\" loading=\"lazy\" class=\" size-medium wp-image-2900 aligncenter\" src=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2017\/11\/virtual6-300x142.png\" alt=\"virtual6\" width=\"300\" height=\"142\" srcset=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2017\/11\/virtual6-300x142.png 300w, https:\/\/himbap.com\/blog\/wp-content\/uploads\/2017\/11\/virtual6-624x296.png 624w, https:\/\/himbap.com\/blog\/wp-content\/uploads\/2017\/11\/virtual6.png 751w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><br \/>\nKeep not of highlighted information. After that we can try to browse below URL to see if we are getting employees information correctly.<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nYourAzureURL\/Employees\r\n<\/pre>\n<p>After that we can create Virtual Entity data source by navigating to <strong>Settings-&gt;Administration-&gt; Virtual Entity Data sources<\/strong> and selecting <strong>OData V4 Data Provider<\/strong>.<br \/>\n<a href=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2017\/11\/virtual7.png\"><img decoding=\"async\" loading=\"lazy\" class=\" size-medium wp-image-2901 aligncenter\" src=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2017\/11\/virtual7-300x172.png\" alt=\"virtual7\" width=\"300\" height=\"172\" srcset=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2017\/11\/virtual7-300x172.png 300w, https:\/\/himbap.com\/blog\/wp-content\/uploads\/2017\/11\/virtual7-624x359.png 624w, https:\/\/himbap.com\/blog\/wp-content\/uploads\/2017\/11\/virtual7.png 642w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><br \/>\nAnd now we can create Employee Virtual Entity, using our virtual entity data source, like below:<br \/>\n<a href=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2017\/11\/virtual8.png\"><img decoding=\"async\" loading=\"lazy\" class=\" size-medium wp-image-2902 aligncenter\" src=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2017\/11\/virtual8-300x141.png\" alt=\"virtual8\" width=\"300\" height=\"141\" srcset=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2017\/11\/virtual8-300x141.png 300w, https:\/\/himbap.com\/blog\/wp-content\/uploads\/2017\/11\/virtual8-624x293.png 624w, https:\/\/himbap.com\/blog\/wp-content\/uploads\/2017\/11\/virtual8.png 799w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>Make sure to add External Name to every field based on our Web Api field. Finally we can add our field to view and after publish we should be able to see our Virtual Entity records like below:<br \/>\n<a href=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2017\/11\/virtual9.png\"><img decoding=\"async\" loading=\"lazy\" class=\" size-medium wp-image-2903 aligncenter\" src=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2017\/11\/virtual9-300x200.png\" alt=\"virtual9\" width=\"300\" height=\"200\" srcset=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2017\/11\/virtual9-300x200.png 300w, https:\/\/himbap.com\/blog\/wp-content\/uploads\/2017\/11\/virtual9.png 556w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>You can access complete code for this OData Web Api from <a href=\"https:\/\/github.com\/himbap\/ODataWebAPI\">GitHub Here<\/a>.<\/p>\n<p>Reference Post: http:\/\/www.odata.org\/blog\/how-to-use-web-api-odata-to-build-an-odata-v4-service-without-entity-framework\/<\/p>\n<p>Stay Tuned for more Dynamics 365 Updates !!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This is our second article regarding virtual entity, if you have not checked our earlier article, we will suggest you to check that first here. In this article we are going to demonstrate about create OData Web API without Entity Framework and hosting it in Azure to consume into Dynamics 365. Let say we have employees data that we want&#8230; <a href=\"https:\/\/himbap.com\/blog\/?p=2885\">Read more &raquo;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[491,21,402,492],"tags":[495,494,496,488,487,493],"_links":{"self":[{"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2885"}],"collection":[{"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2885"}],"version-history":[{"count":7,"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2885\/revisions"}],"predecessor-version":[{"id":2910,"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2885\/revisions\/2910"}],"wp:attachment":[{"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2885"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2885"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2885"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}