in SharePoint 2010

Creating External Lists Programmatically

Creating External Lists using SharePoint Designer 2010 or SharePoint 2010 UI is just a breeze. But what if you want to create them programmatically?

There are two different ways you could do so:

Using Object Model

Using the SPWeb.Lists.Add method we can create an external list:

SPListDataSource ds = new SPListDataSource();
ds.SetProperty(SPListDataSource.BDCProperties.LobSystemInstance, "Demo Customers");
ds.SetProperty(SPListDataSource.BDCProperties.EntityNamespace, "http://intranet");
ds.SetProperty(SPListDataSource.BDCProperties.Entity, "Demo Customers");
ds.SetProperty(SPListDataSource.BDCProperties.SpecificFinder, "CustomerRead Item");

using (SPSite site = new SPSite("http://intranet"))
{
    using (SPWeb web = site.RootWeb)
    {
        web.Lists.Add("Demo Customers", "Demo Customers", "Lists/DemoCustomers", ds);
    }
}

Using the SPListDataSource.BDCProperties, we can set the properties of the external system.

You can now wrap this code in a feature receiver.

Using List Instance

Creating external lists using the List Instance wizard in Visual Studio 2010 is not available, but you can still create an external instance using list instance.

Using the list instance wizard, add a list instance to your project. Select any list to instantiate from.

Now, edit the list instance’s Elements.xml and change it to reflect the creation of an external list:

<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <ListInstance Title="CustomersListInstance"
                OnQuickLaunch="TRUE"
                TemplateType="600"
                FeatureId="00bfea71-de22-43b2-a848-c05709900100"
                Url="Lists/CustomersListInstance"
                Description="External Customers List">
    <DataSource>
      <Property Name="LobSystemInstance" Value="Demo Customers" />
      <Property Name="EntityNamespace" Value="http://intranet" />
      <Property Name="Entity" Value="Demo Customers" />
      <Property Name="SpecificFinder" Value="CustomerRead Item" />
    </DataSource>
  </ListInstance>
</Elements>

Things to note:

1) TemplateType – The external list template type Id is 600

2) DataSource – Same as how we used the SPListDataSource.BDCProperties when creating an external list using object model

Write a Comment

Comment

Time limit is exhausted. Please reload the CAPTCHA.

  1. Very useful article. If like me you are having problems knowing what to put in the appropriate fields; mine were:

    LobSystemInstance = The name of your connection for the External data type
    Entity Namespace = The url to the appropriate site
    Entity = The name of your BDC Model item
    Specific Finder = The actual name you gave the Retrieve Item within the external data type