Share via


Code Snippet: Execute the BulkAssociationNavigator 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 BulkAssociationNavigator 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 computer.

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

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. Microsoft.SharePoint.BusinessData

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

  8. Replace the values of entity name, LobSystem name, and LobSystemInstance name with valid values.

  9. Save the project.

  10. Compile and run the project.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.BusinessData.MetadataModel;
using Microsoft.BusinessData.MetadataModel.Collections;
using Microsoft.BusinessData.Runtime;
using System.Diagnostics;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint.BusinessData.SharedService;

namespace SDKSamples
{
    class Methods
    {
        //Foreign Key Based BulkAssociationNavigator.
        public static void BulkAssociationNavigatorSample()
        {
                BdcService service = 
                    SPFarm.Local.Services.GetValue<BdcService>
                    (String.Empty);
                IMetadataCatalog catalog = 
                    service.GetDatabaseBackedMetadataCatalog(
                    SPServiceContext.Current);

                // Get entities.
                IEntity customerEntity = 
                    catalog.GetEntity("AdventureWorks", "Customer");
                IEntity salesOrderEntity = 
                    catalog.GetEntity("AdventureWorks", "SalesOrder");

                //Get LOB System instance.
                ILobSystemInstance lobSystemInstance = 
                    salesOrderEntity.GetLobSystem().
                    GetLobSystemInstances()["AdventureWorks"];

                // Get the source entity instance with ID 1 to use 
                // to navigate the association.
                IEntityInstance customer1 = 
                    customerEntity.FindSpecific(
                    new Identity(1), 
                    "Read Item", 
                    lobSystemInstance, 
                    OperationMode.Offline);
                IEntityInstance customer2 = 
                    customerEntity.FindSpecific(
                    new Identity(2), 
                    "Read Item", 
                    lobSystemInstance, 
                    OperationMode.Offline);

                // Get the association.
                IAssociation association = 
                    (IAssociation)salesOrderEntity.GetMethodInstance(
                    "Bulk Customers Sales Orders", 
                    MethodInstanceType.BulkAssociationNavigator);

                // Create a collection with the entity instance.
                EntityInstanceCollection sourceInstances1 = 
                    new EntityInstanceCollection(1);
                sourceInstances1.Add(customer1);
                EntityInstanceCollection sourceInstances2 = 
                    new EntityInstanceCollection(1);
                sourceInstances2.Add(customer2);

                IList<EntityInstanceCollection> entityInstanceCollectionList = 
                    new List<EntityInstanceCollection>(2);
                entityInstanceCollectionList.Add(sourceInstances1);
                entityInstanceCollectionList.Add(sourceInstances2);

                IEntityInstanceEnumerator associatedInstances = null;
                try
                {
                    // Navigate the association.
                    associatedInstances = 
                        salesOrderEntity.FindAssociatedMultiple(
                        entityInstanceCollectionList, 
                        association, 
                        lobSystemInstance, 
                        OperationMode.Offline);

                    // List all sales orders for customer 1.
                    Debug.WriteLine(
                        "Listing customer's 1 sales orders ID and dates:");
                    while (associatedInstances.MoveNext())
                    {
                        Debug.WriteLine(
                            String.Format(
                            "Id: {0}, OrderDate: {1}", 
                            associatedInstances.Current["SalesOrderID"], 
                            associatedInstances.Current["OrderDate"]));
                    }
                }
                finally
                {
                    // Ensure the enumerator is closed.
                    if (associatedInstances != null)
                    {
                        associatedInstances.Close();
                    }
                }
            }
        }       
    }
}

See Also

Reference

BdcService

Services

IMetadataCatalog

GetDatabaseBackedMetadataCatalog(SPServiceContext)

GetEntity(String, String)

IEntity

GetLobSystem()

GetLobSystemInstances()

ILobSystemInstance

IEntityInstance

FindSpecific(Identity, String, ILobSystemInstance, OperationMode)

GetMethodInstance(String, MethodInstanceType)

IAssociation

EntityInstanceCollection

IEntityInstanceEnumerator

FindAssociatedMultiple(IList<EntityInstanceCollection>, IAssociation, ILobSystemInstance, OperationMode)