Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Sample: Retrieve with one-to-many relationship

Sample: Retrieve with one-to-many relationship

Applies To: CRM 2015 on-prem, CRM Online


For more information about the requirements for running the sample code provided in this SDK, see Use the sample and helper code.


The following sample shows how to use the RetrieveMultiple method to return the 1:N related entities of an entity along with the primary entity. In this example, we retrieve an account and its related tasks.

Console.WriteLine("One-to-many Query using RetrieveMultiple");
//Create an account and three related tasks
    Account account = new Account();
    account.Name = "SDK Test Account";
    Guid acctId = _serviceProxy.Create(account);
    Console.WriteLine("New account created.");

    Task task1 = new Task();
    task1.Subject = "Test Task1";
    task1.RegardingObjectId = new EntityReference("account", acctId);
    Guid task1Id = _serviceProxy.Create(task1);

    Task task2 = new Task();
    task2.Subject = "Test Task2";
    task2.RegardingObjectId = new EntityReference("account", acctId);
    Guid task2Id = _serviceProxy.Create(task2);

    Task task3 = new Task();
    task3.Subject = "Test Task3";
    task3.RegardingObjectId = new EntityReference("account", acctId);
    Guid task3Id = _serviceProxy.Create(task2);

    Console.WriteLine("Three tasks created.");

// Construct query
    // Condition where task attribute equals account id. 
    ConditionExpression condition = new ConditionExpression();
    condition.AttributeName = "regardingobjectid";
    condition.Operator = ConditionOperator.Equal;

    //Create a column set.
    ColumnSet columns = new ColumnSet("subject");

    // Create query expression.
    QueryExpression query1 = new QueryExpression();
    query1.ColumnSet = columns;
    query1.EntityName = "task";

    EntityCollection result1 = _serviceProxy.RetrieveMultiple(query1);

    foreach (var r in result1.Entities)

See Also

Microsoft Dynamics CRM 2015 and Microsoft Dynamics CRM Online
Send comments about this topic to Microsoft.
© 2015 Microsoft. All rights reserved.
© 2015 Microsoft