Second Life of a Hungarian SharePoint Geek

August 23, 2010

Deleting BCS external systems from code

Filed under: BCS, SP 2010 — Tags: , — Peter Holpar @ 22:20

When working Visual Studio 2010 projects containing Business Data Connectivity Model items, you may find that after the project is started and the BDC model is deployed, but when the project is stopped, the External System for the model is not deleted as part of the retraction process.

That is quite disturbing, since it may cause you headache when you would like to alter the model as described here.

You can delete it manually before re-deploying the project through Cental Administartion / Manage Service Application (Application Management) / Business Data Connectivity Service (Manage) then selecting External Systems (2nd menu item) from the drop down on the top right side of the menu then selecting the check box on the left of your model and clicking Delete on the menu (or simply selecting Delete from the context menu) as illustrated below.

image

Or you can delete the External System programmatically (after adding reference to the Microsoft.BusinessData assembly located in the 14\ISAPI folder) using the following code snippets:

  1. using Microsoft.SharePoint.BusinessData.SharedService;
  2. using Microsoft.SharePoint.BusinessData.Administration;

  1. private void DeleteExternalSystem(SPSite site, String lobSystemName)
  2. {
  3.     SPServiceContext serviceContext = SPServiceContext.GetContext(site);
  4.     BdcServiceApplicationProxy bdcServiceApplicationProxy = (BdcServiceApplicationProxy)serviceContext.GetDefaultProxy(typeof(BdcServiceApplicationProxy));
  5.  
  6.     AdministrationMetadataCatalog administrationMetadataCatalog = bdcServiceApplicationProxy.GetAdministrationMetadataCatalog();
  7.  
  8.     LobSystem lobSystem = null;
  9.     
  10.     try
  11.     {
  12.         lobSystem = administrationMetadataCatalog.GetLobSystem(lobSystemName);
  13.     }
  14.     catch (MetadataObjectNotFoundException ex)
  15.     {
  16.         Console.WriteLine(ex.Message);
  17.     }
  18.  
  19.     if (lobSystem != null)
  20.     {
  21.         if (lobSystem.GetEntityCount() == 0)
  22.         {
  23.             Console.WriteLine("Deleting external system instance '{0}'", lobSystemName);
  24.             lobSystem.Delete();
  25.         }
  26.         else
  27.         {
  28.             if (lobSystem.HasOnlyInactiveEntities())
  29.             {
  30.                 foreach (Entity entity in lobSystem.Entities)
  31.                 {
  32.                     Console.WriteLine("Deleting entity '{0}'", entity.Name);
  33.                     entity.Delete();
  34.                 }
  35.                 // should not delete lobSystem, it is deleted with the last entity
  36.                 // lobSystem.Delete();
  37.             }
  38.             else
  39.             {
  40.                 Console.WriteLine("The external system instance '{0}' is referenced by active entities", lobSystemName);
  41.             }
  42.         }
  43.     }
  44. }

In the DeleteExternalSystem method we use the following extension method to check if there is any active entity in the LOB system:

  1. public static class Extensions
  2. {
  3.     public static bool HasOnlyInactiveEntities(this LobSystem lobSystem)
  4.     {
  5.         if (lobSystem == null)
  6.         {
  7.             throw new ArgumentNullException("lobSystem");
  8.         }
  9.         if (lobSystem.GetEntityCount() == 0)
  10.         {
  11.             return true;
  12.         }
  13.         foreach (Entity entity in lobSystem.Entities)
  14.         {
  15.             if (entity.Active)
  16.             {
  17.                 return false;
  18.             }
  19.         }
  20.         return true;
  21.     }
  22. }

Having these added to your code, you can delete the external system using the following method call:

DeleteExternalSystem(site, "YourBDCModel");

Next step may be to create a custom deployment step in Visual Studio using this code and to add that step to the retraction steps of the SharePoint project.

Advertisements

2 Comments »

  1. […] Note, that you should modify the value of the Folder property to match the path of your local links folder created earlier. If you alter the BDC model and deploy multiple times, don’t forget to delete the corresponding external system between as described in my former post. […]

    Pingback by External list example demonstrating .NET connectivity assembly and custom field type « Second Life of a Hungarian SharePoint Geek — September 11, 2010 @ 03:45

  2. I was stumped on why iterative changes were not taking place on BCS solutions, and this pointed me in the right direction… seems like yet another case of orphaned parent-child relationship fun! Thanks!

    Comment by Jason Lochan — February 25, 2012 @ 00:43


RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at WordPress.com.

%d bloggers like this: