共用方式為


Code Snippet: Execute the IdEnumerator 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 an IdEnumerator 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 .NET Framework 3.5 and Microsoft Visual Studio on the client.

To use this example

  1. Start Visual Studio and 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. Close the project properties window.

  5. In Solution Explorer, under References, remove all project references except for System and System.Core.

  6. Add the following references to the project:

    1. Microsoft.BusinessData

    2. Microsoft.SharePoint

    3. System.Web

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

  8. Replace the SiteURL value with a valid site URL.

  9. This sample is based on the AdventureWorks sample database and the Customer external content type. If your external system is different, change the name of the external content type and LobSystem as appropriate in the code.

  10. Save the project.

  11. Compile and run the project.

using System;
using Microsoft.SharePoint.BusinessData.SharedService;
using Microsoft.BusinessData.MetadataModel;
using Microsoft.BusinessData.MetadataModel.Collections;
using Microsoft.BusinessData.Runtime;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint;
 
namespace SDKSamples
{
    class Methods
    {
 
        static void Main(string[] args)
        {
            EnumerateCustomerIds();
        }
 
        // IdEnumerator.
        public static void EnumerateCustomerIds()
        {
            string SiteURL = "<SiteUrl>";
           
            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);
 
                    // Get customer Entity for Sample AdventureWorks Model which supplies the IdEnumerator Method.
                    IEntity customerEntity = catalog.GetEntity("AdventureWorks", "Customer");
 
                    // Get the default filter values.
                    IMethodInstance idEnumeratorMethodInstance =
                        customerEntity.GetMethodInstance(
                        "Id Enumerator", MethodInstanceType.IdEnumerator);
                    IFilterCollection defaultFilters =
                        idEnumeratorMethodInstance.GetFilters();
 
                    // Set the limit filter value to 10.
                    // This method instance has only one filter, which is the limit filter.
                    IUserInputFilter limitFilter = (IUserInputFilter)defaultFilters[0];
                    limitFilter[0] = 10;
 
                    // Get LOB System instance.
                    ILobSystemInstance lobSystemInstance =
                        customerEntity.GetLobSystem().
                        GetLobSystemInstances()["AdventureWorks"];
 
                    IEntityInstanceEnumerator customerIdentities = null;
 
                    try
                    {
                        // List customer identities with default filter values.
                        customerIdentities =
                            customerEntity.EnumerateIdentities(
                            defaultFilters, lobSystemInstance);
 
                        // List customer IDs found.
                        while (customerIdentities.MoveNext())
                        {
                            Console.WriteLine(
                                String.Format(
                                "Customer Id: {0}",
                                customerIdentities.Current["CustomerID"]));
                        }
                    }
                    finally
                    {
                        // Ensure the enumerator is closed.
                        if (customerIdentities != null)
                        {
                            customerIdentities.Close();
                        }
                    }
                }
            }
        }
    }
}

See Also

Reference

BdcService

Services

IMetadataCatalog

GetDatabaseBackedMetadataCatalog(SPServiceContext)

GetEntity(String, String)

IEntity

GetMethodInstance(String, MethodInstanceType)

IMethodInstance

GetFilters()

IUserInputFilter

GetLobSystem()

GetLobSystemInstances()

ILobSystemInstance

IEntityInstanceEnumerator

EnumerateIdentities(IFilterCollection, ILobSystemInstance)