Using SharePoint 2010 External Lists Programmatically

External list is a cool feature that has way simplified interface on accessing external SQL and Web Service data through familiar SharePoint list UI.

My colleague Larry wrote an excellent post on creating SharePoint 2010 external list programatically using c# as your feature receiver or a command line application.

One of the other ways to create any list including external list is to use list definitions. Here is how:

1. Let`s create a blank SharePoint 2010 project in VS 2010

2. Create a new Feature (web scoped) with any name you like

3. Create new list instance using a Add New Item Wizard

4. Make sure you instantiate from external list

5. Once you have the list instance created, find the Elements.xml

6. Within <ListInstance></ListInstance> definition place the following definition:

<DataSource>
<Property Name=”LobSystemInstance” Value=”Your instance” />
<Property Name=”EntityNamespace” Value=”NamespaceName.EntityName” />
<Property Name=”Entity” Value=”EntityName” />
<Property Name=”SpecificFinder” Value=”ViewMethodName Item” />
</DataSource>

<DataSource>

<Property Name=”LobSystemInstance” Value=”Your instance” />

<Property Name=”EntityNamespace” Value=”NamespaceName.EntityName” />

<Property Name=”Entity” Value=”EntityName” />

<Property Name=”SpecificFinder” Value=”ViewMethodNameItem” />

</DataSource>

This will define the system you will be using and methods to pull data. Of course you will have to have those set up.

Going back to Larry`s method, here is how programmatic definition would look like:

SPListDataSource ds = new SPListDataSource();
ds.BDCProperties.LobSystemInstance, “Your instance”);
ds.BDCProperties.EntityNamespace, “NamespaceName.EntityName”);
ds.BDCProperties.Entity, “EntityName”);ds.BDCProperties.SpecificFinder, “ViewMethodNameItem”);
using (SPSite site = new SPSite(“http://localhost”))
{
using (SPWeb web = site.RootWeb)
{
web.Lists.Add(“MyExternalList”, “”, “Lists/MyExternalList”, ds);
}
}

So whichever way you choose to define you list it will be quite simple.

Good luck!

About the author: Yaroslav Pentsarskyy