Code Snippet: Execute the Associator and Disassociator Method Instances 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 Associator and Disassociator method instances of an external content type 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
Start Visual Studio and create a C# Console application project. Select .NET Framework 3.5 when you create the project.
From the View menu, click Property Pages to bring up the project properties.
In the Build tab, for the Platform target, select Any CPU.
Close the project properties window.
In Solution Explorer, under References, remove all project references except for System and System.Core.
Add the following references to the project:
Microsoft.BusinessData
Microsoft.SharePoint
Microsoft.SharePoint.BusinessData
Replace the autogenerated code in Program.cs with the code listed at the end of this procedure.
Replace the values of entity name, LobSystem name, and LobSystemInstance name with valid values.
Save the project.
Compile and run the project.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.BusinessData.Runtime;
using Microsoft.BusinessData.MetadataModel;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint.BusinessData.SharedService;
namespace SDKSamples
{
class Associate
{
// FKLessAssociateDisAssociate.
public static void AssociateSalesOrderWithSalesReasonSample()
{
BdcService service =
SPFarm.Local.Services.GetValue<BdcService>(String.Empty);
IMetadataCatalog catalog =
service.GetDatabaseBackedMetadataCatalog(
SPServiceContext.Current);
// Get entities.
IEntity salesReasonEntity = catalog.GetEntity(
"AdventureWorks", "SalesReason");
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 salesReasonInstance =
salesReasonEntity.FindSpecific(
new Identity(1),
"Read Item",
lobSystemInstance,
OperationMode.Offline);
IEntityInstance salesOrderInstance =
salesOrderEntity.FindSpecific(
new Identity(44444),
"Read Item",
lobSystemInstance,
OperationMode.Offline);
// Create a collection with the source instance.
EntityInstanceCollection sourceInstances =
new EntityInstanceCollection(1);
sourceInstances.Add(salesReasonInstance);
// Get the association.
IAssociation associator =
(IAssociation)salesOrderEntity.GetMethodInstance(
"Sales Order by Reason Associator",
MethodInstanceType.Associator);
// Associate.
salesOrderEntity.Associate(
sourceInstances,
salesOrderInstance,
associator,
lobSystemInstance);
// Get the disassociator.
IAssociation disassociator =
(IAssociation)salesOrderEntity.GetMethodInstance(
"Sales Order by Reason Disassociator",
MethodInstanceType.Disassociator);
// Disassociate.
salesOrderEntity.Disassociate(
sourceInstances,
salesOrderInstance,
disassociator,
lobSystemInstance);
}
}
}
See Also
Reference
GetDatabaseBackedMetadataCatalog(SPServiceContext)
FindSpecific(Identity, String, ILobSystemInstance, OperationMode)
GetMethodInstance(String, MethodInstanceType)
Associate(EntityInstanceCollection, IEntityInstance, IAssociation, ILobSystemInstance)
Disassociate(EntityInstanceCollection, IEntityInstance, IAssociation, ILobSystemInstance)