6

I'm trying to retrieve multiple data from the new REST API but I have a strange issue. If I use $expand on a collection then it doesn't work.

The request is:

GET [Oranization URL]/api/data/v8.0/accounts?$expand=contact_customer_accounts HTTP/1.1
Accept: application/json
Content-Type: application/json; charset=utf-8
OData-MaxVersion: 4.0
OData-Version: 4.0

And the response:

{
"error": {
    "code": "",
    "message": "Expansion of navigation properties isn\u2019t supported on entity collections.",
    "innererror": {
        "message": "Expansion of navigation properties isn\u2019t supported on entity collections.",
        "type": "Microsoft.Crm.CrmHttpException",
        "stacktrace": "   at Microsoft.Crm.Extensibility.OData.CrmODataUtilities.ThrowIfSelectClauseHasNavigationProperty(SelectExpandClause selectExpandClause, Boolean isCalledFromEntitySet)\r\n   at Microsoft.Crm.Extensibility.OData.CrmODataServiceDataProvider.RetrieveEdmEntityCollection(CrmODataExecutionContext context, String entityCollectionName, ODataQueryOptions queryOptions)\r\n   at Microsoft.Crm.Extensibility.OData.EntityController.GetEntitySetInternal(String entitySetName, String castEntityName, CrmODataExecutionContext context, CrmEdmEntityObjectCollection crmEdmEntityObjectCollection, ODataQueryOptions queryOptions)\r\n   at Microsoft.Crm.Extensibility.OData.EntityController.GetEntitySet(String entitySetName)\r\n   at lambda_method(Closure , Object , Object[] )\r\n   at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.<>c__DisplayClass10.<GetExecutor>b__9(Object instance, Object[] methodParameters)\r\n   at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ExecuteAsync(HttpControllerContext controllerContext, IDictionary`2 arguments, CancellationToken cancellationToken)\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()\r\n   at System.Web.Http.Controllers.ApiControllerActionInvoker.<InvokeActionAsyncCore>d__0.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()\r\n   at System.Web.Http.Controllers.ActionFilterResult.<ExecuteAsync>d__2.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()\r\n   at System.Web.Http.Dispatcher.HttpControllerDispatcher.<SendAsync>d__1.MoveNext()"
        }
    }
}

But if I make this request on a single entity then I get the result

[Organization URL]/api/data/v8.0/accounts(9761dd37-d1b6-e511-80d7-001dd8b71fde)?$expand=contact_customer_accounts

In the previous versions of CRM (2011,2015) this was worked perfectly. Is it possible that MS remove this function? Actually, the oData 4.0 protocol allows this type of queries

http://services.odata.org/v4/TripPinServiceRW/People?$expand=Trips

Why would MS disallow it? The new REST API is useless without this.

Jatin Sanghvi
  • 1,928
  • 1
  • 22
  • 33
zskovacs
  • 98
  • 1
  • 6
  • Maybe you could work around this using a [custom FetchXml query](https://msdn.microsoft.com/en-us/library/mt607533.aspx#bkmk_useFetchXML). – Filburt Jan 12 '16 at 12:01
  • ... gave it a quick try myself and found `` seems to be simply ignored when used in this type of query. Meh. – Filburt Jan 12 '16 at 12:20

1 Answers1

8

Even though I would expect this to be listed in Microsoft Dynamics CRM Web API Limitations it does indeed appear to be a limitation of the current implementation.

Other users have noticed, like you, that it appears that the $expand clause only works when requesting data of a single record.

I have created a suggestion, which was implemented as part of the Spring 2016 release of CRM: Web API: Implement $expand on collections:

We enabled this capability in Spring 2016 release of Dynamics CRM. If you request an expand & select on a related collection the response will contain an empty collection with a nextlink which you can then use to programmatically request each of the related pages. We are actively looking into how we can support $filter on the expanded collection. If your scenario requires that then please create another idea so that it we can prioritize it accordingly.

Henrik H
  • 5,747
  • 1
  • 21
  • 33
  • I got confirmation that the uservoice site you linked will be turned off (it was related to the preview phase), Microsoft Connect is still the official site for submit feedback, feel free to add the same suggestion there (I checked quickly and I didn't find a similar one) and edit your answer. – Guido Preite Jan 13 '16 at 16:44
  • Thanks for pointing that out, Guido. I have edited the answer with a link to Connect instead. As with all links to Connect, it is worth pointing out that one should be logged in as you describe in your blog post: http://www.crmanswers.net/2014/02/how-to-use-microsoft-connect-site.html – Henrik H Jan 14 '16 at 11:51
  • This behavior is different from the old OData v2 service which did allow you to expand collection properties. But that capability was very expensive for overall performance. – Jim Daly -MSFT- Aug 22 '16 at 04:56
  • 1
    In v8.1 Web API does provide @odata.nextLink values that allow you to expand the individual collections as needed with a second operation. See [Retrieve related entities by expanding navigation properties](https://msdn.microsoft.com/en-us/library/gg334767.aspx#bkmk_expandRelated) esp the sample titled 'Retrieve related entities by expanding collection-valued navigation properties' You should post this as a suggestion so it can be re-considered, but I would be surprised if this behavior changes. – Jim Daly -MSFT- Aug 22 '16 at 05:05
  • Note URL for this suggestion is updated to https://ideas.dynamics.com/ideas/dynamics-crm/2241247 – Serge May 10 '17 at 01:02