I am pleased to announce that we released “LINQPad 4 Drive for Dynamics CRM REST and Web API (Preview)” on CodePlex.
For REST: https://crmlinqpadrest.codeplex.com/
For Web API (Preview): https://crmlinqpadwebapi.codeplex.com/
If you are interested in SOAP endpoint driver, please refer to previous blog.
LINQPad 4 Driver for Dynamics CRM is available on CodePlex
What’s LINQPad?
LINQPad (http://www.linqpad.net/) is a great tool which you can write and execute LINQ query against many data sources, like SQL Server, Oracle or Web Services. In addition to LINQ query support, the tool also supports C#/F#/VB expression, statement block or program, to write and execute the code and code snippet.
What can you do with CRM Driver?
By using CRM Driver, you are able to run LINQ queries against Microsoft Dynamics CRM REST/Web API endpoints on the fly. Follow the steps below to try it out!
Install Drivers to LINQPad
1. Download and install LINQPad if you don’t have it yet from http://www.linqpad.net/.
2. Go to REST Driver and Web API Driver and click “DOWNLOADS” tab, then download CRMLinqPadDriverREST.lpx and CRMLinqPadDriverWebAPI.lpx file.
3. Open LINQPad and click “Add connection” link on top left.
Image may be NSFW.
Clik here to view.
4. Click “View more drivers…” button at the bottom of the page.
Image may be NSFW.
Clik here to view.
5. Click “Browse” button in the bottom of the page.
6. Select downloaded lpx file, then click “Open”.
7. Click OK.
Image may be NSFW.
Clik here to view.
8. Do the same for another driver.
Use the REST Driver
Firstly, I explain how to use REST driver.
Create Connection
1. Select “Dynamics CRM REST Linq Pad Driver” in Choose Data Context and click “Next”
Image may be NSFW.
Clik here to view.
2. If you want to use your own ClientId, then uncheck “Register to Azure AD automatically” I will explain what it is later in this article. Click “Login to CRM”. If you want to run query against On-Premise server, (including IFD), you can leave the option as it is.
Image may be NSFW.
Clik here to view.
3. Login to your Organization at login screen.
Image may be NSFW.
Clik here to view.
4. Then it automatically starts doing its work and generates a DataContext from the selected organization. Wait until it’s done.
5. Click “Exit” button once loading completed. Then LINQPad starts loading the schema, which takes a bit of time. Wait until you see the schema information on the left pane like below screenshot.
Image may be NSFW.
Clik here to view.
Write LINQ query and Execute
1. Firstly, select added connection from “Connection” dropdown on the top right.
Image may be NSFW.
Clik here to view.
2. Enter following query to query window.
Image may be NSFW.
Clik here to view.
3. Click “Play” button or press F5 key to execute query. You will see the result in result pane.
4. Click SQL tab in result pane, where you can find OData query for REST endpoint.
Image may be NSFW.
Clik here to view.
Use the Web API (Preview) Driver
Next, Web API Driver. Please note that Web API is still preview and you have to use Dynamics CRM Online organization which 2015 Update 1 is applied and the feature is enabled.
Enable Web API (Preview)
1. Login to Dynamics CRM Online and navigate to Settings | Administration | System Settings.
2. Click “Previews” tab and enabled Web API.
Image may be NSFW.
Clik here to view.
Create Connection
1. Open LINQPad and click “Add Connections”. Select “Dynamics CRM Web API Linq Pad Driver” in Choose Data Context and click “Next”
Image may be NSFW.
Clik here to view.
2. If you want to use your own ClientId, then uncheck “Register to Azure AD automatically” I will explain what it is later in this article. Click “Login to CRM”. Login to the organization where you enabled Web API preview.
3. Then it automatically starts doing its work and generates a DataContext from the selected organization. Wait until it’s done.
4. Click “Exit” button once loading completed. Then LinqPad starts loading the schema, which takes a bit of time. Wait until you see the schema information on the left pane like below screenshot.
Image may be NSFW.
Clik here to view.
Write LINQ query and Execute
1. Firstly, select added connection from “Connection” dropdown on the top right.
Image may be NSFW.
Clik here to view.
2. Enter following query to query window.
Image may be NSFW.
Clik here to view.
3. Click “Play” button or press F5 key to execute query. You will see the result in result pane.
4. Click SQL tab in result pane, where you can find OData query for Web API endpoint.
Image may be NSFW.
Clik here to view.
Write Statements and Execute
1. To try functions, select “C# Statements” from Language and write Query like below.
Image may be NSFW.
Clik here to view.
2. SQL tab gives you OData query, too.
Image may be NSFW.
Clik here to view.
3. In addition to simple function, you are able to try more complex statements like below. This sample code creates an account, and create an opportunity by referecing the account as its parent.
// Create Account record.
DataServiceCollection<account> accounts = new DataServiceCollection<account>(this);
account myaccount = new account();
accounts.Add(myaccount);
// Specify field values
myaccount.name = "Test Account";
// Then call SaveChanges to create it.
this.SaveChanges(SaveChangesOptions.PostOnlySetProperties);
// Create Opportunity record.
DataServiceCollection<opportunity> opportunities = new DataServiceCollection<opportunity>(this);
opportunity myopportunity = new opportunity();
opportunities.Add(myopportunity);
// Specify field values
myopportunity.name = "Test Opps";
// Assign the account
myopportunity.opportunity_customer_accounts = myaccount;
this.SaveChanges(SaveChangesOptions.PostOnlySetProperties);
Console.WriteLine("Opportunity Created");
Azure AD Application Registration
For authentication, Dynamics CRM Online REST based web service endpoints only support OAuth 2. To use OAuth 2.0, you need to register your application (in this case, LINQPad driver) to Azure AD or AD FS (depending on your deployment), which typically requires you to perform manual work. (See Walkthrough: Register a CRM app with Active Directory for more details). To eliminate this manual work, these drivers have a built-in Azure AD Application Registration feature. If you are interested in how to do this, please see the source code.
By default, it uses your CRM login credential to register the application. However if the credential does not have enough privilege, you will be prompted to enter another credential. If you do not know about this and other people managing your Azure AD, you can ask them to register your application and get the ClientId. Once you get the ClientId, you can use it when adding the connection. Uncheck “Register to Azure AD” checkbox and enter ClientId manually.
Image may be NSFW.
Clik here to view.
This tool registers your application as follows. If the same application name already exists, then it reuses existing ClientId.
Image may be NSFW.
Clik here to view.
If you are using On-Premise Server for REST endpoint, the driver uses Windows Integrated Authentication, so it is skipping application registration part.
Ken
Premier Mission Critical/Premier Field Engineer
Microsoft Japan