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:

  1. private void CreateExternalList(SPWeb web)
  2. {
  3. SPListCollection lists = web.Lists;
  4. SPListDataSource listDataSource = new SPListDataSource();
  5. // set up the list data source
  6.     listDataSource.SetProperty(SPListDataSource.BDCProperties.Entity, "YourBdcEntity");
  7.     listDataSource.SetProperty(SPListDataSource.BDCProperties.EntityNamespace, "YourBdc.EntityNamespace");
  8.     listDataSource.SetProperty(SPListDataSource.BDCProperties.LobSystemInstance, "YourLobSystemInstancece");
  9.     listDataSource.SetProperty(SPListDataSource.BDCProperties.SpecificFinder, "ReadItem");
  10. // create list
  11. Guid extListGuid = lists.Add("External list title", "External list description", "extlist", listDataSource);
  12. SPList extList = lists[extListGuid];
  13. // set other list properties
  14.     extList.OnQuickLaunch = true;
  15.     extList.Update();
  16. }

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:

  1. public static class Extensions
  2.     {
  3. public static void Initialize(this SPListDataSource listDataSource, String entity, String entityNamespace, String lobSystemInstance, String specificFinder)
  4.         {
  5.             listDataSource.SetProperty(SPListDataSource.BDCProperties.Entity, entity);
  6.             listDataSource.SetProperty(SPListDataSource.BDCProperties.EntityNamespace, entityNamespace);
  7.             listDataSource.SetProperty(SPListDataSource.BDCProperties.LobSystemInstance, lobSystemInstance);
  8.             listDataSource.SetProperty(SPListDataSource.BDCProperties.SpecificFinder, specificFinder);
  9.         }
  10.     }

Using the new extension method makes the original code a bit more readable:

  1. private void CreateExternalListEx(SPWeb web)
  2. {
  3. SPListCollection lists = web.Lists;
  4. SPListDataSource listDataSource = new SPListDataSource();
  5. // set up the list data source using the extension method
  6.     listDataSource.Initialize("YourBdcEntity", "YourBdc.EntityNamespace", "YourLobSystemInstancece", "ReadItem");
  7. // create list
  8. Guid extListGuid = lists.Add("External list title2", "External list description", "extlist2", listDataSource);
  9. // set other list properties
  10. SPList extList = lists[extListGuid];
  11.     extList.OnQuickLaunch = true;
  12.     extList.Update();
  13. }

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.

Creating External Lists From Code的更多相关文章

  1. leetcode -- Merge k Sorted Lists add code

    Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. [ ...

  2. Creating External Table - KUP-04020

    原因:因为操作系统环境不同,所以换行符也不同,要查看数据文件的换行符 解决方法: 1.如果是苹果系统类的数据文件,则改为:RECORDS DELIMITED BY 0X'0D' 2.如果是window ...

  3. Searching External Data in SharePoint 2010 Using Business Connectivity Services

    from:http://blogs.msdn.com/b/ericwhite/archive/2010/04/28/searching-external-data-in-sharepoint-2010 ...

  4. Comparing Code Playgrounds Codepen, JSFiddle, JS Bin, Dabblet, CSS Deck, and Liveweave

    What is a code playground? Codepen, JSFiddle, JS Bin, Dabblet, CSS Deck, and Liveweave are HTML, CSS ...

  5. Sharepoint 问题集锦 - external list (外部列表)

    使用Sharepoint开发过程中遇到的问题总结. 错误1: Unable to display this Web Part. To troubleshoot the problem, open th ...

  6. Sharepoint 问题集锦 - 外部列表(external list) - 读取当前用户上下文或用户名作为筛选参数

    在创建外部列表过程中,往往需要添加筛选参数,而较多开发用户,会关心如何在外部列表中,只显示当前用户相关的行.本例子中,我们以任务数据表来做例子,看看如何实现这个需求. 1)数据表tbl_task: t ...

  7. 管理外部表(External Tables)

    Oracle数据库允许对外部表中的数据进行只读访问.外部表定义为不驻留在数据库中的表,并且可以是为其提供访问驱动程序的任何格式.通过为数据库提供描述外部表的元数据,数据库能够公开外部表中的数据,就好像 ...

  8. Entity Framework Code-First(5):Code First Conventions

    Code First Conventions: We have seen how EF Code-First creates DB tables from domain classes in the ...

  9. 转:Busy Developers' Guide to HSSF and XSSF Features

    Busy Developers' Guide to Features Want to use HSSF and XSSF read and write spreadsheets in a hurry? ...

随机推荐

  1. Android样式的开发:Style篇

    前面铺垫了那么多,终于要讲到本系列的终篇,整合所有资源,定义成统一的样式.哪些该定义成统一的样式呢?举几个例子吧: 每个页面标题栏的标题基本会有一样的字体大小.颜色.对齐方式.内间距.外间距等,这就可 ...

  2. php高级面试题知识点(转载)

    php高级面试题知识点大全 时间:2016-01-26 06:36:22来源:网络 导读:php高级面试题知识点大全,本套面试题内容包括php魔术方法.php单点登录.linux基本命令.前端开发技术 ...

  3. MDX 占比同比环比

    http://blog.csdn.net/hero_hegang/article/details/9072889

  4. 使用Kibana 分析Nginx 日志并在 Dashboard上展示

    一.Kibana之Visualize 功能 在首页上Visualize 标签页用来设计可视化图形.你可以保存之前在discovery中的搜索来进行画图,然后保存该visualize,或者加载合并到 d ...

  5. SQL实现分组查询取前几条记录

    我要实现的功能是统计订单日志表中每一个订单的前三条日志记录,表结构如下: 一个订单在定点杆日志表中有多条记录,要根据时间查询出每一个订单的前三条日志记录,sql如下: select b.OrderNu ...

  6. 【第三课】ANR和OOM——贪快和贪多的后果(下)

    Out of Mana,法力耗尽. 内存就像法力,耗尽了就什么都不能做了.有时候一个应用程序占用了太大的内存,超过了Android系统为你规定的限制,那么系统就会干掉你,以保证其他app有足够的内存. ...

  7. Scrum 3.2 多鱼点餐系统开发进度(页面优化&下单详细信息页面)

    Scrum 3.2 多鱼点餐系统开发进度(页面优化&下单详细信息页面)  1.团队名称:重案组 2.团队目标:长期经营,积累客户充分准备,伺机而行 3.团队口号:矢志不渝,追求完美 4.团队选 ...

  8. PHP mail

    PHP 允许您从脚本直接发送电子邮件. PHP mail() 函数 PHP mail() 函数用于从脚本中发送电子邮件. 语法 mail(to,subject,message,headers,para ...

  9. R语言-简单线性回归图-方法

    目标:利用R语言统计描绘50组实验对比结果 第一步:导入.csv文件 X <- read.table("D:abc11.csv",header = TRUE, sep = & ...

  10. Mysql –>EF edmx(model first)–> Sql server table

    一.mysql environment When we create an new database,first We need draw er diagram for somebody to sho ...