CRM 2011 + Retrieve Related Entities using OData and Silverlight.


While creating Silverlight plugins for CRM 2011 we are in need of retrieving related entities for a particular entity. By far there is no way of retrieving this data using some kind of query expression or even lambda expression for that matter. After a lot of thought I have found the way out to do this. I had searched the entire web and not a single reference to do this kind of retrieval was found. I hope the following code helps you.

var query = _context.AccountSet.AddQueryOption("$expand", "new_account_address").Where(a => a.ActivationStatus.Equals("A")) as DataServiceQuery;

In the above code “$expand” is the OData operator for retrieving related entities and the parameter passed to the function i.e. “cnc_account_address” is the relationship name between account entity and address entity.

After you execute the query you can find the related address data in their respect account record.

Please post back in case of queries.

Advertisements

3 thoughts on “CRM 2011 + Retrieve Related Entities using OData and Silverlight.

  1. paolo

    Hi, thanks for this post! Could you please submit the code to read the cnc_account_address? I can not figure out how to read because in the related entity i get always 0 records. If i put the OData query in the browser, it shows me only the main entity (in your example Account), but if i open up the source code of the page i can see all the related entities.
    Please help me
    Thanks a lot

    Reply
    1. Sayantan Samanta Post author

      Even if u dont add the $expand operator you will still get all the related entity schema but they will not be populated with data. The new_account_address is the relationship schema name and not the related entity name.

      foreach(Account account in retrievedAccounts)
      {
      Address address = account.Address;
      MessageBox.Show(address.Line1);
      }

      In the above code retrievedAccounts contains all the accounts retrieved and inside that a property named Address will contain the related addresses data.

      Write in incase u still have doubts. May be u can share your code.

      Reply
      1. Paolo

        Hi! Thank you for your reply! I think my problem is a bit different. I would like to get an account with all his related contacts (contacts that have this account as Parent Customer). In my Silverlight OData query i am using Account entity and “$expand”, “contact_customer_accounts”. The problem is that i don’t know how to iterate trough the contacts. If i put the Odata query (url) in IE9 I see my account and if i show the “View source” i can see my 3 associated contacts. From C# code i see only my account, but nothing else. I don’t have ” item.Contacts” to iterate through and “item.contact_customer_accounts.Count” is 0 even if i know that there are 3 contacts … how can i do that? Thank you in advance.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s