Поделиться через


Фрагмент кода: выполнение экземпляра метода Finder внешнего типа контента

Дата последнего изменения: 6 мая 2010 г.

Применимо к: SharePoint Server 2010

В этой статье
Описание
Необходимые компоненты
Использование этого примера

Описание

В следующем примере кода показано программное выполнение экземпляра метода Finder внешнего типа контента с помощью объектной модели среды выполнения BDC на сервере.

Необходимые компоненты

  • Microsoft SharePoint Server 2010 или Microsoft SharePoint Foundation 2010 на сервере.

  • Microsoft Visual Studio на клиентском компьютере.

  • Хотя бы один внешний тип контента, зарегистрированный в хранилище метаданных BDC.

Использование этого примера

  1. Запустите Visual Studio и создайте новый проект консольного приложения C#. При создании проекта выберите .NET Framework 3.5.

  2. В меню Вид выберите Страницы свойств, чтобы вывести свойства проекта.

  3. На вкладке Построение в качестве Целевой платформы выберите Любой ЦП.

  4. В обозревателе решений в разделе Ссылки удалите все ссылки проекта, кроме System и System.Core.

  5. Добавьте в проект следующие ссылки:

    1. Microsoft.BusinessData

    2. Microsoft.SharePoint

    3. System.Web

  6. Замените код в файле Program.cs на код, приведенный в конце этой процедуры.

  7. Замените значения <siteUrl>, <nameSpace> и <entityName> на допустимые значения.

  8. Сохраните проект.

  9. Скомпилируйте и запустите проект.

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

namespace SDKSamples
{
    class Methods
    {

        static void Main(string[] args)
        {
            BDCGetAllFieldsAndRecords();
        }

        // Get the fields and read data from an external content type.
        public static void BDCGetAllFieldsAndRecords()
        {
            // Specify the SiteURL, Namespace, and the Entity Name.
            string SiteURL = "<siteUrl>";
            string nameSpace = "<nameSpace>";
            string entityName = "<entityName>";

            using (SPSite site = new SPSite(SiteURL))
            {
                using (new Microsoft.SharePoint.SPServiceContextScope(
                    SPServiceContext.GetContext(site)))
                {
                    BdcService service =
                        SPFarm.Local.Services.GetValue<BdcService>(
                        String.Empty);
                    IMetadataCatalog catalog =
                        service.GetDatabaseBackedMetadataCatalog(
                        SPServiceContext.Current);

                    IEntity entity = catalog.GetEntity(
                        nameSpace, entityName);
                    ILobSystemInstance LobSysteminstance =
                        entity.GetLobSystem().
                        GetLobSystemInstances()[0].Value;

                    // Display the fields in the Entity.
                    IFieldCollection fieldCollection =
                        entity.GetFinderView("Read List").Fields;
                    foreach (IField field in fieldCollection)
                    {
                        Console.Write(field.Name.PadRight(20));
                    }

                    Console.WriteLine();

                    // Display all the records in the Entity.
                    IMethodInstance methodInstance = entity.GetMethodInstance(
                        "Read List", MethodInstanceType.Finder);
                    IEntityInstanceEnumerator ientityInstanceEnumerator =
                        entity.FindFiltered(
                        methodInstance.GetFilters(), LobSysteminstance);
                    while (ientityInstanceEnumerator.MoveNext())
                    {
                        foreach (IField field in fieldCollection)
                        {
                            Console.Write(
                                ientityInstanceEnumerator.
                                Current[field.Name].ToString().PadRight(20));
                        }
                        Console.WriteLine();
                    }

                }
                Console.ReadKey();
            }
        }
    }
}

См. также

Ссылка

BdcService

Services

IMetadataCatalog

GetDatabaseBackedMetadataCatalog(SPServiceContext)

GetEntity(String, String)

IEntity

GetLobSystem()

GetLobSystemInstances()

ILobSystemInstance

GetFinderView(String)

IFieldCollection

GetMethodInstance(String, MethodInstanceType)

IMethodInstance

GetFilters()

FindFiltered(IFilterCollection, ILobSystemInstance)

IEntityInstanceEnumerator