SharePoint 2010 Web Parts and CRM 2011

Author by John Adali

Recently I was asked to create a web part in SharePoint 2010 that would allow users to create and update entities in CRM 2011. There were some interesting challenges to overcome, so I thought I’d detail the process I went through, so others could benefit from my experience.

Initially, I thought this task would be easy, as the CRM 2011 SDK provides sample code that calls into the CRM 2011 web services to perform CRUD (Create, Read, Update or Delete) operations. However, I quickly discovered that the CRM 2011 SDK code requires references to the .NET Framework 4.0 DLLs, and since SharePoint 2010 web parts do not support .NET 4.0, I encountered compilation errors in my web part project.

The solution I came up with was to create a “middle tier” WCF web service that would perform the calls to the CRM 2011 web services. This WCF web service can be created using the .NET Framework 4.0 to get past the compilation errors, and the ServiceContract can be defined with methods that mirror what is provided in the CRM 2011 web services.

I started by creating a new WCF Services project in Visual Studio 2010, selecting the WCF Service Application project template, and making sure I selected .NET 4.0 as the framework:

Once the new project is created, I was able to define the ServiceContract for the web service, as shown below:

namespace CRM2011MiddlewareService { [ServiceContract] public interface IService1 { [OperationContract] string GetMessage(); [OperationContract] void AddCRMAccount(string strAccountName); [OperationContract] void AddCRMCase(string strCaseName, string strAccountName); } }
Figure 1: Code for ServiceContract

I also needed to add service references in the project to the CRM web services, as listed here:




Once this “middle tier” web service is published to IIS, it can be consumed by a typical web part project in Visual Studio 2010. I created this visual web part project and added a service reference to my “middle tier” web service, so I can call the methods that will create the CRM entity I am interested in creating.

namespace VisualWebPartProject1.VisualWebPart1 { public partial class VisualWebPart1UserControl : UserControl { protected void Page_Load(object sender, EventArgs e) { } protected void Button1_Click(object sender, EventArgs e) { Service1Client proxy = new Service1Client(); string strCaseName = txtCaseName.Text; proxy.AddCRMCase(strCaseName, ""); } } }
Figure 2: Code for Visual Web Part Project

From here, creating a simple visual web part for SharePoint 2010 is a piece of cake, and once it is deployed and activated in SharePoint, I was able to test it out to make sure it works properly.

In conclusion, creating a web part in SharePoint 2010 to consume methods from the CRM 2011 web service requires that you create a “middle tier” WCF web service to call the CRM 2011 web service. This “middle tier” web service can then be called by your web part in SharePoint 2010. My example here just scratches the surface of what the CRM 2011 web service provides, but shows the process I took to get a web part in SharePoint 2010 working with CRM 2011.

Download the Source Code




John Adali

Technical Architect - Dynamics CRM