{"id":25270,"date":"2020-10-27T12:44:44","date_gmt":"2020-10-27T12:44:44","guid":{"rendered":"https:\/\/www.inogic.com\/blog\/?p=25270"},"modified":"2020-10-27T12:45:22","modified_gmt":"2020-10-27T12:45:22","slug":"how-to-retrieve-entity-metadata-using-xrm-utility-getentitymetadata","status":"publish","type":"post","link":"https:\/\/www.inogic.com\/blog\/2020\/10\/how-to-retrieve-entity-metadata-using-xrm-utility-getentitymetadata\/","title":{"rendered":"How to retrieve entity metadata using Xrm.Utility.getEntityMetadata()"},"content":{"rendered":"<h2>Introduction<\/h2>\n<p style=\"text-align: justify;\">Currently, we need to create http requests to retrieve the metadata of the entity or attributes of any entity. But now, Dynamics 365 introduced a Web API method i.e Xrm.Utility.getEntityMetadata() which helps you retrieve any entity metadata easily without creating any http requests. Please find below two examples to understand this method in detail:<\/p>\n<h3>1. Retrieve whole entity metadata<\/h3>\n<p style=\"text-align: justify;\">Let\u2019s take one example, we want to check if the current entity is activity entity or not. For this query, you can simply use Xrm.Utility.getEntityMetadata, you need to just pass the entity logical name of the entity which you want to retrieve the metadata for, as shown below:<\/p>\n<p>Xrm.Utility.getEntityMetadata(&#8220;account&#8221;, []).then(<\/p>\n<p>function(entityMetadata){<\/p>\n<p>\/\/check for entity metadata<\/p>\n<p>If(entityMetadata!=undefined){<\/p>\n<p>\/\/declare one flag which indicate current entity is activity entity<\/p>\n<p>var isActivityEntity = false;<\/p>\n<p>\/\/check for IsActivity attribute<\/p>\n<p>if (entityMetadata.IsActivity != undefined) {<\/p>\n<p>\/\/check current entity is activity entity using IsActivity attribute<\/p>\n<p>if (entityMetadata.IsActivity == true) {<\/p>\n<p>\/\/set flag to true as current entity is Activity entity<\/p>\n<p>isActivityEntity = true;<\/p>\n<p>} else {<\/p>\n<p>\/\/set flag to false as current entity is not Activity entity<\/p>\n<p>isActivityEntity = true;<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>}, function(e){<\/p>\n<p>\/\/error<\/p>\n<p>alert(e.error.message);<\/p>\n<p>});<\/p>\n<p style=\"text-align: justify;\"><em><strong>Note: We pass the second parameter is empty [] to getEntityMetadata method because in the above example we need to retrieve all metadata of the Account entity.<\/strong><\/em><\/p>\n<p style=\"text-align: justify;\">After running the below code, we will get all the metadata of that entity such as logical name, entity set name, as shown in the below screenshot.<\/p>\n<h1><a href=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2020\/10\/How-to-retrieve-entity-metadata-using-Xrm.Utility.getEntityMetadata-1.png\" target=\"_blank\" rel=\"noopener noreferrer\"><img decoding=\"async\" class=\"aligncenter wp-image-25271 size-full\" style=\"border: 1px solid #0a0a0a; padding: 1px; margin: 1px;\" src=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2020\/10\/How-to-retrieve-entity-metadata-using-Xrm.Utility.getEntityMetadata-1.png\" alt=\"\" width=\"1361\" height=\"703\" srcset=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2020\/10\/How-to-retrieve-entity-metadata-using-Xrm.Utility.getEntityMetadata-1.png 1361w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2020\/10\/How-to-retrieve-entity-metadata-using-Xrm.Utility.getEntityMetadata-1-300x155.png 300w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2020\/10\/How-to-retrieve-entity-metadata-using-Xrm.Utility.getEntityMetadata-1-768x397.png 768w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2020\/10\/How-to-retrieve-entity-metadata-using-Xrm.Utility.getEntityMetadata-1-1024x529.png 1024w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2020\/10\/How-to-retrieve-entity-metadata-using-Xrm.Utility.getEntityMetadata-1-660x341.png 660w\" sizes=\"(max-width: 1361px) 100vw, 1361px\" \/><\/a><\/h1>\n<h3><strong>2. Retrieve metadata of selected attribute of any entity <\/strong><\/h3>\n<p style=\"text-align: justify;\">Now, we want to retrieve any attribute metadata of any entity that you want in Dynamics 365. Let\u2019s take one example; we want to retrieve metadata of the industry field of Account entity using the getEntityMetadata method. In this scenario, you need to pass those entity names and attributes list for which you want to retrieve metadata, as shown below.<\/p>\n<p>Xrm.Utility.getEntityMetadata(&#8220;account&#8221;, [&#8220;industrycode&#8221;]).then(<\/p>\n<p>function (entityMetadata) {<\/p>\n<p>\/\/check for attribute entity metadata<\/p>\n<p>if (entityMetadata != undefined &amp;&amp; entityMetadata.Attributes != undefined &amp;&amp; entityMetadata.Attributes._collection != undefined) {<\/p>\n<p>\/\/check for industry type option set<\/p>\n<p>if (entityMetadata.Attributes._collection[&#8220;industrycode&#8221;]) {<\/p>\n<p>\/\/get option set data<\/p>\n<p>var industryCodeOptionSetData = entityMetadata.Attributes._collection[&#8220;industrycode&#8221;].OptionSet;<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>}, function(e){<\/p>\n<p>\/\/error call back<\/p>\n<p>alert(e.error.message);<\/p>\n<p>});<\/p>\n<p style=\"text-align: justify;\">After running the above code you will get all options data in the industry field Option Set, as shown in the below screenshot:<\/p>\n<p><a href=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2020\/10\/How-to-retrieve-entity-metadata-using-Xrm.Utility.getEntityMetadata-2.png\" target=\"_blank\" rel=\"noopener noreferrer\"><img decoding=\"async\" class=\"aligncenter wp-image-25272 size-full\" style=\"border: 1px solid #0a0a0a; padding: 1px; margin: 1px;\" src=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2020\/10\/How-to-retrieve-entity-metadata-using-Xrm.Utility.getEntityMetadata-2.png\" alt=\"How to retrieve entity metadata using Xrm.Utility.getEntityMetadata\" width=\"1359\" height=\"703\" srcset=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2020\/10\/How-to-retrieve-entity-metadata-using-Xrm.Utility.getEntityMetadata-2.png 1359w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2020\/10\/How-to-retrieve-entity-metadata-using-Xrm.Utility.getEntityMetadata-2-300x155.png 300w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2020\/10\/How-to-retrieve-entity-metadata-using-Xrm.Utility.getEntityMetadata-2-768x397.png 768w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2020\/10\/How-to-retrieve-entity-metadata-using-Xrm.Utility.getEntityMetadata-2-1024x530.png 1024w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2020\/10\/How-to-retrieve-entity-metadata-using-Xrm.Utility.getEntityMetadata-2-660x341.png 660w\" sizes=\"(max-width: 1359px) 100vw, 1359px\" \/><\/a><\/p>\n<h2><strong>Conclusion<\/strong><\/h2>\n<p style=\"text-align: justify;\">With help of Xrm.Utility.getEntityMetadata() method you can retrieve metadata of any entity or metadata of any attributes of that entity, you no longer need to create an http request to achieve this.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction Currently, we need to create http requests to retrieve the metadata of the entity or attributes of any entity. But now, Dynamics 365 introduced a Web API method i.e Xrm.Utility.getEntityMetadata() which helps you retrieve any entity metadata easily without creating any http requests. Please find below two examples to understand this method in detail:\u2026 <span class=\"read-more\"><a href=\"https:\/\/www.inogic.com\/blog\/2020\/10\/how-to-retrieve-entity-metadata-using-xrm-utility-getentitymetadata\/\">Read More &raquo;<\/a><\/span><\/p>\n","protected":false},"author":13,"featured_media":0,"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,18,19,24],"tags":[545,592,740,2084,1474,1854,1859],"class_list":["post-25270","post","type-post","status-publish","format-standard","hentry","category-dynamics-365","category-dynamics-365-v9-2","category-dynamics-crm","category-dynamics-crm-2016","tag-dynamics-365-crm","tag-dynamics-crm","tag-entity","tag-metadata","tag-retrive-metadata","tag-xrm","tag-xrm-utility"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/posts\/25270","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=25270"}],"version-history":[{"count":0,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/posts\/25270\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/media?parent=25270"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/categories?post=25270"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/tags?post=25270"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}