Introduction
Open type is a structured type which contains dynamic properties. It allows the client to add properties to the instance of object or class dynamically by specifying unique name and value. Our data model becomes more flexible by using open type. The value of these dynamic properties can be anything; i.e., primitive type, complex type, enumeration type, collection of any type. In this article, we will learn how to use open type in Web API with OData.
Example
My article "An OData V4 Endpoint with ASP.NET Web API 2.2" explains how to configure OData endpoint with ASP.NET Web API. I have taken the same example here. Now to support the Open Type just a little bit changes in model class and model new definition as following.
- namespace WebAPITest
- {
- using System.Collections.Generic;
- public class TestData
- {
- public int Id { get; set; }
- public string Name { get; set; }
- public string Role { get; set; }
- public IDictionary<string, object> DynamicProperties { get; set; }
- }
- }
Now I run this application and try to get service Meta data.
URL: http://localhost:24367/$metadata
Output:

The metadata does not include the Dynamic Properties property for the TestData class. The metadata only includes its declared properties. EntityType ‘s OpenType property is set to true.
Here to check dynamic properties, I have added POST method in to the OData controller. To test the application, I am using Telerik's Fiddler. It’s free. Using Fiddler, we can analyze the input / output of URL and also able to POST data without any user interface.
- public HttpResponseMessage Post([FromBody]TestData entity)
- {
- int id = DALTestData.Testdata.Max(p => p.Id) + 1;
- entity.Id = id;
- DALTestData.Testdata.Add(entity);
- return Request.CreateResponse(HttpStatusCode.OK, entity);
- }
Posting the dynamic property using Fiddler

Here I set a break point to the POST method in the controller. We can see that additional properties are added to the dictionary.

Same as using the GET request, we can get the dynamic property at client end. In this example, I have assigned the value of DynamicProperties of TestData class in Static data source.
- namespace WebAPITest
- {
- using System.Collections.Generic;
- public class DALTestData
- {
- public static List<TestData> Testdata = new List<TestData>();
- static DALTestData()
- {
- Testdata.Add(new TestData { Id = 1, Name = "Jignesh", Role = "Project Manager", DynamicProperties = new Dictionary<string, object>() { { "Test1", "Test1" } } });
- …..
- …..
- }
- }
- }
- public HttpResponseMessage Get([FromODataUri]int key)
- {
- TestData data = DALTestData.Testdata.Where(k => k.Id == key).FirstOrDefault();
- if (data == null)
- {
- return Request.CreateResponse(HttpStatusCode.NotFound);
- }
- return Request.CreateResponse(HttpStatusCode.OK, data);
- }
Output:

Summary
In this way OData enables us to add dynamic properties in to our model class. This feature is very important when client wants to send some additional data other than property of model.

Mohammed FakhriPosted Jul 11, 2018, 8:05 PM
Does not support untyped value in non-open type. getting this error
vipul manekarPosted May 10, 2018, 8:14 AM
Any solution/comment on this please: https://stackoverflow.com/questions/50265218/expand-not-working-associate-child-records-not-returned-in-result
Sujesh Koothumadam ChandranPosted Dec 19, 2017, 10:27 AM
HI, Can I pass an object as the value of the dynamic properties?
Renish PatelPosted May 9, 2016, 4:02 PM
Nice Article.Good one
Neeraj KumarPosted May 7, 2016, 11:40 AM
Nice article...
Kuppurasu NagarajPosted May 6, 2016, 9:15 AM
Nice Sharing..
Sr KarthigaPosted May 6, 2016, 3:39 AM
good one
NitinPosted May 6, 2016, 3:26 AM
good one
Anbu ManiPosted May 6, 2016, 2:35 AM
Nice