CRM 2011 Metadata Query Enhancements
January 11, 2013 Leave a comment
Rollup 12 (CRM December 2012 Service Update), is full of nice new features but a huge bonus was the new capabilities to interact with CRM metadata in a much more efficient manner along with benefiting from improved performance. Only retrieving the metadata that you only require and need is a much more pleasing idea especially when it comes to mobile clients. The MSDN SDK article can be found here that covers this subject in more detail.
The following classes offer this new functionality
- RetrieveMetadataChangesRequest (Microsoft.Xrm.Sdk.Messages)
- RetrieveMetadataChangesResponse (Microsoft.Xrm.Sdk.Messages)
Prior to Rollup 12 you could use RetrieveEntityRequest or RetrieveAllEntitiesRequest in the Microsoft.Xrm.Sdk.Messages namespace but were limited to the amount of query filters so when you queried for entity data you essentially received more than sometimes necessary. You can see the EntityFilters enumeration here
|All||Use this to retrieve all data for an entity. Value = 15.|
|Attributes||Use this to retrieve entity information plus attributes for the entity. Value = 2.|
|Default||Use this to retrieve only entity information. Equivalent to EntityFilters.Entity . Value = 1.|
|Entity||Use this to retrieve only entity information. Equivalent to EntityFilters.Default . Value = 1.|
|Privileges||Use this to retrieve entity information plus privileges for the entity. Value = 4.|
|Relationships||Use this to retrieve entity information plus entity relationships for the entity. Value = 8.|
RetrieveEntityRequest retrieveRequest = new RetrieveEntityRequest
EntityFilters = EntityFilters.Entity,
LogicalName = _customEntityName
RetrieveEntityResponse retrieveEntityResponse = (RetrieveEntityResponse)orgService.Execute(retrieveRequest);
As you can imagine if you only want to check if an entity has Is Visible in Mobile flag set then you don’t want anything else. In this case the new EntityQueryExpression class provides a similar experience to the existing QueryExpression for querying CRM data though specifically for querying metadata.
New metadata query classes include
|AttributeQueryExpression||Defines a complex query to retrieve attribute metadata for entities retrieved using an EntityQueryExpression|
|DeletedMetadataCollection||The structure used to return deleted metadata.|
|EntityQueryExpression||Defines a complex query to retrieve entity metadata.|
|LabelQueryExpression||Defines the languages for the labels to be retrieved for metadata items that have labels.|
|MetadataConditionExpression||Contains a condition expression used to filter the results of the metadata query.|
|MetadataFilterExpression||Specifies complex condition and logical filter expressions used for filtering the results of a metadata query.|
|MetadataPropertiesExpression||Specifies the properties for which non-null values are returned from a query.|
|MetadataQueryBase||Represents the abstract base class for constructing a metadata query.|
|MetadataQueryExpression||Represents the abstract base class for constructing a metadata query.|
|RelationshipQueryExpression||Defines a complex query to retrieve entity relationship metadata for entities retrieved using an EntityQueryExpression|
So to slightly different but if you want to retrieve all entities that have the Is Visible in Mobile set to true using the new classes then specify a filter as follows and execute the RetrieveMetadataChangesRequest message with ClientVersionStamp set to null in the request.
MetadataFilterExpression entityFilter = new MetadataFilterExpression(LogicalOperator.And);
EntityFilter.Conditions.Add(new MetadataConditionExpression("IsVisibleInMobile", MetadataConditionOperator.Equals, true);
EntityQueryExpression entityQueryExpression = new EntityQueryExpression()
Criteria = entityFilter
RetrieveMetadataChangesRequest retrieveMetadataChangesRequest = new RetrieveMetadataChangesRequest()
Query = entityQueryExpression,
ClientVersionStamp = null
RetrieveMetadataChangesResponse response = (RetrieveMetadataChangesResponse)orgService.Execute(retrieveMetadataChangesRequest);
The ClientVersionStamp timestamp value representing when the last request was made can be set to only retrieve data that has changed since the time specified. The RetrieveMetadataChangesResponse returns a timestamp value that can be used with another request at a later time to return information about how metadata has changed since the last request.
This article only scratches the surface of this awesome and long awaited feature so if you want to come up to speed have a read over the SDK article Retrieve and Detect Changes to Metadata.