Create External Content Type based on SharePoint Lists


Introduction:

Business Connectivity Services (BCS) is a new service introduced with SharePoint 2010 to allow SharePoint sites to connect to and manipulate external data. SharePoint 2007 had a similar facility in the form of Business Data Catalog (BDC) which made external data available within its site. However, a major problem with BDC was the difficulty in creating solutions as there was no support in the 2007 designer.  Most BDC solutions were simply for accessing external data, manipulating external data sources was extremely difficult.

With SharePoint 2010, BCS ships with out-of-box features such as solutions, services, and tools which make connecting to external data an easy task. Whether you want to retrieve Outlook contacts in a list offline or edit the contents of your document file or share your excel sheet online or reuse data from dynamic InfoPath forms or just update your business presentation, BCS enables deep content sharing, editing and integration in SharePoint 2010 with SharePoint Designer and Visual Studio tools.

Description

SharePoint 2010 out-of-the-box exposes list data as OData feed. Here we will be consuming the OData feeds using Business Connectivity Services. We can retrieve the list data by the URL http://serverName:1111/_vti_bin/listdata.svc. We will be adding the service reference to the OData feed and will be generating the code in Visual Studio 2010. The following example shows how to create External Content Type based on SharePoint list. I have created a custom list named "Customers" which has the following data's.


Prerequisites

  • Microsoft SharePoint Server 2010 or Microsoft SharePoint Foundation 2010 installed on the server

  • Microsoft .NET Framework 3.5 and Microsoft Visual Studio 2010


Example

Create SharePoint Solution based on Business Data Connectivity Model template:

  • Open Visual Studio 2010.

  • Go to File => New => Project.

  • In the New Project dialog box, expand the SharePoint node, and then select the 2010 node.

  • In the Templates pane, select Business Connectivity Catalog Model.

  • Enter the Name and then click OK.


The SharePoint Customization Wizard appears.

  • In the What local site do you want to use for debugging page, type the URL of the SharePoint Web site you will use to test the model, and then click Finish.


  • An Empty SharePoint Project is created and a Business Data Connectivity Model item is added to the project. By default, the model appears in the BDC designer.


Delete the default BDC Model:

  • In the solution, right click on BDCModel1.

  • Click on Delete.


Create a new BDC Model:

  • Right click on the solution, Select Add => Click on New Item.


  • In the Templates pane, select Business Connectivity Catalog Model.


  • Enter the Name and click on Add.


Modify Entity1.cs:

  • In Solution Explorer, expand the BdcModel node, and rename Entity1.cs as Customers.cs.

  • Replace the code with the following


Modify the Entity:

  • In Solution Explorer, expand the BdcModel node, and then double-click the BdcModel.bdcm file.

  • The Business Data Connectivity model file opens in the BDC designer.

  • Right click on Entity1 and click on Properties.


  • In the properties, change the Name to Customers.


  • Right click on Identifier1 and click on Properties.


  • In the properties, change the Name to CustomerID and Type Name to System.Int32.


  • Right click on ReadList method and click on Delete.


  • Similarly right click on ReadItem method and click on Delete.


ReadItem (Specific Finder Method):

  • On the BDC designer, select the Customers entity.

  • On the View menu, click Other Windows, and then click BDC Method Details and it looks like the following one.


  • In the BDC Method Details window, from the Add a Method drop-down list, select Create Specific Finder Method.


Visual Studio adds the following elements to the model. These elements appear in the BDC Method Details window as shown in the following figure.

--A method named ReadItem. --An input parameter for the method. --A return parameter for the method. --A type descriptor for each parameter. --A method instance for the method.

  • In the BDC Method Details window, click the drop-down list that appears for the Customer Type Descriptor, and then click Edit.


The BDC Explorer opens. The BDC Explorer provides a hierarchical view of the model as shown in the following figure.


  • In the Properties window, click the drop-down list that appears next to the Type Name property, click the Current Project tab, and then select Customers.


  • In the BDC Explorer, right-click the Customers, and then click Add Type Descriptor.

A new type descriptor named TypeDescriptor1 appears in the BDC Explorer.


  • In the Properties window, set the Name property to CustomerID.

  • Click the drop-down list next to the Type Name property, and then select Int32. 

  • Click the drop-down list next to the Identifier property, and then select CustomerID.


  • Repeat step 6 to create a type descriptor for each of the following fields. 


In the BDC Explorer, ReadItem method looks like the following


ReadList (Finder Method):

  • In the BDC Method Details window, from the Add a Method drop-down list, select Create Finder Method.


  • The Type Descriptors of the return parameter have already been defined with the same structure as we just built above. This is because when creating a new method, BDC designer will search the possible Type Descriptors defined in the other methods of this entity and copy them to the newly created methods. 


Add Service Reference:

  • In the solution explorer, right click on References folder and click on Add Service Reference as shown in the following.


  • Enter the Address and click on Go.

  • Enter the Namespace and click on Ok.


Modify CustomersService.cs:

  • Add the following namespaces

-using CustomersBdcModel.ListServiceReference; -using System. Net;

  • Replace CustomersService.cs with the following

  1. using System;

  2. using System.Collections.Generic;

  3. using System.Linq;

  4. using System.Text;

  5. using CustomersBdcModel.ListServiceReference;

  6. using System. Net; 

  7. namespace CustomersBdcModel.BdcModel

  8. {

  9. public class CustomersService   

  10. {

  11. public static IEnumerable<Customers> ReadList()       

  12. {

  13. MPDataContext dc =new

  14. MPDataContext(newUri("http://serverName:1111/hr/MP/_vti_bin/listdata.svc"));           

  15. dc.Credentials =CredentialCache.DefaultNetworkCredentials;

  16. var customers = from e in dc.Customers select new Customers()               

  17. {                   

  18. CustomerID = (int)e.CustomerID,                   

  19. CustomerName = e.CustomerName,                

  20. };

  21. return customers;        

  22. }       

  23. public static Customers ReadItem(int customerID)       

  24. {

  25. MPDataContext dc =new

  26. MPDataContext(newUri("http://serverName:1111/hr/MP/_vti_bin/listdata.svc"));           

  27. dc.Credentials =CredentialCache.DefaultNetworkCredentials;

  28. var customers = from e in dc.Customers where e.CustomerID == customerID select new Customers()               

  29. {                   

  30. CustomerID = (int)e.CustomerID,                    

  31. CustomerName = e.CustomerName,               

  32. };

  33. return customers.FirstOrDefault();       

  34. }   

  35. }

  36. }


Deploy the solution:

  • Right click on the solution and click on Build.

  • Deploy the solution.

  • Go to Central Administration => Application Management => Manage Service Applications => Business Data Connectivity Services => You could see the content type that we have created as shown in the following.


Configure Business Data Connectivity access rights:

  • Go to Central Administration -> Application Management -> Manage Service Applications.


  • Click on Business Data Connectivity Service.


  • In the top Ribbon click on Manage.


  • In Service Application Information check the External Content Type ECT.

  • And in the top Ribbon click the Site Object Permissions.


  • Site Object Permissions wizard will pop up add the account (Group or Users) and assign the permissions.


Testing the solution:

  • The solution can be tested by creating an external list based on the external content type that we have created.

  • Go to SharePoint Site => Site Actions => More Options.

  • Select the external list from the installed templates and click on Create.


  • Enter the Name for the list and select the External Content Type that we have created.


  • The data's are populated as shown in the following.