Virtual Entity - New Way Of Integration - Part Two

This is my second article related to Virtual Entity. If you have not checked the earlier article, I would suggest you check that first here.

In this article, we are going to demonstrate how to create OData Web API without Entity Framework and host it on Azure to consume into Dynamics 365. Let's say, we have employees' data that we want to show into a Virtual Entity; so let’s get started!

Navigate to Visual Studio and create an ASP.NET web application project (File ->New -> select ASP.NET Web Application project under Web template).

Next, select an Empty template and make sure to select Web API under Add folders.

 

After that, the first thing you need to do is to install NuGet package to get the ODATA libraries, so you need to run the following command under Package Manager Console (Tools->NuGet Package Manager -> Package Manager Console).
  1. Install-Package Microsoft.AspNet.OData
And once it is installed, let’s add our Employee class which will hold all the properties for Employees under Models folder. We can add properties based on our requirement but make sure they 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.

 

Now, as we are not using Entity Framework, we need to define our data source class which will hold an in-line database for our Web API. Let’s add a new Employeedb class. We can create a new folder under our Web API project. Let us name it DB and add this class there. We need to add the following code to this class.
  1. using ODataWebAPI.Models;  
  2. using System;  
  3. using System.Collections.Generic;  
  4. using System.Linq;  
  5. using System.Web;  
  6.    
  7. namespace ODataWebAPI.DB  
  8. {  
  9.     public class Employeedb  
  10.     {  
  11.             private static Employeedb instance = null;  
  12.             public static Employeedb Instance  
  13.             {  
  14.                 get  
  15.                 {  
  16.                     if (instance == null)  
  17.                     {  
  18.                         instance = new Employeedb();  
  19.                     }  
  20.                     return instance;  
  21.                 }  
  22.             }  
  23.             public List<Employee> Employees { getset; }  
  24.    
  25.             private Employeedb()  
  26.             {  
  27.                 this.Reset();  
  28.                 this.Initialize();  
  29.             }  
  30.             public void Reset()  
  31.             {  
  32.                 this.Employees = new List<Employee>();  
  33.    
  34.             }  
  35.             public void Initialize()  
  36.             {  
  37.                 this.Employees.AddRange(new List<Employee>  
  38.             {  
  39.                 new Employee()  
  40.                 {  
  41.                     Empid = Guid.NewGuid(),  
  42.                     EName = "Arnav",  
  43.                     Department="Dynamics 365/CRM"  
  44.    
  45.                 },  
  46.                 new Employee()  
  47.                 {  
  48.                     Empid = Guid.NewGuid(),  
  49.                     EName = "Diksha",  
  50.                     Department="Dynamics 365/CRM"  
  51.    
  52.                 },  
  53.                 new Employee()  
  54.                 {  
  55.                     Empid = Guid.NewGuid(),  
  56.                     EName = "Anil",  
  57.                     Department = "Dynamics 365 Operation"  
  58.                 },  
  59.                 new Employee()  
  60.                 {  
  61.                     Empid = Guid.NewGuid(),  
  62.                     EName = "Sunil",  
  63.                     Department = "Dynamics 365 Operation"  
  64.                 }  
  65.             });  
  66.             }  
  67.         }  
  68.     }  

Now, let’s add a Controller for our Web API. We are going to implement just one method, i.e., Get in our controller. To add the Controller, right-click on Controller->Add->Controller.

 

In the "Add Controller" dialog, select the employee class and click on "Add".

Delete all the existing code and just add the following code.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Data;  
  4. using System.Linq;  
  5. using System.Net;  
  6. using System.Net.Http;  
  7. using System.Web.Http;  
  8. using System.Web.Http.ModelBinding;  
  9. using System.Web.Http.OData;  
  10. using System.Web.Http.OData.Query;  
  11. using System.Web.Http.OData.Routing;  
  12. using ODataWebAPI.Models;  
  13. using Microsoft.Data.OData;  
  14. using ODataWebAPI.DB;  
  15.    
  16. namespace ODataWebAPI.Controllers  
  17. {  
  18.     /* 
  19.     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. 
  20.   
  21.     using System.Web.Http.OData.Builder; 
  22.     using System.Web.Http.OData.Extensions; 
  23.     using ODataWebAPI.Models; 
  24.     ODataConventionModelBuilder builder = new ODataConventionModelBuilder(); 
  25.     builder.EntitySet<Employee>("Employees"); 
  26.     config.Routes.MapODataServiceRoute("odata", "odata", builder.GetEdmModel()); 
  27.     */  
  28.     public class EmployeesController : ODataController  
  29.     {  
  30.         public IHttpActionResult Get()  
  31.         {  
  32.             return Ok(Employeedb.Instance.Employees.AsQueryable());  
  33.         }  
  34.     }  
  35. }  

Now, finally, we need to set routing for our Web API. So, let’s add the following code to our WebApiConfig.cs file.

  1. using System;  
  2. using Microsoft.OData.Edm;  
  3. using System.Web.Http;  
  4. using System.Web.OData.Batch;  
  5. using System.Web.OData.Builder;  
  6. using System.Web.OData.Extensions;  
  7. using ODataWebAPI.Models;  
  8.    
  9. namespace ODataWebAPI  
  10. {  
  11.     public static class WebApiConfig  
  12.     {  
  13.         public static void Register(HttpConfiguration config)  
  14.         {  
  15.             config.MapODataServiceRoute("odata"null, GetEdmModel());  
  16.             config.Count().Filter().OrderBy().Expand().Select().MaxTop(null);  
  17.             //config.MapODataServiceRoute("odata", null, GetEdmModel(), new DefaultODataBatchHandler(GlobalConfiguration.DefaultServer));  
  18.             config.EnsureInitialized();  
  19.         }  
  20.         private static IEdmModel GetEdmModel()  
  21.         {  
  22.             ODataConventionModelBuilder builder = new ODataConventionModelBuilder();  
  23.             builder.Namespace = "Demos";  
  24.             builder.ContainerName = "DefaultContainer";  
  25.             builder.EntitySet<Employee>("Employees");  
  26.    
  27.             var edmModel = builder.GetEdmModel();  
  28.             return edmModel;  
  29.         }  
  30.     }  
  31. }  

Now, we are ready to build our project and deploy it to Azure. If you don’t have Azure subscription, you can setup a quick Azure trial from here.

Right-click on your project and select "Publish" from the popup menu. We need to select Microsoft Azure App Service. Then, click on Publish button. If you are new to Azure, you can access rich Azure resource here to get started.
 
If you are deploying to Azure for the first time, it will ask you for signing in; use your Windows Azure credentials. Once logged in, you can select your subscription, Resource Group, AppServicePlan, and finally, click on "Create".

 

And once the deployment is over, make sure to browse the below URL to get metadata and see if you are able to get it correctly.
  1. YourAzureURL/$metadata
You should be able to see metadata like below.

 
Keep note of highlighted information. After that, we can try to browse the below URL to see if we are getting employees' information correctly.
  1. YourAzureURL/Employees
We create the Virtual Entity data source by navigating to Settings->Administration-> Virtual Entity Data sources and selecting OData V4 Data Provider.



And now, we can create Employee Virtual Entity using our virtual entity data source, like below.

 

Make sure to add External Name to every field based on our Web API field. Finally, we can add our field to the View and after publishing, we should be able to see our Virtual Entity records like below.

 

You can access the complete code for this OData Web API from GitHub here.

Reference Post

http://www.odata.org/blog/how-to-use-web-api-odata-to-build-an-odata-v4-service-without-entity-framework/

Stay tuned for more Dynamics 365 Updates !!