Code Snippet: Execute the Creator 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 Creator 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 autogenerated 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.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)
{
BDCCreate();
}
//How To: Create an External Content Type
public static void BDCCreate()
{
//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;
IView createView = entity.GetCreatorView("Create");
IFieldValueDictionary fieldValueDictionary =
createView.GetDefaultValues();
// The following will work only for Sales.Customer table in AdventureWorks2008
//Enter number of Customers To Create
Console.Write("\nEnter number of Customers To Create : ");
int number = int.Parse(Console.ReadLine());
// Code below can be used to edit the values and create Records.
for (int i = 1; i <= number; i++)
{
// Example values for TerritoryID : 1,2,4
Console.Write("\nEnter the TerritoryID : ");
fieldValueDictionary["TerritoryID"] = int.Parse(Console.ReadLine());
// Example value CustomerType : S
Console.Write("\nEnter the CustomerType : ");
fieldValueDictionary["CustomerType"] = Console.ReadLine();
fieldValueDictionary["rowguid"] = Guid.NewGuid();
fieldValueDictionary["ModifiedDate"] = DateTime.Now;
Identity id = entity.Create(fieldValueDictionary, LobSysteminstance);
Console.WriteLine("Successfully Inserted Item Number: {0}", i);
}
}
Console.WriteLine("Press any key to continue...");
Console.ReadKey();
}
}
}
}