Using Microsoft Dynamics CRM API to get Status Reason Metadata Option

Using Microsoft Dynamics API to get Status Reason Option Set Values.

Dynamics 365 is a metadata-driven application, sometimes we need to query the system metadata at client side to get the configuration of an organization. This can be achieved using the Web API as well as using the organization service. The Web API gives us the capability to query metadata but does not allow us to detect changes in metadata at any given point in time.

StatusCode With Dynamic CRM API

We will try to achieve the retrieval of Status Code from the Dynamics API with metadata call.

There are two type of options in metadata to retrieve option set from Dynamics API

  • Dynamics.CRM.PicklistAttributeMetadata is used for option sets
  • Dynamics.CRM.StatusAttributeMetadata is used for the status reasons

Microsoft.Dynamics.CRM.StatusAttributeMetadata API Example

Now let us create the query for the retrieval of status code option values

GET [Organization URI]/api/data/[Api version]/ EntityDefinitions(LogicalName=”Entity Logical Name ”)/Attributes(LogicalName=”Attribute Logical Name “)/Microsoft.Dynamics.CRM. StatusAttributeMetadata?$select=LogicalName&$expand=GlobalOptionSet($select=Options)

Now you can run this in the same browser where you are logged in to Dynamics CRM, so we can verify the result. The result set would be like the one below.

retrieval of status code

JS Code

We will create the XMLHttpRequest and then send the request with metadata API functions call. See the image below.

metadata api functions call

So, we will be able to get the status code for the entity at client side with the help of CRM API using the code above

Microsoft.Dynamics.CRM.PicklistAttributeMetadata API Example

Now let us create the query for the retrieval of option set values

GET [Organization URI]/api/data/[Api version]/ EntityDefinitions(LogicalName=”Entity Logical Name ”)/Attributes(LogicalName=”Attribute Logical Name “)/Microsoft.Dynamics.CRM.PicklistAttributeMetadata?$select=LogicalName&$expand=GlobalOptionSet($select=Options)

Similarly, we will verify the query in the same browser where you are logged in to CRM, so we can verify the result. The result set would be like the one below.

verify the result

JS Code

We will create the XMLHttpRequest and then send the request with metadata API functions call. See the images below.

metadata API

Similarly using the code mentioned above, we can get the option set value for an entity.

Hope this will be helpful.

Thanks!!!

Conclusion

We can use Dynamics 365 API function in JS to get the Status code as well as Option set values at the client side.

Leave a Reply

Your email address will not be published. Required fields are marked *