How to get all lookup values from MS CRM using XRM SDK

2.2k views Asked by At

I am writing an app which uses XRM SDK to get data from MS CRM 11. For fields of type lookup I want to get their corresponding definition so I can retrieve all possible ids and values from the related entity.

I cannot seem to find a way to get this. I understand I can get the entity reference for an entity instance value and how to get target entities from meta data but cannot find anything to help me get all lookup ids and values for a lookup field. Please help!

Edit:

In the interest of helping others, and after several more days investigation, I am convinced what I want to do is not possible server-side using sdk.

I can get pick list values no problem using a RetrieveAttributeRequest and cast the resulting AttributeMetaData to PicklistAttributeMetadata. You can use the same technique to deal with Lookups by casting to LookupAttributeMetadata but this only gives you one useful property over the base class: Targets. All this does is provide a string array of entity logical names. It gives you no additional detail such as mapped Id/Name properties or any view details for lookups where queries are applied (such as primary contact lookups where contacts listed are for current organisation).

So, in the end I have had to compromise. I can get the target entity name from Targets and assume that the lookup is simple - just pull all records from entity. The Id column is fixed so that is ok and generally you are safe to assume the Name column is available (albeit likely asl_name etc. if custom).

If anyone knows a better way I will gladly, willingly eat humble pie!

1

There are 1 answers

2
Anupam On

I believe what you're trying to do is fetch all records of a particular entity after you have retrieved the entity name using the metadata service.

If that is what you are looking to do, I suggest having a look at the following location in the SDK (I am referring to the SDK for CRM 2013) SDK\SampleCode\JS\RESTEndpoint\JQueryRESTDataOperations\JQueryRESTDataOperations\Scripts for the file JQueryRESTDataOperationsSample.js

There's this function called RetrieveMultiple which you can use to fetch all records of a particular entity by providing the Entity Name and any filters (optional). If you need help creating the oData query for the filters, you can download the oData Query Deisgner to form the query.