Friday, March 1, 2013

Connecting to SQL Server Using the External Content Type Feature

Abstract: With the External Content Types feature provided in SharePoint 2010, communication between two systems can be achieved easily. By using SQL Server as Data Source, you can minimize writing explicit code for database connectivity and perform CRUD operations too.

As a SharePoint 2010 developer, I have come across various requirements from clients on connecting to external data sources e.g. Direct Connectivity to Database, Windows Communication Service (WCF) and External .NET type from a SharePoint Web Site. By considering its need in portal development, I decided to pen down my thoughts on this topic. This article is a result of the same.
Practically there are many scenarios when a SharePoint site needs to connect to an external system which stores data inside it. E.g. A Payroll system which calculates salary of the Employee in the organization wants to communicate with a Leave Management and Processing portal for calculating total leaves of Employee. To do this in SharePoint 2010, we have been provided with a facility of integrating external data provided by Database, WCF and Web Services and .NET assemblies.
In this article, I have explained the mechanism of integrating with external data provided by Sql Server Database. For this article I have used a sample ‘Customer’ Table in the ‘Company’ Database:
customer-table
For this article, I already have a Team Site on address ‘http://MyServer/sites/ss’. (Note: You can follow all these steps on the site created by you.) To follow my steps, I recommend you to create aCompany Database in SQL Server and create the Customer table shown above in this database.
Prerequisites:
  • Sql Server 2008 R2.
  • SharePoint 2010 Server.
  • SharePoint 2010 Designer.
Step 1: Browse the site created by you > Select ‘Site Actions’ and select ‘Edit in SharePoint Designer’. This will start the SharePoint Designer.
sharepoint-designer
Step 2: SharePoint 2010 Designer now shows the Site Information. From the left side pane, select ‘External Content Types’ as below:
external-content-types
This step will retrieve the External Content type.
Step 3: To create a new ‘External Content Type’, click on ‘External Content Type’ in the Top left corner of the Ribbon as below:
new-external-content-type
This will show the window from where you can set the External Content Type Information. Here in this window, you can set the following Information:
1. Name of the External Content Type Information
  • Display Name.
  • Office Item Type, this is a List type e.g. Contact, Task, Post, Appointment and Generic List.
  • External System, using which the Data Source can be discovered, in this case our Data Source will be Sql Server.
2. External Content Operations: This is used to define Operations which can be performed on the External Data Source e.g. Create, ReadList, ReadItem, Update and Delete etc.
external-content-operation
Step 4: Set the External Content Type information as below:
set-external-system
Now Click on the ‘Click Here to discover external Data sources and define operations ’ as shown above, you will get the following window using which you can add connections to external content type:
add-connection
Click on ‘Add Connection’. You will see a window using which you can select ‘External Data Source type’ > Select Sql Server as below:
external-data-source-type
After clicking OK, you will be asked to enter Database information as shown below:
sql-server-connection
Click on ‘OK’ and you will get the Database Connection in Data Source Explorer. Expand it and locate the ‘Customer’ table as shown below:
data-source-explorer
In this step, you have completed the Connection part.
Step 5: Now after establishing the connection withan External Data Source, it’s time for us to define possible operations on the Data Source. So right click on ‘Customer’ and select all Operations from the Context menu as shown below:
create-all-operations
The above image shows self-describing operation types. After selecting ‘Create All Operations’, the wizard will start as shown below. Please read the Instructions.
all-operations
Click on ‘Next’. The next window will show the Parameter Configuration as shown below. Note: Here you can ignore the Warnings.
parameters-configuration
Click on ‘Next’. Here it is optional for you to set filter parameters which allows you to set the size of the result set. Click on ‘Finish’. The following operations will get generated as shown below:
external-content-type-operation
Step 6: Once it is done, click on the ‘Save’ button on the top-lfet.
Step 7: Now to create the List, click on ‘Create List & Form’ button on the Ribbon
create-list
After clicking on ‘Create List & Form’, you will see a form where you can enter List name and other information as shown below. If you want to create an InfoPath form, you can check the CheckBox ‘Create InfoPath Form’
customer-info-list
Step 8: Now go back to the Web Site and Refresh. You will see the ‘CustomerInfoList’ in the Quick Launch window as shown below:
quick-launch
Step 9: Click on the ‘CustomerInfoList’ and you will get the following result:
access-denied
You must be wondering why did this happen? What does the error ‘Access denied by Business Data Connectivity’ mean? The reason for this error is that, BCS is an external web service which is hosted on IIS and to establish connectivity between the Service Hosted on IIS and a database, an explicit authorization is required. So now we need to do some configuration here.
Step 10: Open SharePoint 2010 Central Administration and Select Application Management. From ‘Application Management’ > select ‘Service Applications’ and click on ‘Manage Service Applications’. You will get a list all the services which are running. From this page, click on ‘Business Data Connectivity Service’ > you will get ‘Service Application Information’ as shown below:
service-application-info
Step 11: Select the ‘CustomerListInfo’ and click on ‘Set Object Permissions’ from the Ribbon. You will get a window for Setting Object Permissions. Add the user info as shown below:
object-permission
Click ‘OK’
Step 12: Now go back to your site and click on the CustomerInfoList from Quick Launch. You will get the following result:
result
From here, you can perform ‘Create’, ‘Update’, ’Delete’ and ‘Read’ operations.
Conclusion: With the External Content Types feature provided in SharePoint 2010, communication between two systems can be achieved easily. By using SQL Server as Data Source, you can minimize writing explicit code for database connectivity. In cases where you have a local database available with you, you can use the SharePoint 2010 portal directly to perform CRUD operations with your data source.

Connecting to WCF service as External Content Type

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.

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.