OData Client in CampusNexus

Introduction:

As part of the CampusNexus solution, the Cmc.Nexus.Models.Client project provides a client for accessing the OData service.  Using the client classes, an application will be able to seamlessly execute OData queries and work with the results within the client code.  This blog is a tutorial showing how to use the OData client from within a Windows Application to populate a drop-down on a form.  This example can be used from within a Workflow Activity Designer to do the same.

The Cmc.Nexus.Web site uses OData to execute queries against the CampusVue Query model (Cmc.Nexus.Models).  These queries are generated from JavaScript code and the response is handled by parsing the Json response.   Sample OData request/response from the web site:

Request:

http://localhost/Cmc.Nexus.Web/ds/campusnexus/People?$format=json&$top=50&$filter=(Id+ge+50+and+endswith(FullName,’a’))&$count=true

Response:

While the OData service is easily callable from JavaScript, it would not be recommended to manually create OData queries and handle the Json response from a C# client.   This is where the OData Client for .NET comes it.  It is used to generate C# classes that can be used by a C# client to query the OData service.  The response is then deserialized into client side classes to be referenced directly from the C# client code.


 

Tutorial:

Prerequisites:  The Cmc.Nexus.Web site already contains the OData Web API.  The OData endpoint is available at  localhost/Cmc.Nexus.Web/ds/campusnexus.

  • In order to call the OData directly from a C# client, the client must provide an ApiKey for authorization.  Calling the service without an ApiKey in the header of the request will result in an “Access Denied” exception.  The setting of the ApiKey has been included in the Cmc.Nexus.Models.Client project as part of the Container creation.
  • The client project needs to reference the Cmc.Nexus.Models.Client project as well as Microsoft.OData.Client and it’s dependencies.

This tutorial will use a simple Winforms projects to demonstrate using OData to query a list of Campus Groups.  The Campus Group list will be presented in a drop down list.  When the user selects a Campus Group from the drop down, the Campuses included in the selected group will be listed in a ListBox.

Using the main.sln ($/Core/CampusNexus/Main.sln).  Add a new Winforms Project to the Test Projects solution folder:

Capture

Add the Required References:

Project Reference:
Cmc.Nexus.Models.Client

and via NuGet:
CampusNexus Core
Microsoft OData Client – Version 6.14

 

Capture

 

Capture

Add a ComboBox and a ListBox control to the form, similar to this:

Capture

Add code to configure the container – and to populate the ComboBox and ListBox:

Queries:

For CampusGroups, I wanted to only include the user defined campus groups.  These are identified in the database with a type of “U”.  The query then returns only the Name and Id properties for each Campus Group selected.

For Campuses, the query starts with the CampusLists.  CampusLists contains the list of campuses that belong to a group.  The selection is filtered to return the rows in the CampusLists related to the selected CampusGroup Id.   From CampusLists, the query expands the “Campus” property providing the details for each campus in the list.  This allows for returning the Campus.Name value in the results.

Result:

Capture

 

Note:  The ApiKey used in this example is the default value that is specified in the Cmc.Core assembly.  If the service is configured to use a custom key (as it should be), you will have to check the web.config in the Cmc.Nexus.Web site – and copy the ApiKey setting from the web.config to your client applications app.config file.

 


 

 

References:

  • http://blogs.msdn.com/b/odatateam/archive/2014/03/12/how-to-use-odata-client-code-generator-to-generate-client-side-proxy-class.aspx
  • http://odata.github.io/
  • http://odata.github.io/assets/library-relationship.png

 

 

 

 

Leave a Reply

Skip to toolbar