Code Snippet: Execute the Deleter 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 the Deleter 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 on the client computer.
Microsoft Visual Studio.
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
System.Web
Replace the auto-generated code in Program.cs with the code listed at the end of this procedure.
Replace the values of <siteUrl>, <nameSpace>, and <entityName> with valid values.
Save the project.
Compile and run the project.
using System;
using Microsoft.SharePoint;
using Microsoft.SharePoint.BusinessData.SharedService;
using Microsoft.BusinessData.MetadataModel;
using Microsoft.BusinessData.MetadataModel.Collections;
using Microsoft.BusinessData.Runtime;
using Microsoft.SharePoint.Administration;
namespace SDKSamples
{
class Methods
{
static void Main(string[] args)
{
BDCDelete();
}
//How To: Edit an item from an External Content Type
public static void BDCDelete()
{
//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;
// Accept the user input for identity value
Console.Write(
"\nEnter identity value for which you want to delete : ");
int identityColumnValue =
int.Parse(Console.ReadLine());
Identity identity =
new Identity(identityColumnValue);
try
{
IEntityInstance ientityinstance =
entity.FindSpecific(
identity, "Read Item", LobSysteminstance);
IFieldCollection fieldCollection =
entity.GetFinderView("Read List").Fields;
//Display the item
foreach (IField field in fieldCollection)
{
Console.WriteLine(
field.Name.PadRight(20) + ":" +
ientityinstance[field.Name].ToString());
}
Console.Write("\nDo you want to delete this record (Y/N)?");
string confirmDelete = Console.ReadLine();
if (string.Compare(confirmDelete, "Y", true) == 0)
{
ientityinstance.Delete();
Console.WriteLine("Record deleted");
}
}
catch (ObjectNotFoundException exception)
{
Console.WriteLine(
"Identity column with value {0} not found...",
identityColumnValue);
}
}
}
Console.WriteLine("Press any key to continue...");
Console.ReadKey();
}
}
}
See Also
Reference
GetDatabaseBackedMetadataCatalog(SPServiceContext)