{"id":313,"date":"2014-04-29T09:17:00","date_gmt":"2014-04-29T09:17:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/odatateam\/2014\/04\/29\/tutorial-sample-how-to-use-open-type-in-odata\/"},"modified":"2020-01-07T07:35:52","modified_gmt":"2020-01-07T14:35:52","slug":"tutorial-sample-how-to-use-open-type-in-odata","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/odata\/tutorial-sample-how-to-use-open-type-in-odata\/","title":{"rendered":"[Tutorial &amp; Sample] How to Use Open Type in OData"},"content":{"rendered":"<p>OData protocol introduces conception of open type which allows clients to add properties dynamically to instances of the type by specifying uniquely named values in the payload used to insert or update an instance of the type. This makes definition of entity type or complex type more flexible. Developers do not have to define everything which is probably used in the edm model.<\/p>\n<p><span style=\"font-size: medium;\"><strong>Server Side<\/strong><\/span><\/p>\n<p><strong><span style=\"font-size: small;\">1. Model Definition<\/span><\/strong><\/p>\n<p>It is quite easy to define an open entity type or open complex type in server side.<\/p>\n<p>If a server uses CSDL file to define the data model, only adding an attribute OpenType=&#8221;true&#8221; to the node of the entity type or the complex type definition is needed. For example,<\/p>\n<p><script type=\"text\/javascript\" src=\"https:\/\/gist.github.com\/ODataTeam\/8cf0f8dced8df5567801.js\"><\/script><\/p>\n<p>Open complex type:<\/p>\n<p><script type=\"text\/javascript\" src=\"https:\/\/gist.github.com\/ODataTeam\/7869146af16531e37819.js\"><\/script><\/p>\n<p>If a server uses EdmModel to define a data model, the parameter isOpen should be true when construct an entity type or complex type as open. For example,<\/p>\n<p>Open entity type:<\/p>\n<p><script type=\"text\/javascript\" src=\"https:\/\/gist.github.com\/ODataTeam\/c4a05e879315ae1ae788.js\"><\/script><\/p>\n<p>Open complex type:<\/p>\n<p><script type=\"text\/javascript\" src=\"https:\/\/gist.github.com\/ODataTeam\/e80e04512c53abc0460b.js\"><\/script><\/p>\n<p>According to the OData protocol, the default values of OpenType and isOpen are false.<\/p>\n<p><strong><span style=\"font-size: small;\">2. Uri Parse<\/span><\/strong><\/p>\n<p>When query a dynamic property (A property, in open type, not declared in the edm model is called \u201cdynamic property\u201d because it is added by clients dynamically), the query uri can be something like: &#8220;~\/Categories(0)\/DynamicPropertyName&#8221; or &#8220;~\/AccountInfo\/DynamicPropertyName&#8221;. If service calls ODataUriParser to parse the uri, the last segment of the uri should be OpenPropertySegment.<\/p>\n<p><script type=\"text\/javascript\" src=\"https:\/\/gist.github.com\/ODataTeam\/cdf36a9a81c6381f4e5f.js\"><\/script><\/p>\n<p><span style=\"font-size: medium;\"><strong>Client Side<\/strong><\/span><\/p>\n<p>At client side, developers can use OData client code generator to generate OData client code. For more details, please refer to <a href=\"http:\/\/blogs.msdn.com\/b\/odatateam\/archive\/2014\/03\/11\/how-to-use-odata-client-code-generator-to-generate-client-side-proxy-class.aspx\">How to Use OData Client Code Generator<\/a>. In the generated codes, a class which represents an entity type and complex type is defined as a partial class. Developers can add properties into the specific class as dynamic properties. The added code is similar as those generated code for a declared property.<\/p>\n<p>For example, declare a dynamic property with property name as \u201cDescription\u201d in Category which is an open entity type. If interested, people can have try against the <a href=\"http:\/\/services.odata.org\/V4\/(S(readwrite))\/OData\/OData.svc\/\">OData (read\/write)<\/a> service.<\/p>\n<p>It is in same way to declare a dynamic property in an open complex type.<\/p>\n<p><script type=\"text\/javascript\" src=\"https:\/\/gist.github.com\/ODataTeam\/7375f1e82ac908b27950.js\"><\/script><\/p>\n<p>Then the client will treat them as any other properties. So users can use the dynamic property in the same way as a declared property.<\/p>\n<p>Sample Code:\n<script type=\"text\/javascript\" src=\"https:\/\/gist.github.com\/ODataTeam\/541c7aa1522ba1ad3a44.js\"><\/script><\/p>\n","protected":false},"excerpt":{"rendered":"<p>OData protocol introduces conception of open type which allows clients to add properties dynamically to instances of the type by specifying uniquely named values in the payload used to insert or update an instance of the type. This makes definition of entity type or complex type more flexible. Developers do not have to define everything [&hellip;]<\/p>\n","protected":false},"author":517,"featured_media":3253,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[50],"class_list":["post-313","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-odata","tag-odata-client-tools"],"acf":[],"blog_post_summary":"<p>OData protocol introduces conception of open type which allows clients to add properties dynamically to instances of the type by specifying uniquely named values in the payload used to insert or update an instance of the type. This makes definition of entity type or complex type more flexible. Developers do not have to define everything [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/odata\/wp-json\/wp\/v2\/posts\/313","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/odata\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/odata\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/odata\/wp-json\/wp\/v2\/users\/517"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/odata\/wp-json\/wp\/v2\/comments?post=313"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/odata\/wp-json\/wp\/v2\/posts\/313\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/odata\/wp-json\/wp\/v2\/media\/3253"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/odata\/wp-json\/wp\/v2\/media?parent=313"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/odata\/wp-json\/wp\/v2\/categories?post=313"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/odata\/wp-json\/wp\/v2\/tags?post=313"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}