OnIIcE
OnIIcE

Reputation: 871

get related data from ms dynamics crm using XRM SDK

I'm trying to retrieve data from crm in a .net application, using the SDK. I've managed to do simple queries to retrieve lists, but I would now like to get the related entities with items, rather than the ids.

I have tried things like

QueryExpression query = new QueryExpression
  {
    EntityName = "opportunity",
....

LinkEntity linkEntityAccount = new LinkEntity()
 {
    LinkFromEntityName = "opportunity",
    LinkFromAttributeName = "opportunityid",
    LinkToEntityName = "serviceappointment",
    LinkToAttributeName = "regardingobjectid",
    JoinOperator = JoinOperator.Inner,
    Columns = new ColumnSet(new string[] { "scheduledstart", "scheduledend" }),
    EntityAlias = "service"
 };

query.LinkEntities.Add(linkEntityAccount);

(This will return a collection of entities from the opportunity table)

However the LinkedEntities just put the two columns in the returns entities.

What i would like is (say for this example) is a entity.serviceappointment to be the the entity containing the service appointment entity/data. Instead of in entity there being fields such as service.scheduledstart and service.scheduledend

I have looked at the Relationship and RelationshipQueryCollection things in the SDK but i have been unable to setup a query that will do the query, without first getting the opportunity entities. But it looks like that maybe what I need? I'm not sure.

Is this even possible? Or should I just continue to query entities individually?

Thanks

Upvotes: 0

Views: 1567

Answers (2)

MBender
MBender

Reputation: 5650

There's no automatic way to get the entire linked entity data (as an Entity object) that I know of (that's not to say it's impossible, mind you).

But I think it'd be a lot easier to just query the data you need in another request.

  • Find the list of opportunities you need
  • Use the regarding object IDs as the parameter of an "IN" filter for the second query.

Upvotes: 0

Henk van Boeijen
Henk van Boeijen

Reputation: 7948

In the QueryExpression the LinkEntity represents a join. That's why the fields of the joined table are in the Entity row. They can be distinguished from the 'real' entity attributes by the fact that their names are prefixed (including a dot) and their values are wrapped in an AliasedValue object.

It is possible to unwrap them and create strong typed Entity objects, but you will need to write the code yourself.

Alternatively you can consider a few other options:

  1. Query for serviceappointment records and join the opportunity records.
  2. Retrieve opportunity records one by one using the RetrieveRequest and include the query for the related service appointments in the request. (See also this discussion on StackOverflow.)
  3. Create an Action returning all data you need in a convenient OrganizationResponse.

Upvotes: 1

Related Questions