{"id":4576,"date":"2017-02-28T18:04:51","date_gmt":"2017-02-28T12:34:51","guid":{"rendered":"https:\/\/www.inogic.com\/blog\/?p=4576"},"modified":"2022-07-01T15:41:00","modified_gmt":"2022-07-01T10:11:00","slug":"retrieve-metadata-using-name-in-web-api","status":"publish","type":"post","link":"https:\/\/www.inogic.com\/blog\/2017\/02\/retrieve-metadata-using-name-in-web-api\/","title":{"rendered":"Retrieve Metadata using Name in Web API"},"content":{"rendered":"<p style=\"text-align: justify;\">Dynamics 365 introduced a lot of new features and improvements over the previous versions to offer more control and functionalities to the user. The ability to retrieve metadata using name is one such improvement in Web API.<\/p>\n<p style=\"text-align: justify;\">Previously, to retrieve entity metadata, users needed to use \u2018metadataid\u2019. With the addition of this improvement, users can now retrieve the Entity\/Attributes\/Global Option Set meta by using the logical name.<\/p>\n<p style=\"text-align: justify;\"><strong>When do we need to retrieve metadata?<\/strong><\/p>\n<p style=\"text-align: justify;\">There are many scenarios where the user needs object type code, primary attributes, localized label, list of entity attributes, etc. or when the user wants option set metadata. In such situations the user needs to read the entity metadata which can now be easily retrieved.<\/p>\n<p style=\"text-align: justify;\"><strong>Let us see an example to understand how to retrieve Entity metadata:<\/strong><\/p>\n<ul>\n<li style=\"text-align: justify;\">To retrieve Contact entity metadata using Name, use the script mentioned below;<\/li>\n<\/ul>\n<pre class=\"lang:default decode:true \">\/\/this function is used to retrieve entityMetadata\r\nfunction retrieveEntityMetaData() {\r\n\r\n    var entityName = \"\";\r\n    try {       \r\n        \/\/set variable value\r\n        entityName = \"contact\";\r\n\r\n        \/\/create AJAX request\r\n        $.ajax({\r\n            type: \"GET\",\r\n            contentType: \"application\/json; charset=utf-8\",\r\n            datatype: \"json\",\r\n            url: encodeURI(this.getWebAPIPath() + \"EntityDefinitions(LogicalName='\" + entityName + \"')\"),\r\n\r\n            beforeSend: function (xhr) {\r\n                \/\/Specifying this header ensures that the results will be returned as JSON.             \r\n                xhr.setRequestHeader(\"Accept\", \"application\/json\");\r\n                xhr.setRequestHeader(\"Accept\", \"application\/json\");\r\n                xhr.setRequestHeader(\"Content-Type\", \"application\/json; charset=utf-8\");\r\n                xhr.setRequestHeader(\"OData-MaxVersion\", \"4.0\");\r\n                xhr.setRequestHeader(\"OData-Version\", \"4.0\");\r\n            },\r\n            success: function (data, textStatus, xhr) {\r\n                \/\/Call successCallback\r\n                retrieveEntityMetaDataSuccess(data);\r\n            },\r\n            error: function (xhr, textStatus, errorThrown) {\r\n                errorCallback(xhr);\r\n            }\r\n        });\r\n\r\n    } catch (e) {\r\n        showMessage(e.message);\r\n    }\r\n}\r\n<\/pre>\n<ul>\n<li style=\"text-align: justify;\">\u00a0The \u2018success callback\u2019 function mentioned below will get the metadata;<\/li>\n<\/ul>\n<pre class=\"lang:default decode:true \">\/\/success call back\r\nfunction retrieveEntityMetaDataSuccess(data) {\r\n    try {\r\n\r\n        if(data != null) {\r\n\r\n            \/\/get objectTypeCode\r\n            var objectTypeCode = data.ObjectTypeCode;\r\n\r\n            \/\/get primary name\r\n            var primaryAttribute = data.PrimaryNameAttribute;\r\n        }\r\n    }\r\n    catch(e) {\r\n        showMessage(e.message);\r\n    }\r\n}\r\n<\/pre>\n<ul>\n<li style=\"text-align: justify;\">\u00a0If you want to retrieve metadata of any particular attribute then pass the following in <strong>url <\/strong>parameter in Ajax request;<\/li>\n<\/ul>\n<pre class=\"lang:default decode:true \">  url:encodeURI(this.getWebAPIPath() + \"EntityDefinitions(LogicalName='\" + entityName + \"')\/Attributes(LogicalName='parentcustomerid')\") ,\r\nsuccess: function (data, textStatus, xhr) {\r\n                        \/\/Call successCallback\r\n                        successCallback(data);\r\n                    },\r\n<\/pre>\n<p style=\"text-align: justify; padding-left: 30px;\">\u00a0<em>In successCallback function, use data.value to get the attribute metadata.<\/em><\/p>\n<ul>\n<li style=\"text-align: justify;\">If you want to retrieve all the entity attributes then pass the following in <strong>url <\/strong>parameter in Ajax request;<\/li>\n<\/ul>\n<pre class=\"lang:default decode:true \">url:encodeURI(this.getWebAPIPath() + \"EntityDefinitions(LogicalName='\" + entityName + \"')\/Attributes\")<\/pre>\n<p style=\"text-align: justify; padding-left: 30px;\">\u00a0<em>In successCallback function, use data.value to get the list of entity attributes.<\/em><\/p>\n<ul>\n<li style=\"text-align: justify;\">If you want to retrieve relationship metadata then you can use the following in <strong>url <\/strong>parameter in Ajax request;<\/li>\n<\/ul>\n<pre class=\"lang:default decode:true \">url: encodeURI(this.getWebAPIPath() + \"RelationshipDefinitions(SchemaName='account_primary_contact')\")<\/pre>\n<p style=\"text-align: justify; padding-left: 30px;\">\u00a0<em>In successCallback function, use data.value to get the relationship metadata.<\/em><\/p>\n<ul>\n<li style=\"text-align: justify;\">If you want to retrieve Global option set metadata then use the following in <strong>url <\/strong>parameter in Ajax request;<\/li>\n<\/ul>\n<pre class=\"lang:default decode:true \">url: encodeURI(this.getWebAPIPath() + \"GlobalOptionSetDefinitions(Name='budgetstatus')\")<\/pre>\n<p style=\"padding-left: 30px;\"><em>In successCallback function, use data.Options to get all options.<\/em><\/p>\n<p>&nbsp;<\/p>\n<p><strong>Conclusion:<\/strong><\/p>\n<p>Now with the addition of the ability to retrieve metadata using name in Dynamics 365, users can easily retrieve metadata.<\/p>\n<h2 style=\"text-align: left;\"><div class=\"su-heading su-heading-style-default su-heading-align-center\" id=\"\" style=\"font-size:15px;margin-bottom:5px\"><div class=\"su-heading-inner\">One Pic = 1000 words! Analyze data 90% faster with visualization apps!<\/div><\/div><\/h2>\n<p style=\"text-align: left;\"><em>Get optimum visualization of Dynamics 365 CRM data with &#8211;<\/em><br \/>\n<em><strong><a href=\"https:\/\/bit.ly\/3lYvozZ\" target=\"_blank\" rel=\"noopener noreferrer\">Kanban Board<\/a> <\/strong>\u2013 Visualize Dynamics 365 CRM data in Kanban view by categorizing entity records in lanes and rows as per their status, priority, etc.<\/em><br \/>\n<em><strong><a href=\"https:\/\/bit.ly\/3lCSBaA\" target=\"_blank\" rel=\"noopener noreferrer\">Map My Relationships<\/a><\/strong> \u2013 Map My Relationships \u2013 Visualize connections and relationships between Dynamics 365 CRM entities or related records in a Mind Map view.<\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Dynamics 365 introduced a lot of new features and improvements over the previous versions to offer more control and functionalities to the user. The ability to retrieve metadata using name is one such improvement in Web API. Previously, to retrieve entity metadata, users needed to use \u2018metadataid\u2019. With the addition of this improvement, users can\u2026 <span class=\"read-more\"><a href=\"https:\/\/www.inogic.com\/blog\/2017\/02\/retrieve-metadata-using-name-in-web-api\/\">Read More &raquo;<\/a><\/span><\/p>\n","protected":false},"author":13,"featured_media":4578,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[16,33,65],"tags":[591,1468,1469,1813],"class_list":["post-4576","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-dynamics-365","category-javascript","category-webapi","tag-dynamics-365-web-api","tag-retrieve-metadata-using-name","tag-retrieve-metadata-using-name-in-web-api","tag-web-api"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/posts\/4576","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/users\/13"}],"replies":[{"embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/comments?post=4576"}],"version-history":[{"count":0,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/posts\/4576\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/media\/4578"}],"wp:attachment":[{"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/media?parent=4576"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/categories?post=4576"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/tags?post=4576"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}