Second Life of a Hungarian SharePoint Geek

February 1, 2011

How to get a BCS metadata catalog without specifying a URL?

Filed under: BCS, SP 2010 — Tags: , — Peter Holpar @ 23:06

It was the original question in this MSDN forum thread.

As you probably know, we can get a reference for a BCS catalog from client side code (Microsoft.SharePoint.BusinessData.Administration.Client) like this:

AdministrationMetadataCatalog catalog = AdministrationMetadataCatalog.GetCatalog(siteUrl);

On the server side (Microsoft.SharePoint.BusinessData.Administration) there are two different options. If the process runs in the SharePoint context, you can use a code that does not require the URL:

BdcService service = SPFarm.Local.Services.GetValue<BdcService>();
AdministrationMetadataCatalog catalog = service.GetAdministrationMetadataCatalog(SPServiceContext.Current);

However if your code runs out of SharePoint context, you need a URL again to get the SPServiceContext reference:

SPServiceContext.GetContext(siteUrl)

In this specific question we had to solve the puzzle for the server side, out of SharePoint server context variation, to be able to use the code from an STSADM command.

Although my first reply (one has to provide some kind of context for the BdcSerivce to be able to return a catalog to work with) remained true, it turned out one can get a service context without specifying a site URL, and it can be done using the following code snippet:

  1. private AdministrationMetadataCatalog GetBdcCatalog()
  2. {
  3.     AdministrationMetadataCatalog catalog = null;
  4.  
  5.     try
  6.     {
  7.         BdcService bdcService = SPFarm.Local.Services.GetValue<BdcService>();
  8.         SPServiceApplication serviceApp = bdcService.Applications.FirstOrDefault();
  9.  
  10.         if (serviceApp != null)
  11.         {
  12.             SPServiceContext context = SPServiceContext.GetContext(serviceApp.ServiceApplicationProxyGroup, SPSiteSubscriptionIdentifier.Default);
  13.             catalog = bdcService.GetAdministrationMetadataCatalog(context);
  14.         }
  15.     }
  16.     catch (Exception ex)
  17.     {
  18.         // do error handling
  19.     }
  20.  
  21.     return catalog;
  22.  
  23. }

A bit tricky, but seems to work as expected.

Advertisements

Leave a Comment »

No comments yet.

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

Blog at WordPress.com.

%d bloggers like this: