Share via


Create an External List in SharePoint 2013 using Visual Studio

Business Connectivity Services (BCS) provides a great support to bring external data to SharePoint through External Content Types. Also the External Content Types can be used to create external lists when we need to display data. These external lists can be created using SharePoint Designer with limited flexibility as well as using Visual Studio with much higher flexibility. This article will be focused on how to create an External List using Visual Studio for SharePoint 2013.

Adding the External Content Type

1. Create a 'SharePoint 2013 - Empty' project in Visual Studio and name it as 'BCSVSDemo'. Click the 'OK' button and you will be navigated to the 'SharePoint Customization Wizard'. Mention the site URL and select the option 'Deploy as a farm solution'. Click 'Finish' button in order to complete project creation.

http://2.bp.blogspot.com/-n8wQwBqNN7Q/U74v5yuR3UI/AAAAAAAAAWQ/r-B6ip9kAAk/s1600/1.JPG

  1. Right click on project in the solution explorer and Add --> New Item --> Business Data Connectivity Model (farm Solution only). Type the name as 'VSDemoModel'.

http://4.bp.blogspot.com/-LTP0uUitKes/U74xAc9XgUI/AAAAAAAAAWc/SM8-ZD1ufLE/s1600/2.JPG

Click 'Add' button to add the Model to solution.

The designer will show an entity as 'Entity1' and the Model adds 2 new classes 'Entity1.cs' and 'Entity1Service.cs'. Also open BDC Method Details window. If not available by default, open it from VIEW --> Other Windows --> BDC Method Details.

You can rename 'Entity1' to whatever the entity you need such as 'Contact', 'Application' and etc...

Defining the Operations

3. Replace the 'Entity1Service.cs' class with following code.

http://3.bp.blogspot.com/-tNS_X2pcKMY/U74zflR6K_I/AAAAAAAAAW0/sgFtRw51hCc/s1600/4.JPG

What I have done here is creating a static variable and populating it with two Entity1 type objects. ReadList method is associated with displaying the list items in the list view. ReadItem method is associated with displaying the item content in the list item display view.

Create List

4. Deploy the solution to SharePoint site by right click the solution and selecting 'Deploy Solution'.

Navigate to 'Site Content' and click 'add an app'. You will be navigated to Your Apps page. Select 'External List' template from the set of list templates. 'Adding External List' window will be open. Give the Name as 'BCSVS List' and pick 'VSDemoModel' from 'External Content Type Picker'.

http://3.bp.blogspot.com/-D8BanM1B72g/U7433eGVDbI/AAAAAAAAAXA/YRhwNC0J4Z0/s1600/5.JPG

5. Navigate to 'BCSVS List' and you will see 2 list items in it.

http://2.bp.blogspot.com/-af4BiX_9ULU/U744OmEHPtI/AAAAAAAAAXI/jBB4WBBxiH8/s1600/6.JPG

Important:

1. In case, you try to add new items to the list or edit an item, you will see the options are disabled in the ribbon. The reason is that we have only 2 methods defined in the content type. 'ReadList' and 'ReadItem'. In order to add edit and create new options you need to write implementations in the same Entity1Service.cs calss. Will cover that in the next article.

2. If you are connecting a SQL data source or a WCF Service, you can write the implementations in the 'ReadList' and 'ReadItem' methods. Best suggested option is adding a 'LINQ to SQL' item to the project.