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.

browse.gif

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.

    datamodel.gif

    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.

    wizard.gif

  • 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.

    entity.gif

Delete the default BDC Model:

  • In the solution, right click on BDCModel1.

  • Click on Delete.

    bdcmodel.gif

Create a new BDC Model:

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

    newitem.gif

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

    bussinesdatamodel.gif

  • 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

    code.gif

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.

    identifier.gif
  • In the properties, change the Name to Customers.

    propertyvalue.gif

  • Right click on Identifier1 and click on Properties.

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

    typename.gif

  • Right click on ReadList method and click on Delete.

    readlist.gif
  • 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.

    bdcmethod.gif

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

    bdcdetails.gif

    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.

    methoddetail.gif

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

    bdcexplorer.gif
  • 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.

    primitives.gif

  • 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.

    descriptor.gif

  • 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.

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

    typedescriptorproperty.gif
In the BDC Explorer, ReadItem method looks like the following

readitemmethod.gif

ReadList (Finder Method):

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

    methoddetails.gif

  • 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.

    addservice.gif

  • Enter the Address and click on Go.

  • Enter the Namespace and click on Ok.

    servicerefrence.gif

Modify CustomersService.cs:

  • Add the following namespaces

    -using CustomersBdcModel.ListServiceReference;
    -using System.Net;
     
  • Replace CustomersService.cs with the following

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using
    System.Text;
    using CustomersBdcModel.ListServiceReference;
    using System.Net; 
    namespace CustomersBdcModel.BdcModel
    {
        public class CustomersService
        {
            public static IEnumerable<Customers> ReadList()
            {
                MPDataContext dc = new MPDataContext(new Uri("http://serverName:1111/hr/MP/_vti_bin/listdata.svc"));
                dc.Credentials = CredentialCache.DefaultNetworkCredentials;
                var customers =
                    from e in dc.Customers
                    select new Customers()
                    {
                        CustomerID = (int)e.CustomerID,
                        CustomerName = e.CustomerName, 
                    };
                return customers;
            }       
            public static Customers ReadItem(int customerID)
            {
                MPDataContext dc = new MPDataContext(new Uri("http://serverName:1111/hr/MP/_vti_bin/listdata.svc"));
                dc.Credentials = CredentialCache.DefaultNetworkCredentials;
                var customers =
                    from e in dc.Customers
                    where e.CustomerID == customerID
                    select new Customers()
                    {
                        CustomerID = (int)e.CustomerID,
                        CustomerName = e.CustomerName,
                    };
                return customers.FirstOrDefault();
            }
        }
    }

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.

    appinfo.gif

Configure Business Data Connectivity access rights:

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

    appmanagement.gif

  • Click on Business Data Connectivity Service.

    applicator.gif

  • In the top Ribbon click on Manage.

    serviceapplication.gif

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

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

    objectpermission.gif
  • 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.

    externallist.gif

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

    externalcontent.gif

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

    output.gif

Next Recommended Readings