Tuesday 1 November 2011

Business Connectivity Services Using WCF in SharePoint 2010

Business Connectivity Services(BCS)

1. What BCS is and what does it provide?
  • BCS is a set of services that facilitates the connection between SharePoint solutions and external systems like Databases, web services, WCF services, .NET connectors etc.
  • BCS=BDC + much more....
         which means that BCS have the capabilities of BDC(Business Data Catalog) + some other       capabilities which were not supported by BDC in Sharepoint 2007. Some of these capabilities are :

                a. Read/Write capability: BDC provides only read functionality whereas BCS provide read and write both.
                b. Connectivity Options: BCS provides variety connectivity options like connecting to SQL Server or through .NET Connector or through the WCF Service or web services.
                c. Search Functionality: BCS can also be used to search external data using the Search Application Service.
                d. Offline Access: In BCS, you can work with offline or cache mode and synchronize with external systems when online.

In this post I would be discussing how to create an External Content Type using WCF and SharePoint Designer 2010, which will further be used to create an external list so that the users can interact with the External data through this list.

Steps for Creating External Content Type Using WCF:

1. So the first step is to create a WCF service which will interact with the database. This is a simple WCF Service having code to perform all the CRUD operations on the Customers table. Customers table have three fields: CustomerId, CustomerName and CustomerCity.
I have added a Dataset in my WCF Service Application which will have all the CRUD operations defined.

Below is the image for the WCF contract:




And below is the image for service implementation:






The methods defined in the WCF service will be used while creating the external content type.



2. After creating the WCF service, build the service or deploy it to IIS.



3. Once the WCF service gets created or deployed, navigate to the site collection or site where you want to create the external list and open the site collection or site in SharePoint Designer 2010.

4. In the SharePoint Designer, click External Content Type in the left pane and then click External Content Type in the top menu bar to create a new content type.







5. Specify a Name for the content type and then click "Click here to discover external data sources and define operations." or click on Operations Design View in the top menu.


6. Next, click Add Connection to establish a connection with the external data source and select WCF Service as the Data Source Type in the External Data Source Type Selection window.

7. In the WCF Connection dialog box, enter the Service Metadata URL say for example(http://localhost:26494/CustomerService.svc?wsdl) in the Service Metadata URL text box and the Service URL(http://localhost:26494/CustomerService.svc) in the Service Endpoint URL text box. You can also specify the Name for the Connection.

8. Check the Use Proxy Server checkbox so that the connection uses the proxy server for interacting with the WCF service and then click OK.



9. You can notice that all the methods which we defined in our WCF service i.e. GetAllCustomers(), GetCustomerData(), AddCustomer(), UpdateCustomer() and DeleteCustomer() gets displayed in the Connection which we created.


Sometimes it happens that the connection does not work with our application credential, so to make the connection working we need to configure the Secure Store Service so that the user can perform the CRUD operations on the external list without any error or credential issues. What Secure Store Service do is it maintains an encrypted database that maps the identities of SharePoint users, groups, or process accounts to the external credentials required to access external systems. When the Business Data Catalog (BDC) needs to impersonate external credentials to access a data source, it passes the identity of the caller to the SSS. The SSS then returns the external credentials that are mapped to the identity of the caller.

So the steps to configure the Secure Store Service are as follows:

1. Navigate to Manage service applications in Central Administration and then create a new Secure Store Service by clicking New-> Secure Store Service.



2. Specify the Name and Application pool name for the Secure Store Service. Also specify the Database Server in which the Database for Secure Store Service will be created.
Then click OK.


3. If its the first time in your Secure Store Service, then you can see a message saying that you must configure it. So we will "Generate New Key" as shown in the below image:


4. Enter the valid pass phrase in the Generate New Key dialog box and click OK.

5. Next step is to create  a New Secure Store Target Application.

Within the SSS(Secure Store Service), credentials mappings are organized by target applications. A target application represents an external system or data source, and includes a unique target application ID. When the BDC requests a set of credentials from the SSS, it specifies the target application ID so that the SSS knows which credential mapping to retrieve.

Click New in Manage Target Applications tab in the ribbon to create a new target application.

6. Specify the Target Application ID, Display Name and Contact Details, then select Target Application Type for mapping individual users.

7. Select None in the Target Application Page URL and then click Next.
 


8. In the next page leave the credentials fields as it is and click Next. You can also add any fields if needed.


9. On the next page, in the Target Application Administrators text box, add your administrative account and click OK.

This will create the Secure Store Target Application which will map the SharePoint users to data source or external systems credentials.

10. The final step in Secure Store Service configuration is to set credentials and to do that select the Secure Store Target Application which we created and click Set in the Credentials tab on the ribbon.


9. Next, in the "Set Credentials for Secure Store Target Application(Individual)" dialog box, provide the credentials as required to access the external system. You can map any user or group to external systems credentials. See the below image:




After the connection is established with the WCF service and Secure Store Service is configured, its time to configure our external content type using this Connection.


Thus, to configure the External Content Type, right click the method say GetAllCustomers Web Method which returns a collection of items and click New Read List Operation  to create the operation for the external content type which will read all the items in the Customers table and will display them in a list created by this external content type. Read List operation is Finder method whereas Read Item operation is a Specific Finder method i.e. the Read List method will return the collection of items and the Read Item method will returns only the item whose Id matches with the selected list item in Sharepoint.

10. Now, in the Read List operation dialog box specify the Name for the operation and click Next.


11. Click Next again in the Input Parameters as we dont have any input parameters in the Read List operation since it returns all the entries from the Customer table.

12. Next, in the Return Parameter select CustomerId and click Map to Identifier in Properties tab to map each entry with a unique CustomerId and then click Finish.

13. Similarly, create other operations like method to read a specific item, add an item in the external list, update an item or delete an item in the external list. We can also create any custom method doing some other functionality other than the CRUD operations.

Note: Create methods of appropriate type for different operations i.e. use New Create Operation for the method which will add a new customer to the list(AddCustomer in WCF service) and New Update Operation for the method which will update the list item etc...

So, this is how the External Content Type will be configured for the CRUD operations using WCF service.
14. Now Save this External Content Type.


Our next step is to create an External List using this External Content Type. To do this the steps are as follows:
1. Navigate to the site or site collection where you wnat to create the External List and click All Site Content in the left navigation.
2. Next, click Create and select External List in the List menu and click Create.

3. Then specify the Name of the list and Select the External Content Type as the one which we created using WCF service and then click Create.

The External List having the external data source's entries gets created. You can now perform the CRUD operations on this list as we defined in our WCF service and the changes will get affected to the external system as well.






















1 comment: