• 在C#代码中执行BCS外部内容类型方法


    前面的博文中我们讨论了如何使用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方法返回的产品的名称。 

    参考资料

    executing bcs external content type methods in c# 

  • 相关阅读:
    EditText setError 图片不显示问题
    android TextView getlinecount返回0
    PANIC: Could not open AVD config file:
    case expressions must be constant expressions
    无法创建抽象类或接口“Newtonsoft.Json.JsonWriter”的实例
    Listview嵌套ListView 及包含button onItemClic失效问题
    errror initializing phoneGap:class not found
    动态添加ImageView 设置setPadding不起作用问题
    Parameter 参数替换成value
    Listview addHeaderView添加view报错
  • 原文地址:https://www.cnblogs.com/Sunmoonfire/p/1771420.html
Copyright © 2020-2023  润新知