The Microsoft Dynamics CRM Blog
News and views from the Microsoft Dynamics CRM Team

New Technical Article: Query Metadata Using JavaScript

New Technical Article: Query Metadata Using JavaScript

  • Comments 0

If you work with JavaScript in Microsoft Dynamics CRM and need to query the metadata, please take a few minutes to look at the new technical article I just published: Query Metadata Using JavaScript.

For a while now, the SDK has had sample libraries for retrieving metadata. The SDK.Metadata.js library included in the Sample: Retrieve Entity Metadata Using JavaScript topic includes functions that use RetrieveAllEntitiesRequest, RetrieveEntityRequest, and RetrieveAttributeRequest.

The new library is different because:

  • You can use the new classes in the Microsoft.Xrm.Sdk.Metadata.Query namespace to more efficiently retrieve just the metadata you need and to detect changes to the metadata. Details about these classes are found in the SDK topic: Retrieve and Detect Changes to Metadata.
  • It uses object oriented programming techniques to provide a development experience using JavaScript that is similar to the experience you have with managed code.

As a programmer/writer for the CRM SDK team, I write content and sample code for a variety of areas including form scripting, web resources, and metadata APIs. In my free time I’ve made a number of tools that access the metadata using JavaScript. The new Metadata Browser and the Form Query String Parameter tool are two recent examples. But the old metadata APIs are not optimized for performance. You often get more data than you really need and performance is affected. So I was really excited about the new classes in the Microsoft.Xrm.Sdk.Metadata.Query namespace because they implement something similar to the QueryExpression classes that you can use to define queries for CRM data.

However, with this flexibility came a new challenge. The old metadata APIs have simple parameters like entity name, attribute name and one enumeration value. It is relatively straight forward to convert these into a simple JavaScript function using the pattern described in Walkthrough: Use the SOAP Endpoint for Web Resources with JavaScript.

The new metadata API requires a complex object defined in XML. To get this I had to implement a whole set of JavaScript “classes” that map to the managed code classes. Each of the classes that define a query has a _toXml method that is used to generate the XML that is sent with the request. I spent a lot of time using the SOAPLogger tool in the SDK to observe the XML sent through using managed code so I could reverse engineer the patterns to get the same XML using JavaScript.

The thing about classes in JavaScript is that there are different ways to write them. Many people use Script#. Recently, TypeScript became available. The way that I came up with is an amalgamation of a number of different patterns I found on the Internet, but I wrote them by hand without any supporting tools like TypeScript or Script#. This is probably because I actually enjoy writing JavaScript. I know many of you do not. If TypeScript had been available when I started this project, I might have used that. If you want to follow this pattern and don’t like JavaScript as much as I do, you might want to try TypeScript or Script#.

Now that I have this library done, I’ve already created another one that provides the same experience for QueryExpression with CRM data. I’m still testing it, and I’ll publish an article when it’s ready. But please don’t think that this is what I spend my workday doing. I have a backlog of content and samples to create to support current and upcoming features. I have no time to spend on these kinds of projects during the work week. I do all these projects on my weekends. If you enjoy using this library, thank my wife for her patience.

I’m not releasing this library in the SDK. This library is much more than a sample for using a CRM API. Although I tested it myself and sent it out to others to review, it hasn’t gone through a full set of tests for all the types of queries that you might make with it. I’m confident that it will work for you, but including it in the SDK would require that our technical support team provide some level of support for it. Our technical support team supports the SOAP Endpoint for web resources which this library uses, but I am the support provider for this library. If you have problems with it, contact me using the Send comments about this topic to Microsoft link at the bottom of the Query Metadata Using JavaScript article. But I hope the complete reference documentation provided in the technical article should answer most of your questions.

Happy Scripting –

Jim Daly

Leave a Comment
  • Please add 2 and 2 and type the answer here:
  • Post