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.

  • 相关阅读:
    【PQ】学会逆透视、透视,专治表格多行并一行,一行拆多行【分分合合几时休,学会了马上休】
    【Pandas】concat拼接,plan shapes are not aligned列标号不一致问题
    【MySQL】Pivot功能 一行拆多行等
    【PowerQuery】制作年底倒计时提醒
    数据分析师8大能力
    【爬虫基础】如何查看网页编码
    Mysql 插入中文错误:Incorrect string value: 'xE7xA8x8BxE5xBAx8F...' for column 'course' at row 1
    【MySQL】日期函数、时间函数总结
    mysql相关问题总结
    2020年 10月17 日 我遇见了一个很好的,善解人意的女孩
  • 原文地址:https://www.cnblogs.com/frankzye/p/3520597.html
Copyright © 2011-2022 走看看