You can create an external list based on an entity (external content type) defined in SharePoint Business Connectivity Services programmatically. All you need to do is to use the SPListDataSource class that describes the entity to be bound to the external list instance.
When creating an external list, you should use the Add method overload of the SPListCollection that has the SPListDataSource type parameter. You can find a sample for that from Frederik Prijck here.
A similar method replacing the strings with the string constants defined in the SPListDataSource.BDCProperties class:
- private void CreateExternalList(SPWeb web)
- {
- SPListCollection lists = web.Lists;
- SPListDataSource listDataSource = new SPListDataSource();
- // set up the list data source
- listDataSource.SetProperty(SPListDataSource.BDCProperties.Entity, "YourBdcEntity");
- listDataSource.SetProperty(SPListDataSource.BDCProperties.EntityNamespace, "YourBdc.EntityNamespace");
- listDataSource.SetProperty(SPListDataSource.BDCProperties.LobSystemInstance, "YourLobSystemInstancece");
- listDataSource.SetProperty(SPListDataSource.BDCProperties.SpecificFinder, "ReadItem");
- // create list
- Guid extListGuid = lists.Add("External list title", "External list description", "extlist", listDataSource);
- SPList extList = lists[extListGuid];
- // set other list properties
- extList.OnQuickLaunch = true;
- extList.Update();
- }
As you can see the properties of the SPListDataSource class are not real .NET properties. Instead of calling the setter of the property, you can set the their values by calling the SetProperty method.
I felt initializing the SPListDataSource through the SetProperty method a bit cumbersome, so I’ve created an extension method for the SPListDataSource class:
- public static class Extensions
- {
- public static void Initialize(this SPListDataSource listDataSource, String entity, String entityNamespace, String lobSystemInstance, String specificFinder)
- {
- listDataSource.SetProperty(SPListDataSource.BDCProperties.Entity, entity);
- listDataSource.SetProperty(SPListDataSource.BDCProperties.EntityNamespace, entityNamespace);
- listDataSource.SetProperty(SPListDataSource.BDCProperties.LobSystemInstance, lobSystemInstance);
- listDataSource.SetProperty(SPListDataSource.BDCProperties.SpecificFinder, specificFinder);
- }
- }
Using the new extension method makes the original code a bit more readable:
- private void CreateExternalListEx(SPWeb web)
- {
- SPListCollection lists = web.Lists;
- SPListDataSource listDataSource = new SPListDataSource();
- // set up the list data source using the extension method
- listDataSource.Initialize("YourBdcEntity", "YourBdc.EntityNamespace", "YourLobSystemInstancece", "ReadItem");
- // create list
- Guid extListGuid = lists.Add("External list title2", "External list description", "extlist2", listDataSource);
- // set other list properties
- SPList extList = lists[extListGuid];
- extList.OnQuickLaunch = true;
- extList.Update();
- }
Remark: I found that creating the external list from code takes considerably more time than creating one from the SharePoint UI. In my test environment it was about 30 secs vs. 5 secs that is quite a big difference. Probably I should launch Reflector to see that quicker method of external list creation.