共用方式為


Code Snippet: Execute a Finder Method Instance of an External Content Type

Applies to: SharePoint Server 2010

In this article
Description
Prerequisites
To use this example

Description

The following code example shows how to programmatically execute a Finder method instance of an external content type by using the BDC Runtime object model on the server.

Prerequisites

  • Microsoft SharePoint Server 2010 or Microsoft SharePoint Foundation 2010 on the server.

  • Microsoft Visual Studio on the client.

  • At least one external content type that is registered in the BDC Metadata Store.

To use this example

  1. Start Visual Studio, and then create a C# Console application project. Select .NET Framework 3.5 when you create the project.

  2. From the View menu, click Property Pages to bring up the project properties.

  3. In the Build tab, for the Platform target, select Any CPU.

  4. In Solution Explorer, under References, remove all project references but System and System.Core.

  5. Add the following references to the project:

    1. Microsoft.BusinessData

    2. Microsoft.SharePoint

    3. System.Web

  6. Replace the code in Program.cs with the code listed at the end of this procedure.

  7. Replace the values of <siteUrl>, <nameSpace>, and <entityName> with valid values.

  8. Save the project.

  9. Compile and run the project.

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();
            }
        }
    }
}

See Also

Reference

BdcService

Services

IMetadataCatalog

GetDatabaseBackedMetadataCatalog(SPServiceContext)

GetEntity(String, String)

IEntity

GetLobSystem()

GetLobSystemInstances()

ILobSystemInstance

GetFinderView(String)

IFieldCollection

GetMethodInstance(String, MethodInstanceType)

IMethodInstance

GetFilters()

FindFiltered(IFilterCollection, ILobSystemInstance)

IEntityInstanceEnumerator