And that huge advantage, is the ability to represent
any back-end system, in a consistent object model that can be
programmed against. This object model is the BCS object model. What
this means is that as the back-end systems change and upgrade over
time, as long as their equivalent BCS external content types are kept
up to date, all existing systems can continue to leverage the same
object model, and existing systems will continue to work with changing
newer versions of back-end systems without even the need of a
recompile. This is because, as long as you update the BCS external
content types, the actual BCS object model won't change, and will
continue to work and will simply reflect the updated external content
types.
The sample that I am about to show assumes that you
have the "NorthWind Customer" external content type setup in your
system. The BCS object model provides you with functionality to both
query and maintain the catalog, or to execute methods on individual
external content types.
Start Visual Studio 2010 and create a .net 3.5
console application. Make sure the target platform is AnyCPU, and add
the following references.
C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\ISAPI\Microsoft.Sharepoint.dll
C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\ISAPI\Microsoft.BusinessData.dll
Microsoft.Sharepoint.BusinessData.Administration.Client.dll from the GAC
In your console application add a static variable pointing to your SharePoint site as shown below:
privatestaticstringsiteUrl = "http://sp2010";
The you can browse through the existing catalog by
using the AdministrationMetaDataCatalog object in the
Microsoft.Sharepoint.BusinssData.Administration.Client namespace. This
can be seen in Listing 1.
Example 1. Browsing through the catalog on your Sharepoint installation.
private static void BrowseCatalogDetails() { Console.WriteLine("Now Browsing the details of the catalog:"); AdministrationMetadataCatalog catalog = AdministrationMetadataCatalog.GetCatalog(siteUrl); EntityCollection entities = catalog.GetEntities("*", "*", true); Console.WriteLine("\nEntities in the system:"); foreach (Entity entity in entities) { Console.WriteLine(entity.Name); }
// Lets pick the first entity var entityEnum = entities.GetEnumerator(); entityEnum.MoveNext(); Entity firstEntity = entityEnum.Current; Console.WriteLine("\nMethods on the first Entity:"); foreach (var method in firstEntity.Methods) { Console.WriteLine(method.Name); } }
|
As you can see from Listing 1,
you have access to all the entities and all the details of all the
entities in the catalog. In fact if you poke around the object model,
you will be able to find that purely through the object model you can
create the new entities in the catalog as well. This is very similar to
what used to be able to do in SharePoint 2007 using the BDC object
model. However there is one big difference that may not be very
apparent! Note that the
Microsoft.Sharepoint.BusinssData.Administration.Client namespace is
actually part of the client stack. In other words, the code shown above
does not need to run on the server! In SharePoint 2007 you were
required to run this code on the machine that was running the SSP and
then expose it as a web service. That is no longer necessary.
Next let us look at an example of actually executing a method on an entity, and writing the results out. This can be seen in Listing 2.
Example 2. Executing a method and writing the results out.
private static void ExecuteMethod() { Console.WriteLine("\nNow Executing methods"); using (SPSite site = newSPSite(siteUrl)) { using (newSPServiceContextScope(SPServiceContext.GetContext(site))) { BdcService service = SPFarm.Local.Services.GetValue<BdcService>(); IMetadataCatalog catalog = service.GetDatabaseBackedMetadataCatalog(SPServiceContext.Current);
IEntity entity = catalog.GetEntity(siteUrl, "Northwind Customer"); ILobSystemInstance LobSysteminstance = entity.GetLobSystem().GetLobSystemInstances()[0].Value;
IMethodInstance method = entity.GetMethodInstance("Read List", MethodInstanceType.Finder); IEntityInstanceEnumerator ieie = entity.FindFiltered(method.GetFilters(), LobSysteminstance);
Console.WriteLine("\nCustomers in Northwind:"); while (ieie.MoveNext()) { Console.WriteLine(ieie.Current["ContactName"].ToString()); } } } }
|
As you can see from Listing 2,
I'm able to use a consistent object model, irrespective of the details
of the external content type, or the back-end system, and be able to
execute methods on the back-end system. Thus as the back-end system
changes, and the external content type is maintained, the code shown in
Listing 2
will remain unchanged. This is the value that BCS offers! Also if you
poke through the object model further, you will see significant
enhancements in the object model over the SharePoint 2007 equivalent.
For instance in SharePoint 2010, you now have support for bulk
operations, and for reading blob types with streaming support.