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).

ExterShare1.gif

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

  1. Open Visual Studio 2010.
  2. Go to File => New => project.
  3. Select Console Application template from the installed templates.
  4. Select .NET Framework 3.5 when you create the project.
  5. Add the following references to the project:

    a. Microsoft.BusinessData (from SharePoint_RootFolder\ISAPI)
    b. Microsoft.SharePoint
  6. 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;
     
  7. Replace Program.cs with the following code:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Microsoft.SharePoint.BusinessData;
    using Microsoft.SharePoint;
    using Microsoft.SharePoint.BusinessData.SharedService;
    using Microsoft.BusinessData.MetadataModel;
    using Microsoft.BusinessData;
    using Microsoft.SharePoint.Administration;

    namespace BDCService
    {
        class Program
        {
            static void Main(string[] args)
            {
                using (SPSite site = new SPSite("http://serverName:1111/hr/MP/"))
                {
                    SPServiceContext serviceContext = SPServiceContext.GetContext(site);
                    BdcService bdc = SPFarm.Local.Services.GetValue<BdcService>();
                    IMetadataCatalog metadataCatalog = bdc.GetDatabaseBackedMetadataCatalog(serviceContext);
                    foreach (IEntity ect in metadataCatalog.GetEntities("*"))
                    {
                        Console.WriteLine("Name: {0}",ect.Name);     
                        Console.WriteLine("Namespace: {0}", ect.Namespace);
                       Console.WriteLine("Version: {0}", ect.Version.ToString());                   

                    }
                    Console.ReadLine();
                }
            }
        }
    }

     

  8. Right click on the solution, Click on Build.

  9. Hit F5.

Output:

ExterShare2.gif

Up Next
    Ebook Download
    View all
    Learn
    View all