UPDATE: Thanks to some internal discussions with folks on the SDK team, I discovered a simpler way that doesn’t require manually putting the deviceid/devicepassword in the config file. I decided to leave this post as-is for two reasons. First, because the easier way requires full trust and won’t work for you if your hoster or IT department requires partial trust. Second, so people understand what the easier way automates. I’ve updated this post with a “you can skip this step” explanation.
“I want to make calls to CRM from an ASP.NET page, .NET middle tier code, etc. How do I do it with CRM Online?”
Simple answer right? Maybe for the persistent developer who is happy to reverse engineer the steps from the SDK documentation. However, I have to say the SDK doesn’t spell it out for you step by step.
The following resources seem to help on the surface:
But there’s a little CRM Online nuance that sort of gets lost in the shuffle unless you connect the dots across some of these articles. CRM Online, when using a Live ID, requires that you use a DeviceId and DevicePassword when connecting. While the Simplified Connection to Microsoft Dynamics CRM article does explain this, it leaves it up to the reader to hunt down the whole Device ID setup process. This post is my attempt to connect the dots in a step by step walkthrough of building a simple ASP.NET Web Forms page that connects to CRM Online using a Live ID.
Let’s start by creating a new ASP.NET Empty Web Application and call it CrmOnlineConnectedWebApp:
Add the following references from the
Add the following references from .NET.
You can skip this next step and jump to CREATE EARLY BOUND TYPES if you follow the enhancement in the Eliminating manual device registration post.
There’s a section titled “To Generate your individual device ID and password” in the Create Early Bound Entity Classes with the Code Generation Tool (CrmSvcUtil.exe), but it’s easy to miss. Follow the instructions from that section:
- Open and build the DeviceRegistration project: SDKToolsDeviceRegistrationDeviceRegistration.csproj.
- Run the executable file from the command line. To register your device, set the /operation parameter to Register.
- C:deviceregistration.exe /operation:Register
- Copy the displayed device ID and password values and use them as the deviceid and devicepassword parameter values when you run the CrmSvcUtil tool.
The easiest way to copy the Device ID and the Device Password from the console is to right-click and select all:
…right-click again (this will copy the contents) and paste into notepad. Then you can copy the actual values.
CREATE EARLY BOUND TYPES
Let’s create some early bound types as described in step one of Walkthrough: Build a Web Application That Connects to Microsoft Dynamics CRM 2011 Using Developer Extensions. However, we need to remove the domain flag:
CrmSvcUtil.exe /codeCustomization:"Microsoft.Xrm.Client.CodeGeneration.CodeCustomization, Microsoft.Xrm.Client.CodeGeneration" /out:Xrm.cs /url:[YOUR_CRM_ROOT]/XRMServices/2011/Organization.svc /username:[YOUR_USERNAME] /password:[YOUR_PASSWORD] /namespace:Xrm /serviceContextName:XrmServiceContext
Now add the generated file to your Visual Studio project. Now is a good time to build. If the build fails, you are likely missing an assembly reference. Now let’s add a Web Form called Default.aspx:
Add a GridView to the markup of Default.aspx:
The next steps are slightly different if you decided to use the approach in my Eliminating manual device registration post. Switch to Default.aspx.cs and paste the following code into the Page_Load handler:
The final step is to put the CrmOnline connection string into your web.config:
You should be able to run the app now and see data in the GridView.