zoukankan      html  css  js  c++  java
  • Creating External Lists From Code

    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.

  • 相关阅读:
    HTTP响应状态码记录
    Linux给指定用户或全部用户(已登录)发送消息
    JS面向对象的程序设计
    Linux下查看/管理当前登录用户及用户操作历史记录
    JS Math对象中一些小技巧
    Linux常用命令学习
    Python学习问题记录
    Python中dir()与help()的使用
    webdriver常用API
    数据库备份表
  • 原文地址:https://www.cnblogs.com/frankzye/p/3520597.html
Copyright © 2011-2022 走看看