OData service across companies

The OData service in Dynamics 365 for Operations returns data from user’s default company. But you can easily add cross-company=true to the URL (e.g. https://myaos.cloudax.dynamics.com/data/Customers?cross-company=true) to get data from all companies. You can also filter the query by DataAreaId field, if you want data from a specific company.

But developers usually don’t construct the URL by themselves; they use some libraries that deal with such implementation details. In .NET, you’ll most likely use OData Client Code Generator (which was also used to create the OData app in Microsoft’s integration samples). The obvious question is how to put the cross-company parameter there.

You can subscribe to the BuildingRequest event, parse the URL and add the parameter, like this:

context.BuildingRequest += (sender, e) =>
{
    var uriBuilder = new UriBuilder(e.RequestUri);
    // Requires a reference to System.Web
    var paramValues = HttpUtility.ParseQueryString(uriBuilder.Query);
    paramValues.Add("cross-company", "true");
    uriBuilder.Query = paramValues.ToString();
    e.RequestUri = uriBuilder.Uri;
};

Note that simply attaching ?cross-company=true wouldn’t be enough; it wouldn’t work if there were some other parameters (such as for filtering).

One Comment

Comments are closed.