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.

  • 相关阅读:
    iOS7 自己定义动画跳转
    Android开发之用双缓冲技术绘图
    postgres 使用存储过程批量插入数据
    渗透过程
    python pytesseract使用
    排序算法比较
    python算法
    python中PIL模块
    数字电路复习
    windows服务参考
  • 原文地址:https://www.cnblogs.com/frankzye/p/3520597.html
Copyright © 2011-2022 走看看