I’ve been a longtime fan of LINQPad (www.linqpad.net) as a general purpose tool for executing LINQ queries with various LINQ enabled technologies as well as a general purpose scratchpad for testing out .NET code without having to fire up Visual Studio. I’ve already blogged about how to use LINQPad to work with the CRM Online SOAP service:
One thing that may not be obvious is how to use LINQPad to query the OData service that comes with Dynamics CRM Online. Using the same basic concept that I showed with the SOAP service, I created a video walkthrough of how to interact with the OData service from LINQPad:
Here’s the LINQPad starter code I showed in the video:
var clientId = "[YOUR_CLIENT_ID]";
var resource = "https://[YOUR_INSTANCE].crm.dynamics.com";
var redirectUri = new Uri("http://linqpad"); //replace with your redirect urivar authContext = new AuthenticationContext("https://login.windows.net/common/oauth2/authorize", false);var authResult = authContext.AcquireToken(resource, clientId, redirectUri, PromptBehavior.Auto);Util.ClearResults();var ctx = new marcsctest1Context(new Uri(resource + "/XRMServices/2011/OrganizationData.svc/"));ctx.SendingRequest2 +=(o, eventArgs) => eventArgs.RequestMessage.SetHeader("Authorization", "Bearer " + authResult.AccessToken);var query =from a in ctx.AccountSet
where a.Name.Contains("sample")
select new
{a.AccountId,a.Name,a.Address1_City,a.Address1_StateOrProvince};query.ToString().Dump("OData Query Url");
query.Dump("Query Results");
One of the things I did, after creating the video, was to write a reusable helper method in the LINQPad MyExtensions class so I can reuse the code to get the access token across multiple LINQPad query sessions. Here’s the code for the extension:
void Main()
{// Write code to test your extensions here. Press F5 to compile and run.
}public static class MyExtensions{// Write custom extension methods here. They will be available to all queries.
static string LINQPadAccessToken = null;public static string GetAccessToken(){if (LINQPadAccessToken != null){return LINQPadAccessToken;
}var clientId = "[YOUR_CLIENT_ID]";
var resource = "https://[YOUR_INSTANCE].crm.dynamics.com";
var redirectUri = new Uri("http://linqpad"); //your redirect urivar authContext = new AuthenticationContext("https://login.windows.net/common/oauth2/authorize", false);var authResult = authContext.AcquireToken(resource, clientId, redirectUri, PromptBehavior.Auto);Util.ClearResults();LINQPadAccessToken = authResult.AccessToken;return LINQPadAccessToken;
}}Here’s a version of the code in my query tab that is simplified, after writing and leveraging the helper method:
var ctx = new marcsctest1Context(
new Uri("https://[YOUR_INSTANCE].crm.dynamics.com/XRMServices/2011/OrganizationData.svc/"));ctx.SendingRequest2 +=(o, eventArgs) => eventArgs.RequestMessage.SetHeader("Authorization", "Bearer " + MyExtensions.GetAccessToken());var query =from a in ctx.AccountSet
where a.Name.Contains("sample")
select new
{a.AccountId,a.Name,a.Address1_City,a.Address1_StateOrProvince};query.ToString().Dump("OData Query Url");
query.Dump("Query Results");
Note the call to MyExtensions.GetAccessToken() above. You could obviously refactor the GetAccessToken() method more to make it even more reusable, but I will leave that as an exercise for you.
This approach will become even more powerful once Dynamics CRM introduces the new OData v4 service endpoint:
https://msdn.microsoft.com/en-us/dynamics/crm/webapipreview
As I mention in the video, once that happens, you’ll want to perform the codgen with the OData Client Code Generator instead of “Add Service Reference”: