Programmatically get all the external content types in SharePoint 2010


In this article we will be seeing shows how to get all the External Content Types by using the Business Data Connectivity (BDC) service Runtime object model.

Description

The following code example shows how to get all the External Content Types by using the Business Data Connectivity (BDC) service Runtime object model on the server. I have one external content type in BDC metadata store ( Go to Central Administration => Application Management => Manage Service Applications => Business Data Connectivity Service Application).



Prerequisites

  • Microsoft SharePoint Server 2010 or Microsoft SharePoint Foundation 2010 installed on the server

  • Microsoft .NET Framework 3.5 and Microsoft Visual Studio 2010

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


Example

  • Open Visual Studio 2010.

  • Go to File => New => project.

  • Select Console Application template from the installed templates.

  • Select .NET Framework 3.5 when you create the project.

  • Add the following references to the project:

a. Microsoft.BusinessData (from SharePoint_RootFolder\ISAPI) b. Microsoft.SharePoint

  • Add the following namespaces: a. Using Microsoft.BusinessData; b. Using Microsoft.SharePoint; c. Using Microsoft.SharePoint.Administration; d. using Microsoft.SharePoint.BusinessData.SharedService; e. using Microsoft.BusinessData.MetadataModel;

  • Replace Program.cs with the following code:

  1. using System;

  2. using System.Collections.Generic;

  3. using System.Linq;

  4. using System.Text;

  5. using Microsoft.SharePoint.BusinessData;

  6. using Microsoft.SharePoint;

  7. using Microsoft.SharePoint.BusinessData.SharedService;

  8. using Microsoft.BusinessData.MetadataModel;

  9. using Microsoft.BusinessData;

  10. using Microsoft.SharePoint.Administration;

  11. namespace BDCService

  12. {

  13. class Program

  14. {

  15. static void Main(string[] args)       

  16. {

  17. using (SPSite site = new SPSite("http://serverName:1111/hr/MP/"))        

  18. {

  19. SPServiceContext serviceContext = SPServiceContext.GetContext(site);

  20. BdcService bdc =SPFarm.Local.Services.GetValue<BdcService>();

  21. IMetadataCatalog metadataCatalog = bdc.GetDatabaseBackedMetadataCatalog(serviceContext);

  22. foreach (IEntity ect in metadataCatalog.GetEntities("*"))               

  23. {

  24. Console.WriteLine("Name: {0}",ect.Name);               

  25. Console.WriteLine("Namespace: {0}", ect.Namespace);      

  26. Console.WriteLine("Version: {0}", ect.Version.ToString());                                   

  27. }

  28. Console.ReadLine();           

  29. }       

  30. }

  31. }

  • Right click on the solution, click on build.

  • 5.


Output: