首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> .NET 網頁程式設計 >> 關於.NET

透過 BCS 物件模型使用篩選器:從外部內容類型查詢資料

     編輯:關於.NET

前言

上篇介紹了如何使用 SharePoint 伺服器上的 BCS 物件模型連接中繼資料存儲,獲取外部內容類型 (ECT) 及其方法並執行。本文示範如何執行帶有篩選器定義的 ECT Finder 方法。

步驟詳解

1. SharePoint Designer 2010 建立 ECT 與篩選器

  • 建立新外部內容類型,使用 AdventureWorks2000 資料庫 Employees 表。
  • 在 First Name 欄位上設置篩選器,勾選「如果為以下值,則忽略篩選器」。
    設定篩選器

2. Visual Studio 2010 建立視覺 Web 組件專案

  • 從 SharePoint 2010 專案範本選「可視 Web 部件」。

3. 設計使用者介面

  • 加入標籤 "First Name:"、TextBox (ID: FirstNameTextbox)、Button (ID: SearchButton)、GridView。
    完成介面

4. 添加組件引用

右鍵「引用」→「添加引用」,瀏覽至:
c:\program files\common files\microsoft shared\web server extensions\14\ISAPI\Microsoft.BusinessData.dll

5. 添加命名空間

using Microsoft.SharePoint;
using Microsoft.SharePoint.BusinessData.SharedService;
using Microsoft.BusinessData.MetadataModel;
using Microsoft.SharePoint.Administration;
using Microsoft.BusinessData.Runtime;
using Microsoft.SharePoint.BusinessData.Runtime;

6. 核心代碼:執行篩選方法

private void executeFilterMethod() {
    // 獲取中繼資料目錄
    BdcService service = SPFarm.Local.Services.GetValue<BdcService>();
    IMetadataCatalog catalog = service.GetDatabaseBackedMetadataCatalog(SPServiceContext.Current);
    // 獲取 Employee ECT
    IEntity entity = catalog.GetEntity("https://contoso14", "Employee");
    // 獲取預設 Finder 方法的篩選器集合
    IFilterCollection filters = entity.GetDefaultFinderFilters();
    // 設置篩選器值
    if (FirstNameTextBox.Text != string.Empty) {
        WildcardFilter filter = (WildcardFilter)filters[0];
        filter.Value = FirstNameTextBox.Text;
    }
    // 執行篩選查詢
    IEntityInstanceEnumerator enumerator = entity.FindFiltered(filters, 
        entity.GetLobSystem().GetLobSystemInstances()[0].Value);
    // 綁定結果到 GridView
    DataTable table = null;
    while (enumerator.MoveNext()) {
        if (table == null) table = enumerator.Current.EntityAsDataTable;
        else table.ImportRow(enumerator.Current.EntityAsDataTable.Rows[0]);
    }
    GridView1.DataSource = table;
    GridView1.DataBind();
}

7. 按鈕點擊事件

protected void SearchButton_Click(object sender, EventArgs e) {
    executeFilterMethod();
}

8. 部署與測試

  • 按 F5 打包部署 (WSP)。
  • 頁面編輯模式插入 Web 組件 (Custom 分組下)。
  • 點擊按鈕返回所有資料,或輸入 First Name (如 Linda) 篩選。
    篩選結果

後續展望

後續文章將進一步介紹 BCS 物件模型更多用法。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved