SharePoint of Contact 2: Using CRM Online Data within SharePoint
In this previous blog post, I explained how to integrate Microsoft Dynamics CRM with SharePoint 2010 using an External Content Type (ECT) linked to the Dynamics SQL Server instance. Today I will explain how to integrate Microsoft Dynamics CRM Online with SharePoint 2010, also using an ECT. Since CRM Online data is only accessible via web services, we will use a WCF web service-based ECT.
First a note about why CRM Online data is only accessible via web services. CRM Online is Microsoft's cloud-hosted CRM solution, so the location of the databases containing your data is not local. For this reason, we will not be able to pursue the strategy outlined in the first blog post, but will instead use the web services provided by CRM Online.
Next, I will note that the CRM Online web services can only be used by a client running .NET 4. SharePoint 2010 is built with .NET 3.5, so SharePoint's Business Connectivity Services (BCS) cannot connect directly to the CRM Online web service. Instead, we will build an intermediary web service that runs on an application pool using the .NET 4 runtime and has a BCS-compatible interface.
To start, set up a CRM Online account (if you do not already have one )and associate it with a Windows Live ID. Once you can access your CRM Online account in a web browser, download the CRM 2011 SDK.
Within the "bin" directory of the CRM SDK find the crmsvcutil executable. This tool will generate the CRM data model used by your CRM Online site in code so that we will be able to use Linq to query CRM data. The syntax of the tool follows.
crmsvcutil.exe /url:https://[Site Name].crm.dynamics.com/XRMServices/2011/Organization.svc /out:CrmDataModel.cs /username:"[Live ID]" /password:"[Password]"
The file CrmDataModel.cs will be used in your web service.
Next, open up Visual Studio 2010 and create a new project based on a WCF Web Service template. Be sure to set the .NET Framework version to 4. Add CrmDataModel.cs to the project.
I would also suggest deleting the default web service item from the project and adding a new one with a more specific name.
Next, flesh out the web service so that it can gather the desired entities from CRM Online and deliver them to the web service consumer. Please see this blog for example code on how to use the CRM data model. If designing the web service to be used as part of an ECT, keep in mind the CRRUD operations (Create, Read List, Read Item, Update, Delete) that an ECT supports.
Next test out the web service. Visual Studio 2010 has a great point-and-click client for testing out web services that is opened when you begin debugging the web service through the IDE. If testing on a server that does not have Visual Studio 2010 installed, I have found that WCFStorm Lite is a great tool for reproducing the behavior of the Visual Studio WCF client.
Once you have verified that the web service is functional, you will have to create a web site and application pool to host it. Please remember that since this web service is unsecured, it should run either on a server that is not externally accessible or on a closed port. When adding the web site, make sure to grant the user IIS_IUSRS read/execute permissions to the site. This can be done through IIS Manager by right-clicking on the site folder and selecting Permissions. Also, remember when creating the application pool to be used by this site to set it to use the .NET 4 Framework. This can be done by selecting the application pool in IIS Manager and configuring Basic Settings. Lastly in IIS set the "Load User Profile" option to true under the Advanced Settings of the application pool.
Add the web service to the site by selecting the WCF project folder you created as a web application. This should be the location of the .svc files generated when building the project. You may need to grant permissions to this application to IIS_IUSRS. Fire up a browser and navigate to the new site you created - you should receive a page summarizing the web service.
If installing the web service on a server different than that hosting Visual Studio, be sure to copy the .svc files, the web.config configuration file, and the code assembly containing the service's logic. This assembly should be placed in a "bin" folder that resides at the same location as the web application folder holding the .svc files.
To test the web service, debug one of the service's code-behind files, or start up WCFStorm. If debugging through Visual Studio 2010, the methods available through the service should be available, as well as the arguments that can be passed to each method. The interface for WCFStorm is similar.
After the web service has been tested and the data returned has been verified, you can create an ECT using the WCF service rather than a SQL Server view as explained in the previous SharePoint-CRM integration blog post. You can also use the web service as a service reference in another .NET project - with any framework version.