前面的博文中我們討論瞭如何使用Business Connectivity Services對象模型欄獲取已部署在SharePoint BCS中的外部內容類型。本文中我們將學習如何獲取一個ECT的BCS方法集合。並且還要通過Business Connectivity Services對象模型執行其中的Finder方法和SpecificFinder方法。請先按照上一次文章中的步驟1到5創建一個簡單的...


在前面的博文中我們討論瞭如何使用Business Connectivity Services對象模型欄獲取已部署在SharePoint BCS中的外部內容類型。 

本文中我們將學習如何獲取一個ECT的BCS方法集合。並且還要通過Business Connectivity Services對象模型執行其中的Finder方法和SpecificFinder方法。

請先按照上一次文章中的步驟1到5創建一個簡單的Visual WebPart。並添加所需的引用和命名空間。

創建好後,按照下列步驟調用Business Connectivity Services對象模型來得到外部內容類型的方法。

 

1) 在你的可視化webpart的代碼視圖中添加下列using語句。該命名空間允許我們使用KeyValuePair類。

  using System.Collections.Generic;

2)修改Page_Load方法,調用一個方法來執行外部內容類型的某個方法。

protected void Page_Load(object sender, EventArgs e)

{

    EnumrateAndExecuteECTMethods();

}

3)接下來,我們來定義該方法。需要做兩件事:列出給定外部內容類型的所有方法;執行其中的finder方法和specific finder方法。

該方法的代碼如下:

private void EnumrateAndExecuteECTMethods()
{
    //獲取BDC服務引用
    BdcService service = SPFarm.Local.Servers.GetValue<BdcService>();
    //獲取元數據目錄
    IMetadataCatalog catalog = service.GetDatabaseBackedMetadataCatalog(SPServiceContext.Current);
    //通過相應的命名空間和名稱獲取實體
    IEntity entity = catalog.GetEntity("http://sp2010u", "產品");

    Literal1.Text = "<h1>" + entity.Name + " 的方法</h1> " + "<br/>";
    //為Finder和SpecificFinder方法的調用準備些變量
    int finderMethodRecordsCount = 0;
    string strName = "";

    //獲取方法集合
    foreach (KeyValuePair<string,IMethod> method in entity.GetMethods())        
    {
        //顯示方法名
        Literal1.Text += method.Key + ",";
        //顯示當前方法的實例
        IMethodInstance methodInstance = method.Value.GetMethodInstances()[method.Key];
        if (methodInstance.MethodInstanceType == MethodInstanceType.Finder)
        { 
            //調用Finder方法
            IEntityInstanceEnumerator ieie = entity.FindFiltered(method
.Value.GetFilters(methodInstance), entity.GetLobSystem().GetLobSystemInstances()[0].Value);
            //返回結果計數
            while (ieie.MoveNext())
            {
                finderMethodRecordsCount++;
            }
        }

        //調用SpecificFinder方法
        if (methodInstance.MethodInstanceType == MethodInstanceType.SpecificFinder)
        { 
            //標識符的值
            int i = 1;
            //創建一個標識符
            Identity identity = new Identity(i);
            //調用SpecificFinder方法,獲取該實體的實例
            IEntityInstance entInstance = entity.FindSpecific(identity, entity.GetLobSystem()
.GetLobSystemInstances()[0].Value);
            //顯示SpecificFinder所返回的實體實例的Name字段值
            strName = entInstance["Name"].ToString();
        }
    }
    Literal1.Text += "<br/>Finder 方法獲取的記錄數 = " + finderMethodRecordsCount.ToString();
    Literal1.Text += "<br/>Specific Finder方法返回的實例的Name為 " + strName;
}接下來,我們對其中重點的行進行單獨解釋,以便了解更多細節。
4)通過Business Connectivity Services對象模型,我們首先需要獲得BdcService以及元數據目錄 ,然後才是外部內容類型。
在本例中我們使用產品ECT,其命名空間為http://sp2010u。
    //獲取BDC服務引用
    BdcService service = SPFarm.Local.Servers.GetValue<BdcService>();
    //獲取元數據目錄
    IMetadataCatalog catalog = service.GetDatabaseBackedMetadataCatalog(SPServiceContext.Current);
    //通過相應的命名空間和名稱獲取實體 
    IEntity entity = catalog.GetEntity("http://sp2010u", "產品");
5)有了產品外部內容類型後,就可以遍歷該ECT所有可用的方法了。
IEntity的GetMethods方法返回一個KeyValuePare<string,IMethod>集合,其中Key為方法的名稱,IMethod為方法本身。
    //獲取方法集合
    foreach (KeyValuePair<string,IMethod> method in entity.GetMethods())        
    {
        //顯示方法名
        Literal1.Text += method.Key + ",";
6)在得到可用的方法後,我們需要檢查MethodInstanceType的值,判斷方法的類型:
        //顯示當前方法的實例
        IMethodInstance methodInstance = method.Value.GetMethodInstances()[method.Key];
        if (methodInstance.MethodInstanceType == MethodInstanceType.Finder)
        {

7)如果是Finder方法的話我們要執行它,然後簡單的遍歷一下返回的記錄並得到記錄的數量。

可以調用IEntity的FindFiltered方法來執行finder方法。

FindFiltered方法的第一個參數是篩選器的集合,可以通過調用IMethod的GetFilters方法獲得,然後作為參數傳給該方法實例。

FindFiltered方法的第二個參數是相應的LOB(Line Of Business,企業核心業務系統)系統的實例,可以通過IEntity的GetLobSystem方法,然後再調用 GetLobSystemInstance就可以獲得。所有這些調用完成後,我們就可以循環遍歷所返回的enumerator,並使記錄計數器自增。

//調用Finder方法
 IEntityInstanceEnumerator ieie = entity.FindFiltered(method
	.Value.GetFilters(methodInstance), entity.GetLobSystem().GetLobSystemInstances()[0].Value);
 //返回結果計數
 while (ieie.MoveNext())
 {
     finderMethodRecordsCount++;
 }

 8)對於得到的SpecificFinder方法,調用它的方法有一點不同。因為SpecificFinder方法總是要求傳遞至少一個參數(該參數映射到標識符)。

我們需要創建一個Identity類的實例,並作為參數傳給IEntity的FindSpecific方法。FindSpecifice方法的第一個參數是標識符,第二個參數是LOB系統的實例。

當FindSpecific方法執行完成後,會返回一個IEntityInstance實例。

在本例中我們硬編碼了一個標識符的值(int i=1),然後只是簡單的從EntityInstance中返回Name字段的值。

//標識符的值
 int i = 1;
 //創建一個標識符
 Identity identity = new Identity(i);
 //調用SpecificFinder方法,獲取該實體的實例
 IEntityInstance entInstance = entity.FindSpecific(identity, entity.GetLobSystem()
   	.GetLobSystemInstances()[0].Value);
 //顯示SpecificFinder所返回的實體實例的Name字段值
 strName = entInstance["Name"].ToString();

9)代碼編寫好後,按CTRL+F5部署到你的SharePoint站點。

10)在你的SharePoint站點中編輯頁面,並添加我們剛剛部署的Visual WebPart 。

該WebPart位於Custom分類下。

它會顯示產品外部內容類型的方法名,調用Finder方法後返回的記錄個數,以及調用SpecificFinder方法返回的產品的名稱。 

若以如何講system中的類應用到common模塊中_對象模型