{"id":60,"date":"2009-12-01T14:32:42","date_gmt":"2009-12-01T14:32:42","guid":{"rendered":"http:\/\/localhost\/todd_wp\/how-to-programmatically-create-a-sharepoint-2010-external-content-type\/"},"modified":"2011-07-19T17:00:44","modified_gmt":"2011-07-19T17:00:44","slug":"how-to-programmatically-create-a-sharepoint-2010-external-content-type","status":"publish","type":"post","link":"https:\/\/toddbaginski.com\/blog\/how-to-programmatically-create-a-sharepoint-2010-external-content-type\/","title":{"rendered":"HOW TO: Programmatically create a SharePoint 2010 External Content Type"},"content":{"rendered":"<p><a class=\"retweet self\"><\/a><\/p>\n<p>I demonstrated how to make an external content type programmatically during the <strong>SPC 405 &#8211; Business Connectivity Services Runtime and Object Model Deep Dive<\/strong> session at the <a href=\"http:\/\/www.mssharepointconference.com\/Pages\/default.aspx\" target=\"_blank\">2009 Microsoft SharePoint Conference<\/a>.  Since the conference I have received a couple of dozen emails about the this topic.  Most of the emails ask for the code samples and an explanation of the code, and at least half of the emails inquire about <em>when and why<\/em> you may need to create an external content type programmatically.  Because this is obviously a hot topic I decided to write it up in a blog post.<\/p>\n<h1>Sample Business Context<\/h1>\n<p>To begin, let\u2019s take a moment to understand what types of business situations lend well to programmatically creating external content types.  The diagram below illustrates a common business scenario where creating external content types programmatically makes a lot of sense. <\/p>\n<p>For example, an engineer in the engineering department creates a specification for a new product.  The purchasing department then contacts multiple suppliers to determine which suppliers the company will work with to secure the parts and components used to create the new product.  The new product and the suppliers are entered into the company\u2019s ERP system.  Customer service representatives who support the product need quick access to the supplier data in the ERP system.  The customer service representatives have not been trained to use the ERP system, however they are familiar with web based applications.  To provide the customer service representatives with real time access to the product and supplier information in the ERP system an external content type may be automatically created when a new product and its associated supplier information is entered into the ERP system.  The external content type will correspond to the new product and supplier information and provide the ability for the customer service representatives to access the data within a SharePoint web site.<\/p>\n<p> <a rel=\"lightbox\" href=\"https:\/\/toddbaginski.com\/blog\/images\/www_toddbaginski_com\/blog\/WindowsLiveWriter\/HOWTOProgrammaticallycreateanExternalCon_CF4C\/image_2.png\"><img loading=\"lazy\" decoding=\"async\" style=\"display: inline; border-width: 0px;\" title=\"image\" src=\"https:\/\/toddbaginski.com\/blog\/images\/www_toddbaginski_com\/blog\/WindowsLiveWriter\/HOWTOProgrammaticallycreateanExternalCon_CF4C\/image_thumb.png\" border=\"0\" alt=\"image\" width=\"562\" height=\"852\" \/><\/a><\/p>\n<h1>The Solution<\/h1>\n<p>To build on the example above the first thing that needs to be done is to hook the creation of a new product and associated suppliers in the ERP system to the creation of an external content type.  There are several ways to do this and the appropriate method will depend on the system where the data is stored.  Workflows, batch jobs, triggers, and event callouts may all be viable options.  No matter which option is the best solution, eventually you will use the the Business Data Connectivity  (BDC)administration API to automate the creation of an external content type in SharePoint 2010.<\/p>\n<p>The BDC administration API allows you to make changes to the metadata in the BDC metadata store.  This API is primarily found in the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/microsoft.sharepoint.businessdata.administration(office.14).aspx\" target=\"_blank\">Microsoft.SharePoint.BusinessData.Administration<\/a>and the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/microsoft.sharepoint.businessdata.administration.client(office.14).aspx\" target=\"_blank\">Microsoft.SharePoint.BusinessData.Administration.Client<\/a> namespaces.  The <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/microsoft.sharepoint.businessdata.administration(office.14).aspx\" target=\"_blank\">Microsoft.SharePoint.BusinessData.Administration<\/a>namespace is used when running on the SharePoint server and the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/microsoft.sharepoint.businessdata.administration.client(office.14).aspx\" target=\"_blank\">Microsoft.SharePoint.BusinessData.Administration.Client<\/a>namespace is used when running on a client machine (machine other than a server in a SharePoint server farm).  The <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/microsoft.businessdata.metadatamodel(office.14).aspx\" target=\"_blank\">Microsoft.BusinessData.MetadataModel<\/a> and <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/microsoft.businessdata.runtime(office.14).aspx\" target=\"_blank\">Microsoft.BusinessData.Runtime<\/a>namespaces are also used to support creating external content types.<\/p>\n<p>As I mentioned earlier, the exact implementation you choose will depend on your environment.  Instead of providing a click by click tutorial to create a console application, Web\/WCF service, Web Part, or the other usual suspects, I\u2019ll just focus on the code you need to get the job done and leave the implementation details up to you.  This example creates an external content type for the Customers table in the AdventureWorks sample database.  This is the same example I demoed at the 2009 Microsoft SharePoint Conference, however I updated it to run on Beta 2.  You can download the complete source code, updated for SharePoint 2010 Beta 2, <a href=\"https:\/\/toddbaginski.com\/blog\/files\/Article%20Files\/ECTGenerator.zip\" target=\"_blank\">here<\/a>.<\/p>\n<h2>Assembly references<\/h2>\n<p>To begin, create the appropriate Visual Studio project for your implementation and add references to the following assemblies.<\/p>\n<table border=\"1\" cellspacing=\"0\" cellpadding=\"2\" width=\"600\">\n<tbody>\n<tr>\n<td valign=\"top\"><strong>Assembly<\/strong><\/td>\n<td valign=\"top\"><strong>Location<\/strong><\/td>\n<td valign=\"top\"><strong>Usage<\/strong><\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">Microsoft.BusinessData.dll<\/td>\n<td valign=\"top\">C:Program FilesCommon FilesMicrosoft SharedWeb Server Extensions14ISAPI<\/td>\n<td valign=\"top\">Needed on both client and server<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">Microsoft.SharePoint.dll<\/td>\n<td valign=\"top\">C:Program FilesCommon FilesMicrosoft SharedWeb Server Extensions14ISAPI<\/td>\n<td valign=\"top\">Solution runs on a SharePoint server<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">Microsoft.SharePoint.BusinessData.Administration.Client.dll<\/td>\n<td valign=\"top\">C:Program FilesMicrosoft OfficeOffice14<\/td>\n<td valign=\"top\">Solution runs on a machine other than a SharePoint server (client)<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>This example assumes you <em>are not<\/em> running on the SharePoint server and uses the Microsoft.SharePoint.BusinessData.Administration.Client.dll assembly.  Therefore, the Microsoft.SharePoint.BusinessData.Administration.dll assembly is not added as a reference.<\/p>\n<p><a rel=\"lightbox\" href=\"https:\/\/toddbaginski.com\/blog\/images\/www_toddbaginski_com\/blog\/WindowsLiveWriter\/HOWTOProgrammaticallycreateanExternalCon_CF4C\/References_2.png\"><img loading=\"lazy\" decoding=\"async\" style=\"display: inline; border-width: 0px;\" title=\"References\" src=\"https:\/\/toddbaginski.com\/blog\/images\/www_toddbaginski_com\/blog\/WindowsLiveWriter\/HOWTOProgrammaticallycreateanExternalCon_CF4C\/References_thumb.png\" border=\"0\" alt=\"References\" width=\"323\" height=\"51\" \/><\/a><\/p>\n<h2>The Code<\/h2>\n<p>The BDC administration API is easy to use.  The classes, properties, and methods you use to create an external content type map directly to the metadata structures which define a BDC Model.  If you are already familiar with BDC Models (Application Definitions in MOSS 2007 speak) this code will look familiar to you as well.<\/p>\n<h3>Using Statements<\/h3>\n<p>To begin, add the following using statements to your project.<\/p>\n<div id=\"codeSnippetWrapper\">\n<pre id=\"codeSnippet\" style=\"text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: &quot;Courier New&quot;, courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;\"><span style=\"color: #0000ff;\">using<\/span> Microsoft.SharePoint.BusinessData.Administration.Client;\r\n<span style=\"color: #0000ff;\">using<\/span> Microsoft.BusinessData.MetadataModel;\r\n<span style=\"color: #0000ff;\">using<\/span> Microsoft.BusinessData.Runtime;<\/pre>\n<h3>Connect to the BDC Metadata Store<\/h3>\n<p>Next, connect to the BDC Metadata Store where the definitions for external content types are stored.  In this case I am connecting to the BDC Metadata Store associated with the site located at the following URL: <strong>http:\/\/dev1\/sites\/Team Site<\/strong><\/p>\n<div id=\"codeSnippetWrapper\">\n<pre id=\"codeSnippet\" style=\"text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: &quot;Courier New&quot;, courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;\">AdministrationMetadataCatalog catalog =\r\nAdministrationMetadataCatalog.GetCatalog(<span style=\"color: #006080;\">\"http:\/\/dev1\/sites\/Team Site\"<\/span>);<\/pre>\n<h3>Create the Model, LobSystem, and LOBSystemInstance<\/h3>\n<p>Next, create the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/microsoft.sharepoint.businessdata.administration.client.model(office.14).aspx\" target=\"_blank\">Model<\/a>, <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/microsoft.sharepoint.businessdata.administration.client.lobsystem(office.14).aspx\" target=\"_blank\">LobSystem<\/a>, and <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/microsoft.sharepoint.businessdata.administration.client.lobsysteminstance(office.14).aspx\" target=\"_blank\">LobSystemInstance<\/a>.  Pay special attention to the names you provide for these items because they appear in the External Content Type picker and your users will see them.  See the screenshot near the end of this article for more details.<\/p>\n<div id=\"codeSnippetWrapper\">\n<pre id=\"codeSnippet\" style=\"text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: &quot;Courier New&quot;, courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;\"><span style=\"color: #008000;\">\/\/Create a new customer model\r\n<\/span>Model customerModel = Model.Create(<span style=\"color: #006080;\">\"CustomerModel\"<\/span>, <span style=\"color: #0000ff;\">true<\/span>, catalog);\r\n\r\n<span style=\"color: #008000;\">\/\/Make a new Customer LobSystem\r\n<\/span>LobSystem awLobSystem =\r\ncustomerModel.OwnedReferencedLobSystems.Create(<span style=\"color: #006080;\">\"Customer\"<\/span>, <span style=\"color: #0000ff;\">true<\/span>, SystemType.Database);\r\n\r\n<span style=\"color: #008000;\">\/\/Make a new AdventureWorks LobSystemInstance\r\n<\/span>LobSystemInstance awLobSystemInstance =\r\nawLobSystem.LobSystemInstances.Create(<span style=\"color: #006080;\">\"AdventureWorks\"<\/span>, <span style=\"color: #0000ff;\">true<\/span>);\r\n\r\n<span style=\"color: #008000;\">\/\/Set the connection properties\r\n<\/span>awLobSystemInstance.Properties.Add(<span style=\"color: #006080;\">\"AuthenticationMode\"<\/span>, <span style=\"color: #006080;\">\"PassThrough\"<\/span>);\r\nawLobSystemInstance.Properties.Add(<span style=\"color: #006080;\">\"DatabaseAccessProvider\"<\/span>, <span style=\"color: #006080;\">\"SqlServer\"<\/span>);\r\nawLobSystemInstance.Properties.Add(<span style=\"color: #006080;\">\"RdbConnection Data Source\"<\/span>, <span style=\"color: #006080;\">\"DEV1\"<\/span>);\r\nawLobSystemInstance.Properties.Add(<span style=\"color: #006080;\">\"RdbConnection Initial Catalog\"<\/span>, <span style=\"color: #006080;\">\"Customers\"<\/span>);\r\nawLobSystemInstance.Properties.Add(<span style=\"color: #006080;\">\"RdbConnection Integrated Security\"<\/span>, <span style=\"color: #006080;\">\"SSPI\"<\/span>);\r\nawLobSystemInstance.Properties.Add(<span style=\"color: #006080;\">\"RdbConnection Pooling\"<\/span>, <span style=\"color: #006080;\">\"true\"<\/span>);<\/pre>\n<h3>Create the Entity<\/h3>\n<p>Next, create the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/microsoft.sharepoint.businessdata.administration.client.entity(office.14).aspx\" target=\"_blank\">Entity<\/a>to represent the Customers table and define which column(s) make up the identifier for the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/microsoft.sharepoint.businessdata.administration.client.entity(office.14).aspx\" target=\"_blank\">Entity<\/a>.<\/p>\n<div id=\"codeSnippetWrapper\">\n<pre id=\"codeSnippet\" style=\"text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: &quot;Courier New&quot;, courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;\"><span style=\"color: #008000;\">\/\/Create a new Customer Entity\r\n<\/span>Entity customerEntity =\r\n   Entity.Create(<span style=\"color: #006080;\">\"Customer\"<\/span>, <span style=\"color: #006080;\">\"AdventureWorks\"<\/span>, <span style=\"color: #0000ff;\">true<\/span>,\r\n<span style=\"color: #0000ff;\">   new<\/span> Version(<span style=\"color: #006080;\">\"1.0.0.0\"<\/span>), 10000, CacheUsage.Default,\r\n   awLobSystem, customerModel, catalog);\r\n<span style=\"color: #008000;\">\/\/Set the identifier - CustomerID column\r\n<\/span>customerEntity.Identifiers.Create(<span style=\"color: #006080;\">\"CustomerId\"<\/span>, <span style=\"color: #0000ff;\">true<\/span>, <span style=\"color: #006080;\">\"System.Int32\"<\/span>);<\/pre>\n<\/div>\n<h3>Define the Specific Finder Method, Parameters and Type Descriptors<\/h3>\n<p>Next, create the specific finder <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/microsoft.sharepoint.businessdata.administration.client.method(office.14).aspx\" target=\"_blank\">Method<\/a>, specify the query it will use, and define the input and output parameters associated with it.  The specific finder <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/microsoft.sharepoint.businessdata.administration.client.method(office.14).aspx\" target=\"_blank\">Method<\/a> returns exactly one row of data from the data source, given an identifier.<\/p>\n<div id=\"codeSnippetWrapper\">\n<pre id=\"codeSnippet\" style=\"text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: &quot;Courier New&quot;, courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;\"><span style=\"color: #008000;\">\/\/Create the specific finder method\r\n<\/span>Method getCustomerMethod = customerEntity.Methods.Create(<span style=\"color: #006080;\">\"GetCustomer\"<\/span>, <span style=\"color: #0000ff;\">true<\/span>, <span style=\"color: #0000ff;\">false<\/span>, <span style=\"color: #006080;\">\"GetCustomer\"<\/span>);\r\n<span style=\"color: #008000;\">\/\/Specify the query\r\n<\/span>getCustomerMethod.Properties.Add(<span style=\"color: #006080;\">\"RdbCommandText\"<\/span>, <span style=\"color: #006080;\">\"SELECT [CustomerId] ,\r\n   [FirstName] , [LastName] , [Phone] , [EmailAddress] , [CompanyName]\r\n   FROM[Customers].[SalesLT].[Customer]\r\n   WHERE [CustomerId] = @CustomerId\"<\/span>);\r\n<span style=\"color: #008000;\">\/\/Set the command type\r\n<\/span>getCustomerMethod.Properties.Add(<span style=\"color: #006080;\">\"RdbCommandType\"<\/span>, <span style=\"color: #006080;\">\"Text\"<\/span>);\r\n<span style=\"color: #008000;\">\/\/Create the CustomerID input parameter\r\n<\/span>Parameter customerIDParameter =\r\n   getCustomerMethod.Parameters.Create(<span style=\"color: #006080;\">\"@CustomerId\"<\/span>, <span style=\"color: #0000ff;\">true<\/span>, DirectionType.In);\r\n<span style=\"color: #008000;\">\/\/Create the TypeDescriptor for the CustomerID parameter\r\n<\/span>customerIDParameter.CreateRootTypeDescriptor(<span style=\"color: #006080;\">\"CustomerId\"<\/span>, <span style=\"color: #0000ff;\">true<\/span>, <span style=\"color: #006080;\">\"System.Int32\"<\/span>,\r\n<span style=\"color: #006080;\">   \"CustomerId\"<\/span>, <span style=\"color: #0000ff;\">new<\/span> IdentifierReference(<span style=\"color: #006080;\">\"CustomerId\"<\/span>,\r\n<span style=\"color: #0000ff;\">   new<\/span> EntityReference(<span style=\"color: #006080;\">\"AdventureWorks\"<\/span>, <span style=\"color: #006080;\">\"Customer\"<\/span>, catalog), catalog),\r\n<span style=\"color: #0000ff;\">   null<\/span>, TypeDescriptorFlags.None, <span style=\"color: #0000ff;\">null<\/span>, catalog);\r\n<span style=\"color: #008000;\">\/\/Create the Customer return parameter\r\n<\/span>Parameter customerParameter =\r\n   getCustomerMethod.Parameters.Create(<span style=\"color: #006080;\">\"Customer\"<\/span>, <span style=\"color: #0000ff;\">true<\/span>, DirectionType.Return);\r\n<span style=\"color: #008000;\">\/\/Create the TypeDescriptors for the Customer return parameter\r\n<\/span>TypeDescriptor returnRootCollectionTypeDescriptor =\r\n   customerParameter.CreateRootTypeDescriptor( <span style=\"color: #006080;\">\"Customers\"<\/span>, <span style=\"color: #0000ff;\">true<\/span>,\r\n<span style=\"color: #006080;\">   \"System.Data.IDataReader, System.Data, Version=2.0.0.0, Culture=neutral,\r\n   PublicKeyToken=b77a5c561934e089\"<\/span>, <span style=\"color: #006080;\">\"Customers\"<\/span>, <span style=\"color: #0000ff;\">null<\/span>, <span style=\"color: #0000ff;\">null<\/span>,\r\n   TypeDescriptorFlags.IsCollection, <span style=\"color: #0000ff;\">null<\/span>, catalog);\r\nTypeDescriptor returnRootElementTypeDescriptor =\r\n   returnRootCollectionTypeDescriptor.ChildTypeDescriptors.Create( <span style=\"color: #006080;\">\"Customer\"<\/span>, <span style=\"color: #0000ff;\">true<\/span>,\r\n<span style=\"color: #006080;\">   \"System.Data.IDataRecord, System.Data, Version=2.0.0.0, Culture=neutral,\r\n   PublicKeyToken=b77a5c561934e089\"<\/span>,\r\n<span style=\"color: #006080;\">   \"Customer\"<\/span>, <span style=\"color: #0000ff;\">null<\/span>, <span style=\"color: #0000ff;\">null<\/span>, TypeDescriptorFlags.None, <span style=\"color: #0000ff;\">null<\/span>);\r\nreturnRootElementTypeDescriptor.ChildTypeDescriptors.Create( <span style=\"color: #006080;\">\"CustomerId\"<\/span>, <span style=\"color: #0000ff;\">true<\/span>,\r\n<span style=\"color: #006080;\">   \"System.Int32\"<\/span>, <span style=\"color: #006080;\">\"CustomerId\"<\/span>, <span style=\"color: #0000ff;\">new<\/span> IdentifierReference(<span style=\"color: #006080;\">\"CustomerId\"<\/span>,\r\n<span style=\"color: #0000ff;\">   new<\/span> EntityReference(<span style=\"color: #006080;\">\"AdventureWorks\"<\/span>, <span style=\"color: #006080;\">\"Customer\"<\/span>, catalog),\r\n   catalog), <span style=\"color: #0000ff;\">null<\/span>, TypeDescriptorFlags.None, <span style=\"color: #0000ff;\">null<\/span>);\r\nreturnRootElementTypeDescriptor.ChildTypeDescriptors.Create( <span style=\"color: #006080;\">\"FirstName\"<\/span>, <span style=\"color: #0000ff;\">true<\/span>,\r\n<span style=\"color: #006080;\">   \"System.String\"<\/span>, <span style=\"color: #006080;\">\"FirstName\"<\/span>,\r\n<span style=\"color: #0000ff;\">   null<\/span>, <span style=\"color: #0000ff;\">null<\/span>, TypeDescriptorFlags.None, <span style=\"color: #0000ff;\">null<\/span>);\r\nreturnRootElementTypeDescriptor.ChildTypeDescriptors.Create( <span style=\"color: #006080;\">\"LastName\"<\/span>, <span style=\"color: #0000ff;\">true<\/span>,\r\n<span style=\"color: #006080;\">   \"System.String\"<\/span>, <span style=\"color: #006080;\">\"LastName\"<\/span>,\r\n<span style=\"color: #0000ff;\">   null<\/span>, <span style=\"color: #0000ff;\">null<\/span>, TypeDescriptorFlags.None, <span style=\"color: #0000ff;\">null<\/span>);\r\n\r\nreturnRootElementTypeDescriptor.ChildTypeDescriptors.Create( <span style=\"color: #006080;\">\"Phone\"<\/span>, <span style=\"color: #0000ff;\">true<\/span>,\r\n<span style=\"color: #006080;\">   \"System.String\"<\/span>, <span style=\"color: #006080;\">\"Phone\"<\/span>, <span style=\"color: #0000ff;\">null<\/span>, <span style=\"color: #0000ff;\">null<\/span>, TypeDescriptorFlags.None, <span style=\"color: #0000ff;\">null<\/span>);\r\nreturnRootElementTypeDescriptor.ChildTypeDescriptors.Create( <span style=\"color: #006080;\">\"EmailAddress\"<\/span>, <span style=\"color: #0000ff;\">true<\/span>,\r\n<span style=\"color: #006080;\">   \"System.String\"<\/span>, <span style=\"color: #006080;\">\"EmailAddress\"<\/span>, <span style=\"color: #0000ff;\">null<\/span>, <span style=\"color: #0000ff;\">null<\/span>, TypeDescriptorFlags.None, <span style=\"color: #0000ff;\">null<\/span>);\r\nreturnRootElementTypeDescriptor.ChildTypeDescriptors.Create( <span style=\"color: #006080;\">\"CompanyName\"<\/span>, <span style=\"color: #0000ff;\">true<\/span>,\r\n<span style=\"color: #006080;\">   \"System.String\"<\/span>, <span style=\"color: #006080;\">\"CompanyName\"<\/span>, <span style=\"color: #0000ff;\">null<\/span>, <span style=\"color: #0000ff;\">null<\/span>, TypeDescriptorFlags.None, <span style=\"color: #0000ff;\">null<\/span>);\r\n<span style=\"color: #008000;\">\/\/Create the specific finder method instance\r\n<\/span>getCustomerMethod.MethodInstances.Create(<span style=\"color: #006080;\">\"GetCustomer\"<\/span>, <span style=\"color: #0000ff;\">true<\/span>,\r\n   returnRootElementTypeDescriptor, MethodInstanceType.SpecificFinder, <span style=\"color: #0000ff;\">true<\/span>);<\/pre>\n<h3>Define the Finder Method, Parameters and Type Descriptors<\/h3>\n<p>Next, create the finder <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/microsoft.sharepoint.businessdata.administration.client.method(office.14).aspx\" target=\"_blank\">Method<\/a>, specify the query it will use, and define the output parameters associated with it.  The finder <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/microsoft.sharepoint.businessdata.administration.client.method(office.14).aspx\" target=\"_blank\">Method<\/a> returns all of the rows of data from the data source which its query defines.<\/p>\n<div id=\"codeSnippetWrapper\">\n<pre id=\"codeSnippet\" style=\"text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: &quot;Courier New&quot;, courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;\"><span style=\"color: #008000;\">\/\/Create the Finder method\r\n<\/span>Method getCustomersMethod = customerEntity.Methods.Create(<span style=\"color: #006080;\">\"GetCustomers\"<\/span>, <span style=\"color: #0000ff;\">true<\/span>,\r\n   <span style=\"color: #0000ff;\">false<\/span>, <span style=\"color: #006080;\">\"GetCustomers\"<\/span>);\r\n<span style=\"color: #008000;\">\/\/Specify the query\r\n<\/span>getCustomersMethod.Properties.Add(<span style=\"color: #006080;\">\"RdbCommandText\"<\/span>, <span style=\"color: #006080;\">\"SELECT [CustomerId] ,\r\n[FirstName] , [LastName] , [Phone] , [EmailAddress] , [CompanyName]\r\nFROM [Customers].[SalesLT].[Customer]\"<\/span>);\r\n<span style=\"color: #008000;\">\/\/Set the command type\r\n<\/span>getCustomersMethod.Properties.Add(<span style=\"color: #006080;\">\"RdbCommandType\"<\/span>, <span style=\"color: #006080;\">\"Text\"<\/span>);\r\n<span style=\"color: #008000;\">\/\/Create the Customer return parameter\r\n<\/span>Parameter customersParameter = getCustomersMethod.Parameters.Create(<span style=\"color: #006080;\">\"Customer\"<\/span>, <span style=\"color: #0000ff;\">true<\/span>,\r\n   DirectionType.Return);\r\n<span style=\"color: #008000;\">\/\/Create the TypeDescriptors for the Customer return parameter\r\n<\/span>TypeDescriptor returnRootCollectionTypeDescriptor2 =\r\n   customersParameter.CreateRootTypeDescriptor(<span style=\"color: #006080;\">\"Customers\"<\/span>, <span style=\"color: #0000ff;\">true<\/span>,\r\n<span style=\"color: #006080;\">   \"System.Data.IDataReader, System.Data, Version=2.0.0.0, Culture=neutral,\r\n   PublicKeyToken=b77a5c561934e089\"<\/span>, <span style=\"color: #006080;\">\"Customers\"<\/span>, <span style=\"color: #0000ff;\">null<\/span>, <span style=\"color: #0000ff;\">null<\/span>,\r\n   TypeDescriptorFlags.IsCollection, <span style=\"color: #0000ff;\">null<\/span>, catalog);\r\nTypeDescriptor returnRootElementTypeDescriptor2 =\r\n   returnRootCollectionTypeDescriptor2.ChildTypeDescriptors.Create( <span style=\"color: #006080;\">\"Customer\"<\/span>, <span style=\"color: #0000ff;\">true<\/span>,\r\n   <span style=\"color: #006080;\">\"System.Data.IDataRecord, System.Data, Version=2.0.0.0, Culture=neutral,\r\nPublicKeyToken=b77a5c561934e089\"<\/span>, <span style=\"color: #006080;\">\"Customer\"<\/span>, <span style=\"color: #0000ff;\">null<\/span>, <span style=\"color: #0000ff;\">null<\/span>,\r\n   TypeDescriptorFlags.None, <span style=\"color: #0000ff;\">null<\/span>);\r\nreturnRootElementTypeDescriptor2.ChildTypeDescriptors.Create( <span style=\"color: #006080;\">\"CustomerId\"<\/span>, <span style=\"color: #0000ff;\">true<\/span>,\r\n   <span style=\"color: #006080;\">\"System.Int32\"<\/span>, <span style=\"color: #006080;\">\"CustomerId\"<\/span>, <span style=\"color: #0000ff;\">new<\/span> IdentifierReference(<span style=\"color: #006080;\">\"CustomerId\"<\/span>,\r\n<span style=\"color: #0000ff;\">   new<\/span> EntityReference(<span style=\"color: #006080;\">\"AdventureWorks\"<\/span>, <span style=\"color: #006080;\">\"Customer\"<\/span>, catalog), catalog),\r\n<span style=\"color: #0000ff;\">   null<\/span>, TypeDescriptorFlags.None, <span style=\"color: #0000ff;\">null<\/span>);\r\nreturnRootElementTypeDescriptor2.ChildTypeDescriptors.Create( <span style=\"color: #006080;\">\"FirstName\"<\/span>, <span style=\"color: #0000ff;\">true<\/span>,\r\n<span style=\"color: #006080;\">   \"System.String\"<\/span>, <span style=\"color: #006080;\">\"FirstName\"<\/span>, <span style=\"color: #0000ff;\">null<\/span>, <span style=\"color: #0000ff;\">null<\/span>, TypeDescriptorFlags.None, <span style=\"color: #0000ff;\">null<\/span>);\r\nreturnRootElementTypeDescriptor2.ChildTypeDescriptors.Create( <span style=\"color: #006080;\">\"LastName\"<\/span>, <span style=\"color: #0000ff;\">true<\/span>,\r\n<span style=\"color: #006080;\">   \"System.String\"<\/span>, <span style=\"color: #006080;\">\"LastName\"<\/span>, <span style=\"color: #0000ff;\">null<\/span>, <span style=\"color: #0000ff;\">null<\/span>, TypeDescriptorFlags.None, <span style=\"color: #0000ff;\">null<\/span>);\r\nreturnRootElementTypeDescriptor2.ChildTypeDescriptors.Create( <span style=\"color: #006080;\">\"Phone\"<\/span>, <span style=\"color: #0000ff;\">true<\/span>,\r\n<span style=\"color: #006080;\">   \"System.String\"<\/span>, <span style=\"color: #006080;\">\"Phone\"<\/span>, <span style=\"color: #0000ff;\">null<\/span>, <span style=\"color: #0000ff;\">null<\/span>, TypeDescriptorFlags.None, <span style=\"color: #0000ff;\">null<\/span>);\r\nreturnRootElementTypeDescriptor2.ChildTypeDescriptors.Create( <span style=\"color: #006080;\">\"EmailAddress\"<\/span>, <span style=\"color: #0000ff;\">true<\/span>,\r\n<span style=\"color: #006080;\">   \"System.String\"<\/span>, <span style=\"color: #006080;\">\"EmailAddress\"<\/span>, <span style=\"color: #0000ff;\">null<\/span>, <span style=\"color: #0000ff;\">null<\/span>, TypeDescriptorFlags.None, <span style=\"color: #0000ff;\">null<\/span>);\r\nreturnRootElementTypeDescriptor2.ChildTypeDescriptors.Create( <span style=\"color: #006080;\">\"CompanyName\"<\/span>, <span style=\"color: #0000ff;\">true<\/span>,\r\n<span style=\"color: #006080;\">   \"System.String\"<\/span>, <span style=\"color: #006080;\">\"CompanyName\"<\/span>, <span style=\"color: #0000ff;\">null<\/span>, <span style=\"color: #0000ff;\">null<\/span>, TypeDescriptorFlags.None, <span style=\"color: #0000ff;\">null<\/span>);\r\ngetCustomersMethod.MethodInstances.Create(<span style=\"color: #006080;\">\"GetCustomers\"<\/span>, <span style=\"color: #0000ff;\">true<\/span>,\r\n   returnRootCollectionTypeDescriptor2, MethodInstanceType.Finder, <span style=\"color: #0000ff;\">true<\/span>);<\/pre>\n<h3>Commit the changes<\/h3>\n<p>Finally, commit the changes to the BCS Metadata Store.<\/p>\n<div id=\"codeSnippetWrapper\">\n<pre id=\"codeSnippet\" style=\"text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: &quot;Courier New&quot;, courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;\"><span style=\"color: #008000;\">\/\/Publish the Customer Entity\r\n<\/span>customerEntity.Activate();<\/pre>\n<h2>Verification<\/h2>\n<p>After the code executes check Central Administration to verify the external content type is successfully created.  Then create an external list to verify the external content type works as expected.<\/p>\n<h3>Central Administration<\/h3>\n<p>To verify the external content type is successfully created follow these steps.<\/p>\n<ol>\n<li>Open SharePoint Central Administration<\/li>\n<li>Click Application Management<\/li>\n<li>Click Manage service applications<\/li>\n<li>Click the Business Data Connectivity service<\/li>\n<li>In the ribbon, click Manage<\/li>\n<li>In the ribbon, click Edit<\/li>\n<li>Select BDC Models in the View dropdown list<\/li>\n<li>Verify the BDC Model is in the list <a rel=\"lightbox\" href=\"https:\/\/toddbaginski.com\/blog\/images\/www_toddbaginski_com\/blog\/WindowsLiveWriter\/HOWTOProgrammaticallycreateanExternalCon_CF4C\/BDC%20Model%20List_4.png\"><img loading=\"lazy\" decoding=\"async\" style=\"display: inline; border-width: 0px;\" title=\"BDC Model List\" src=\"https:\/\/toddbaginski.com\/blog\/images\/www_toddbaginski_com\/blog\/WindowsLiveWriter\/HOWTOProgrammaticallycreateanExternalCon_CF4C\/BDC%20Model%20List_thumb_1.png\" border=\"0\" alt=\"BDC Model List\" width=\"142\" height=\"71\" \/><\/a><\/li>\n<li>In the ribbon, click Edit<\/li>\n<li>Select External Content Types in the View dropdown list<\/li>\n<li>Verify the BCS Model is in the list <a rel=\"lightbox\" href=\"https:\/\/toddbaginski.com\/blog\/images\/www_toddbaginski_com\/blog\/WindowsLiveWriter\/HOWTOProgrammaticallycreateanExternalCon_CF4C\/External%20Content%20Types%20List_4.png\"><img loading=\"lazy\" decoding=\"async\" style=\"display: inline; border-width: 0px;\" title=\"External Content Types List\" src=\"https:\/\/toddbaginski.com\/blog\/images\/www_toddbaginski_com\/blog\/WindowsLiveWriter\/HOWTOProgrammaticallycreateanExternalCon_CF4C\/External%20Content%20Types%20List_thumb_1.png\" border=\"0\" alt=\"External Content Types List\" width=\"600\" height=\"54\" \/><\/a>  <\/li>\n<\/ol>\n<h3>Create An External List<\/h3>\n<p>To verify the external content type works as expected follow these steps.<\/p>\n<ol>\n<li>Open the SharePoint web site where you created the external content type<\/li>\n<li>Log in as a user who has the permission to create lists<\/li>\n<li>Click the Site Actions menu<\/li>\n<li>Select View All Site Content<\/li>\n<li>Click Create<\/li>\n<li>In the Filter By section, click List<\/li>\n<li>Select External List<br \/>\n<a rel=\"lightbox\" href=\"https:\/\/toddbaginski.com\/blog\/images\/www_toddbaginski_com\/blog\/WindowsLiveWriter\/HOWTOProgrammaticallycreateanExternalCon_CF4C\/Create%20External%20List_2.png\"><img loading=\"lazy\" decoding=\"async\" style=\"display: inline; border-width: 0px;\" title=\"Create External List\" src=\"https:\/\/toddbaginski.com\/blog\/images\/www_toddbaginski_com\/blog\/WindowsLiveWriter\/HOWTOProgrammaticallycreateanExternalCon_CF4C\/Create%20External%20List_thumb.png\" border=\"0\" alt=\"Create External List\" width=\"600\" height=\"377\" \/><\/a><\/li>\n<li>Click the Create button<\/li>\n<li>In the Name textbox enter Programmatically Generated External Content Type<\/li>\n<li>Click the button to browse the external content types<br \/>\n <a rel=\"lightbox\" href=\"https:\/\/toddbaginski.com\/blog\/images\/www_toddbaginski_com\/blog\/WindowsLiveWriter\/HOWTOProgrammaticallycreateanExternalCon_CF4C\/External%20Content%20Type%20Picker_2.png\"><img loading=\"lazy\" decoding=\"async\" style=\"display: inline; border-width: 0px;\" title=\"External Content Type Picker\" src=\"https:\/\/toddbaginski.com\/blog\/images\/www_toddbaginski_com\/blog\/WindowsLiveWriter\/HOWTOProgrammaticallycreateanExternalCon_CF4C\/External%20Content%20Type%20Picker_thumb.png\" border=\"0\" alt=\"External Content Type Picker\" width=\"301\" height=\"57\" \/><\/a><\/li>\n<li>Select the Customer External Content Type in the External Content Type Picker<br \/>\n<a rel=\"lightbox\" href=\"https:\/\/toddbaginski.com\/blog\/images\/www_toddbaginski_com\/blog\/WindowsLiveWriter\/HOWTOProgrammaticallycreateanExternalCon_CF4C\/External%20Content%20Type%20Picker_4.png\"><img loading=\"lazy\" decoding=\"async\" style=\"display: inline; border-width: 0px;\" title=\"External Content Type Picker\" src=\"https:\/\/toddbaginski.com\/blog\/images\/www_toddbaginski_com\/blog\/WindowsLiveWriter\/HOWTOProgrammaticallycreateanExternalCon_CF4C\/External%20Content%20Type%20Picker_thumb_1.png\" border=\"0\" alt=\"External Content Type Picker\" width=\"583\" height=\"149\" \/><\/a><\/li>\n<li>Click OK<\/li>\n<li>Click Create<\/li>\n<li>View the data from the data source in the external list<br \/>\n<a rel=\"lightbox\" href=\"https:\/\/toddbaginski.com\/blog\/images\/www_toddbaginski_com\/blog\/WindowsLiveWriter\/HOWTOProgrammaticallycreateanExternalCon_CF4C\/Data%20in%20external%20list_4.png\"><img loading=\"lazy\" decoding=\"async\" style=\"display: inline; border-width: 0px;\" title=\"Data in external list\" src=\"https:\/\/toddbaginski.com\/blog\/images\/www_toddbaginski_com\/blog\/WindowsLiveWriter\/HOWTOProgrammaticallycreateanExternalCon_CF4C\/Data%20in%20external%20list_thumb_1.png\" border=\"0\" alt=\"Data in external list\" width=\"600\" height=\"265\" \/><\/a><\/li>\n<\/ol>\n<h1>Wrap Up<\/h1>\n<p>As always, I hope this article saves you time and effort and helps you understand how to put this technology to use.  There are several good BCS articles appearing on the Internet lately.  You may check them out at the following blogs.<\/p>\n<p><a href=\"http:\/\/blogs.msdn.com\/bcs\/\" target=\"_blank\">BCS Team Blog<\/a><br \/>\n<a href=\"http:\/\/www.lightningtools.com\/blog\/\" target=\"_blank\">Nick Swan\u2019s blog<\/a><br \/>\n<a href=\"http:\/\/fabiangwilliams.wordpress.com\/\" target=\"_blank\">Fabian Williams\u2019 blog<\/a><\/p>\n<p>Yeah baby, only 12 days and I\u2019m on vacation in Mexico!!!<\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>I demonstrated how to make an external content type programmatically during the SPC 405 &#8211; Business Connectivity Services Runtime and Object Model Deep Dive session at<span class=\"excerpt-hellip\"> [\u2026]<\/span><\/p>\n","protected":false},"author":1,"featured_media":231,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[46],"tags":[28,4,10,30,5,6,7,26],"class_list":["post-60","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-most-popular","tag-bcs","tag-bdc","tag-custom-development","tag-external-content-types","tag-helpful-code","tag-how-to-sharepoint","tag-sharepoint","tag-sharepoint-2010"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/toddbaginski.com\/blog\/wp-json\/wp\/v2\/posts\/60","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/toddbaginski.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/toddbaginski.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/toddbaginski.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/toddbaginski.com\/blog\/wp-json\/wp\/v2\/comments?post=60"}],"version-history":[{"count":7,"href":"https:\/\/toddbaginski.com\/blog\/wp-json\/wp\/v2\/posts\/60\/revisions"}],"predecessor-version":[{"id":318,"href":"https:\/\/toddbaginski.com\/blog\/wp-json\/wp\/v2\/posts\/60\/revisions\/318"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/toddbaginski.com\/blog\/wp-json\/wp\/v2\/media\/231"}],"wp:attachment":[{"href":"https:\/\/toddbaginski.com\/blog\/wp-json\/wp\/v2\/media?parent=60"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/toddbaginski.com\/blog\/wp-json\/wp\/v2\/categories?post=60"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/toddbaginski.com\/blog\/wp-json\/wp\/v2\/tags?post=60"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}