SharePoint 2010: Connecting to WCF service as External Content Type

Posted by: Mahesh Sabnis , on 4/12/2012, in Category SharePoint
Views: 116647
Abstract: By using WCF Service with Business Data Connectivity, a SharePoint 2010 portal can easily connect to any external system within the organization or even to an external software system outside the organization. In this article, we will see how to connect between a WCF service and Sharepoint 2010

When you develop a SharePoint 2010 portal for an Organization, your portal may need to connect to an external software system for e.g. an already available accounting software system. To establish such communications in SharePoint 2010, we can use the Business Data Connectivity Web Service. This service helps to establish communication between the SharePoint 2010 portal and an external data source like SQL Server, WCF and other .NET types.

In this article, I am going to explain how to establish connection between SharePoint 2010 portal and WCF service. To consume WCF service, I have used SharePoint Designer 2010.

 

Note: To learn how to establish connection between SP2010 and SQL Server, read my previous article

SharePoint 2010: Connecting to SQL Server Using the External Content Type Feature

For this article, I already have created a Web Site ‘http://MyServer/sites/ss’.

Step 1: Open VS2010 and create a new WCF Service Application, rename ‘IService1.cs’ to ‘IService.cs’ and ‘Service1.cs’ to ‘Service1.cs’. Open ‘IService.cs’ and add the following method and class in it:

iservice

Step 2 : Open Service.cs and implement the ‘IService’ interface in Service class:

iservice-implementation

Step 3: Open Service.svc in markup by right-clicking on it and change the Service attribute as shown below:

<%@ ServiceHost Language="C#" Debug="true" Service="WCF_For_SPS.Service" CodeBehind="Service.svc.cs" %>

Step 4: Publish the Service on IIS (IIS 7.0). The communication will happen in the following manner:

sp2010-wcf

 

Communication from Website to WCF

Now to communicate from the Web Site to WCF, follow these steps

Step 1: Navigate to the ‘http://MyServer/sites/ss’ web site. Click on ‘Site Actions’ on the top left, and select ‘Edit in SharePoint Designer’ as below:

edit-sharepoint-designer

Step 2: The SharePoint 2010 Designer will open with the Site. Click on the ‘External Content Type’ from the left pane as shown below:

external-content-type

This action will open the ‘External Content Types’ window.

Step 3: Click on the ‘External Content Type’ on the top left. This will open the ‘New External Content Type’ window as shown below:

new-content-type

Set the Name of the External Content Type as shown below, the Office Item Type is set to ‘Generic List’

office-item-type

Click on ‘Click here to discover external data sources and define operations’ and you will get the ‘Add Connection’ as shown below:

add-connection

Click on ‘Add Connection’. You will see the ‘External Data Source Type Selection’ window. Select WCF from it as shown below:

select-wcf-service

A ‘WCF Connection’ window appear where you can enter the WCF Service Metadata URL and Service EndPoint URL as shown below:

Note: Instead of the localhost please specify IP address or name of the server.

wcf-connection

The WCF Service will be added in the SharePoint 2010 Designer as shown below:

data-source-explorer

Step 4: Now once the WCF Service is configured with the Site, we need to define Operations for the List generated. Right Click on the ‘GetEmployees’ method and select ‘New Read List Operation’:

new-read-list

This will start the ‘Read List Wizard’ as shown below:

read-list-wizard

Click on ‘Next’ and you will get the ‘Input Parameters Configuration’ window. Since there is no input parameter for the method, you can simply click on ‘Next’. You will get the ‘Return Parameter Configuration’ screen. Since the method returns an Employee List, we need to set an identifier as shown below:

return-parameter

Click on ‘Finish’. You will get the ‘GetEmployee’ operation List.

Step 5: Right Click on ‘GetEmployeeByEmpNo’ and select ‘New Read Item Operation’ as shown below:

new-read-item-operation

 

This will bring up the ‘Read Item‘ Wizard window

read-item-wizard

Click on ‘Next’ and you will see the ‘Input Parameters Configuration’ window, for this method the input parameter is ‘EmpNo’, we need to map it with Identifier as below:

input-parameter-config

Click on ‘Next’ again. Since the return parameter for this method is an ‘Employee’ object, we need to set ‘EmpNo’ as map to the Identifier as shown below:

map-to-identifier

Click on ‘Finish’ and you will get the ‘GetEmployeeByEmpNo’.

Step 6: Let’s move to add Insert operation. To do this, right –click on ‘InsertEmployee’ method and select ‘Create New Operation’ as below:

new-create-operation

This will start the ‘Create’ Wizard as shown below:

create-wizard

Click on ‘Next’ to see the ‘Input Parameters Configuration’ window. Once again, since the input parameter for this method is the Employee object, we need to set ‘EmpNo’ as Map to the Identifier as shown below:

input-param-config

Click on ‘Next’.  Since there is no output parameter for this method, you can simply click on Finish. You should get all the operations listed as shown below:

external-operatios

 

Save the configuration by clicking ‘Save’ button on the top left of the SharePoint 2010 Designer.

Step 7: Now to create a List, click on the ‘Create List & Form’ on the Ribbon of the SharePoint 2010 designer as shown below:

create-list-form

In the ‘Create List and Form for EmployeeWCFServiceList’ window, enter the List Name as shown below:

create-list-form-2

This creates a List on the Web Site.

Step 8: Open the Web Site and you will find the ‘EmployeeEWCFListData’ list created in the Quick Launch bar

quick-launch

Step 9: Click on the List and you will see the following result

employee-wcf-listdata

You must be wondering why do you get the ‘Access denied by business data connectivity’ error? The reason can be understood by this simple architecture diagram shown below:

sharepoint-bcs-architecture

Since the user has to be impersonated by the WCF Service, we need to set the access rights for the user.

Step 10: To set the Access rights, Open SharePoint Central Administration > ‘Application Management’ > select ‘Service Applications’ and click on ‘Manage Service Applications’. You will see the ‘Business Data Connectivity Service’. Click on it and you will get the External Lists as shown below:

service-app-info

Select ‘EmployeeWCFServiceList’ and click on the ‘Set Object Permissions’ on the Ribbon. The ‘Set Object Permissions’ window appears. Add the user in it as shown below:

set-object-permissions

Step 11: Refresh the Web Site. The result will be as shown below:

list-data

Step 12: Select a Record on the List and click on ‘View Item’ as shown below:

view-item

employee-data-list-item

Step 13: To insert a record, click on ‘List Tools’ on the Ribbon and select ‘Items’ from it. Then click on ‘New Item’ as shown below:

list-data-new-item

Step 14: The New Item Window appears. Enter the record info in it as shown below:

add-record

Click on ‘Save’ and the newly added record appears in the List

newly-added-record

Like wise, you can implement the Update and Delete methods also.

Conclusion

With the External Content Types feature provided in SharePoint 2010, communication between two systems can be achieved easily. By using WCF Service with Business Data Connectivity, a SharePoint 2010 portal can easily connect to any external system within the organization or even to an external software system outside the organization.

The entire source code of this article can be downloaded over here

Give a +1 to this article if you think it was well written. Thanks!
Recommended Articles


Page copy protected against web site content infringement by Copyscape


User Feedback
Comment posted by vikranth on Tuesday, June 5, 2012 5:31 AM
Hi i want same functionality with oracle database how to do.
Comment posted by Gangi Reddy on Friday, June 22, 2012 12:01 AM
Nice........
Comment posted by Mahesh Sabnis on Saturday, June 23, 2012 6:09 AM
Hi Vikrant,
  Use Oracle provider for MS.NET and integrate a code with WCF.
USE ADO.NET EF with Oracle and integrate it with WCF and then expose to SPS ECT.

Regards
Mahesh Sabnis
Comment posted by Yasser on Thursday, July 19, 2012 2:40 AM
can you tell me how to publish wcf service on iis
Comment posted by Praveen on Monday, August 13, 2012 3:30 PM
Hi Mahesh,

I have created ExternalList as mentioned in your article but when i open that External List, it is displaying error message.

"The server was unable to process the request due to an internal error. For more information about the error, either turn on IncludeExceptionDetailInFaults (either from ServiceBehaviorAttribute or from the <serviceDebug> configuration behavior) on the server in order to send the exception information back to the client, or turn on tracing as per the Microsoft .NET Framework 3.0 SDK documentation and inspect the server trace logs."

I update the  <serviceDebug httpHelpPageEnabled="true" includeExceptionDetailInFaults="true"/>
in web.config file but still it is displaying same error.
Kindly suggest me.

Thanks,
Praveen
Comment posted by Mahesh Sabnis on Wednesday, September 26, 2012 11:28 AM
Hi Yaser,
  Please visit to google and put Mahesh Sabnis + WCF + iis you will het the walkthrough.
Thanks
regards
MAhesh Sabnis
Comment posted by Mahesh Sabnis on Wednesday, September 26, 2012 11:30 AM
Hi Praveen,

Please attach the debugger w3wp.exe to the service and debug, there may be database connectivity problem from WCF service if it is hosted on IIS.
Thanks
Regards
MAhesh Sabnis
Comment posted by Geoffrey Cleaves on Wednesday, October 31, 2012 5:06 PM
To connect with user's identity instead of impersonated identity, is it necessary to be using Claims based authentication and Kerberos?
Comment posted by asdad on Monday, January 28, 2013 6:55 PM
Adding the code as images, that's very smart. Thanks.
Comment posted by KK on Tuesday, February 26, 2013 11:44 AM
How can I connect MS Access database for the same functionality?
Comment posted by KK on Tuesday, February 26, 2013 12:03 PM
How can I connect MS Access database for the same functionality?
Comment posted by Kishor on Thursday, February 28, 2013 11:14 PM
Great Post
Comment posted by Sasho Jovevski on Wednesday, June 5, 2013 4:19 AM
Hi Praveen,
I cannot reach my wcf external list from internet. I can reach from intranet only. The wcf service is configured with a basicHttpBinding and no authentication is required. I can reach the service directly from Internet with any client, but I cannot reach from my sharepoint 2010 foundation external list. Pleasehelp me? It makes me crazy...
Comment posted by Juan Carlos Escalante on Wednesday, November 20, 2013 9:14 AM
Hi Mahesh,

Please help me sort this one out. I've been all over the web with no luck.  I am facing the same problem as Praveen... I enable tracing for the WCF service but cant find any error message on the logs.

Any idea on what to check?

I really appreciate your help.

Regards,

JC
Comment posted by Suhel on Friday, December 13, 2013 5:39 AM
Very Good ,Article this can encourage real time practice
Comment posted by Jayaprakash Reddy on Friday, December 20, 2013 2:47 AM
It's very good article
Comment posted by Aritro Mukhegrjee on Wednesday, April 16, 2014 4:18 AM
Hi Mahesh,

This is an Excellent article . I rate this as 9.99 out of 10. I only want to know If I want to debug my .net WCF code how can I do it.

Aritro
Comment posted by Aritro Mukhegrjee on Wednesday, April 16, 2014 7:17 AM
Hi Mahesh,

This is an Excellent article . I rate this as 9.99 out of 10. I only want to know If I want to debug my .net WCF code how can I do it.

Aritro
Comment posted by Mahesh Sabnis on Sunday, July 13, 2014 11:52 PM
Hi Aritro Mukhegrjee ,

  Thanks  a lot. To debug WCF host it on IIS and then attach the W3WP.exe process to the WCF from VS instance.

Regards
Mahesh Sabnis

Post your comment
Name:  
E-mail: (Will not be displayed)
Comment:
Insert Cancel