Ever wondered how to write code against the BDC?

by Shawn March 03, 2009 10:01

SharePoint 2007: BDC - The Business Data Catalog  (this is from http://blah.winsmarts.com/2007-4-SharePoint_2007__BDC_-_Writing_Custom_Code_against_the_runtime_object_model.aspx)

This post is in continuation to a series of blogposts I put up on BDC - the Business Data Catalog.

Table of Contents:

  • Introduction
  • In-built Webparts
  • Lists with Business data type columns.
  • Search
  • User Profiles
  • Other custom applications targeting a common runtime object model.  <--- You are here.
  • Making it easier to author XML Applications: BDCMetaMan

    Whew!

    My long series on BDC is coming to an end. What a bitch it has been to type so much LOL. So first of all, if I missed something, please ping me, so I can blog about it.

    In this last post, I am going to target the application we wrote here, using custom code.

    Here is a quick sample application targeting literally any BDC app.

    Sniffing your BDC App.

    static void Main(string[] args)
    {
    	string ssp = "SharedServices1";
    	
    	SqlSessionProvider.Instance().SetSharedResourceProviderToUse(ssp);
    	NamedLobSystemDictionary lobSystems = ApplicationRegistry.GetLobSystems();
    	
    	foreach (LobSystem system in lobSystems.Values)
    	{
    		Console.WriteLine("Application: " + system.Name);
    		
    		NamedDataClassDictionary lobClasses = system.GetDataClasses();
    		
    		foreach (DataClass dataClass in lobClasses.Values)
    		{
    			Console.WriteLine("  Entities: " + dataClass.Name);
    		}
    	}
    }
    
  • If I run the above, I get this output -

    Application: NorthWindApp
      Entities: Order
      Entities: Customer

    If you are wondering, I am using namespaces

        Microsoft.Office.Server.ApplicationRegistry.MetaDataModel
        Microsoft.Office.Server.ApplicationRegistry.Infrastructure.

    The references are

        Microsoft.Office.Server.dll
        Microsoft.SharePoint.dll
        Microsooft.SharePoint.Portal.dll

    Now, Let us slice and dice the above code step by step.

    The first thing you see in the above code is SqlSessionProvider. SqlSessionProvider represents a way to connect to the SSP. It immediately sticks out that the BDC runtime can target only the local farm. So, if you wish to allow BDC access over the network, you will need to write a webservice and deploy it on the farm, and access it via the web service.

    Once you get a hold of an instance using SqlSessionprovider.Instance(), you can then start querying it for various stuff. For instance, the first thing I wanted to find out were all the LobSystems (i.e. applications) registered in the SSP. And once I have the applications found, I can find the individual entities in each one of those classes.

    Once I have the individual entities, I can easily find the methods that entity has using the code below -

    DataClass customers = lobClasses["Customer"];
    NamedMethodDictionary methods = customers.GetMethods();
    
    foreach (Method meth in methods.Values)
    {
    	Console.WriteLine("Method: " + meth.Name);
    }
    

    You can continue to sniff around, and you will see that you can find methods, method instances, properties, heck if you snooped around enough, you could literally smell the entire metadata for the BDC app.

    So now that I have information about the BDC app, the next question is "How to use it".

    Using your BDC App.

    In order to actually use your BDC App, you need to first get a hold of the LobSystemInstances. Check the code out below -

    static void Main(string[] args)
    {
    	string ssp = "SharedServices1" ;
    	
    	SqlSessionProvider.Instance().SetSharedResourceProviderToUse(ssp);
    	
    	NamedLobSystemDictionary lobSystems = ApplicationRegistry.GetLobSystems();
    	
    	foreach (LobSystem system in lobSystems.Values)
    	{
    		Console.WriteLine("Application: " + system.Name);
    
    		NamedLobSystemInstanceDictionary instances = 
    		
    		lobSystem.GetLobSystemInstances();
    		
    		foreach (LobSystemInstance instance in instances.Values)
    		{
    			Console.WriteLine(instance.Name);
    		}
    	}
    }
    

    You might ask, what is the difference between a LobSystem and a LobSystemInstance? BDC in MOSS 2007 lets you put upto two instances on each LobSystem. This means, you get different authentication settings for the two instances, for the same application. This is incredibly useful, say when you are setting up search on BDC, because you would typically want search to work under different credentials than regular BDC usage by a user.

    Once you have the specific instance, you can then find the entity, the filters, set filter values, and finally execute the Finder method using the following code -

    Entity customerEntity = instance.GetEntities()["Customer"];
    FilterCollection filterColl = customerEntity.GetFinderFilters();
    
    (filterColl[0] as WildcardFilter).Value = "London";
    
    IEntityInstanceEnumerator customerEnum = customerEntity.FindFiltered(filterColl, instance);
    
    while(customerEnum.MoveNext())
    {
    	DataTable dt = (customerEnum.Current as DbEntityInstance).EntityAsDataTable;
    	PrintDataRow(dt.Rows[0]);
    }
    

    PrintDataRow is simply a method that prints out the values of the DataRow in a single line.

    The full final code looks like this.

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Data;
    using Microsoft.Office.Server.ApplicationRegistry.MetadataModel;
    using Microsoft.Office.Server.ApplicationRegistry.Infrastructure;
    using Microsoft.Office.Server.ApplicationRegistry.Runtime;
    using Microsoft.Office.Server.ApplicationRegistry.SystemSpecific.Db;
    
    namespace SampleBDCApp
    {
    	class Program
    	{
    		static void Main(string[] args)
    		{
    			string ssp = "SharedServices1";
    			SqlSessionProvider.Instance().SetSharedResourceProviderToUse(ssp);
    
                NamedLobSystemDictionary lobSystems = ApplicationRegistry.GetLobSystems();
    			
                foreach (LobSystem system in lobSystems.Values)
                {
    				Console.WriteLine("Application: " + system.Name);
                    NamedLobSystemInstanceDictionary instances =                     system.GetLobSystemInstances();
    				
                    foreach (LobSystemInstance instance in instances.Values)
                    {
    					Console.WriteLine(instance.Name);
    					
    					Entity customerEntity = instance.GetEntities()["Customer"];
    					FilterCollection filterColl = customerEntity.GetFinderFilters() ;
    					
                        (filterColl[0] as WildcardFilter).Value = "London";
                        IEntityInstanceEnumerator customerEnum =  customerEntity.FindFiltered(filterColl, instance);
    					
                        while(customerEnum.MoveNext())
                        {
    						DataTable dt = (customerEnum.Current as DbEntityInstance).EntityAsDataTable;
    					PrintDataRow(dt.Rows[0]);
                        }
    				}
    			}
    		}
    		
            private static void PrintDataRow(DataRow dr)
            {
    			foreach (DataColumn dataColumn in dr.Table.Columns)
                {
    				Console.Write(dr[dataColumn] + "\t");
    			}
    			Console.WriteLine();
    		}
    	}
    } 			
    

    .. and when you execute this, you get the following output -

    Congratulations - you've just written your first application using BDC. You can now implement the above in a web service, and expose any underlying LOB using a consistent BDC API.

    Here is an interesting exercise for you to try - Try using Office SSO + WindowsCredentials, so the end application's identity matters when calling the web service, and that same identity goes all the way to the web service.

    Have fun!

    Tags:

    Development | SharePoint

    Comments

    8/15/2010 6:21:02 AM #

    Today Gossip and News

    The catchy blog with the interesting contents. You give the very useful information that many people don't know before. most of your contents are make me have more knowledge. it is very different. I was impressed with your website. Never be bored to visit your blog again. Have the nice your time.Keep enjoyed your blogging.

    Today Gossip and News United States

    8/16/2010 11:37:44 PM #

    Jame Huckabee

    Hi!
    Thanks Much for providing  some creative information  on the topic.

    I am saving your website and for sure  definitely check back often.

    Jame Huckabee United States

    8/18/2010 7:58:27 PM #

    Bradly

    wow what a fountian of knowledge thanks guys!

    Bradly Oman

    8/18/2010 10:34:35 PM #

    tips and information

    What a super blog!

    tips and information Algeria

    8/19/2010 12:30:47 AM #

    Mens Engagement Rings

    Excellent source of information. I'm browsing through your blog and found some other great posts!

    Mens Engagement Rings United States

    8/19/2010 7:13:31 PM #

    shaun t

    A excellent way to lose fat is following a good fitness routine plus a sensible diet strategy. It's that basic.

    shaun t United States

    8/20/2010 5:20:03 AM #

    Credit Suisse Gold Bars

    Comfortably, the post is actually the greatest on this notable topic. I agree with your conclusions and will thirstily look forward to your incoming updates. Saying thanks will not just be sufficient, for the exceptional lucidity in your writing. I will directly grab your rss feed to stay abreast of any updates. Authentic work and much success in your business dealings!

    Credit Suisse Gold Bars United States

    8/22/2010 7:21:24 PM #

    Fat Loss for Idiots

    I have not yet read this blog article, but it seems very intersting.I would like to read more blogs from you in future. Keep up the good work.

    Fat Loss for Idiots United States

    8/22/2010 10:09:29 PM #

    Camgirls

    You've made a few posts that have had a major impact on my thinking. I just wanted to say thank you.   [more...]

    Camgirls United States

    8/23/2010 4:33:46 PM #

    Bradly

    I should really be working

    Bradly Guatemala

    8/24/2010 1:09:24 AM #

    electronic cigarette

    It's a beautiful country, more people need to recognise that.

    electronic cigarette United Kingdom

    8/24/2010 11:24:00 AM #

    Ellsworth Colan

    What a comment!! Very informative also easy to understand. Looking for more such posts!! Do you have a myspace?
    I recommended it on digg. The only thing that it's missing is a bit of new design. However thank you for this blog.

    Ellsworth Colan United States

    8/24/2010 11:53:14 AM #

    Pozycjonowanie

    It's difficult (to be productive) when you don't get much work in practice, but it's a learned trait that I picked up last year, ... I feel like I'm  definitely a better quarterback than I was last year. I'll just keep trying to do the best I can in these next two preseason games and hopefully impress the Eagles or some other team.

    Pozycjonowanie United States

    8/24/2010 12:17:00 PM #

    Www.allegro.pl

    Lot's of people will agree with this article.

    Www.allegro.pl United States

    8/24/2010 3:57:40 PM #

    RomfordDentist

    enormous pack.

    RomfordDentist United States

    8/26/2010 11:46:14 AM #

    Hamster Cage

    I am definitely digging your blog! Just a simple question though. How long have you been writing & working on your site? Thank you Smile -Jessi

    Hamster Cage United States

    8/26/2010 12:53:13 PM #

    pozycjonowanie

    Sometimes, life gets so busy that we could not find time to say "Thank you" to someone, who inspired us. So Thank you" For this article Smile

    pozycjonowanie United States

    8/26/2010 9:18:41 PM #

    Download free PC games like Super Mario Bros. and Sonic at Dash.fm.

    Why didn?t I find this post earlier? Keep up the good work!  

    Regards

    James Willmort!

    Download free PC games like Super Mario Bros. and Sonic at Dash.fm. United States

    8/27/2010 1:38:53 AM #

    Electronic Cigarettes

    I would like to thank you for the efforts you have made in writing this article.

    Electronic Cigarettes United States

    8/27/2010 8:37:29 AM #

    NHL11 Controls

    I think your on to soemthing in your article IT Ramblings | Ever wondered how to write code against the BDC?. I will make sure to be reading your blog more.
    See you
    NHL11 Controls

    NHL11 Controls United States

    8/27/2010 12:38:55 PM #

    botkier bags

    Hello. I wanted to drop you a quick note to express my thanks. I've been following your blog for a week or so and have picked up a ton of good information as well as enjoyed the way you've setup your blog. I am attempting to run my own blog but I think it's far too general. I would like to focus more on more specific topics. Being all things to all people is not all that its cracked up to be. Many thanks.

    botkier bags United States

    8/27/2010 3:39:39 PM #

    Marcellus Ehrhart

    Very interesting blog. Alot of blogs I see now don't really offer anything that I'm interested in, but I'm most definately interested in this blog. Just thought I would pass that message on.

    Marcellus Ehrhart United States

    8/27/2010 5:04:53 PM #

    jordan shoes

    I'm diggin' the blog. Alot of interesting information.

    jordan shoes United States

    8/27/2010 10:13:40 PM #

    villas in orlando

    Dude.. I am not much into reading, but somehow I got to read lots of articles on your blog. Its amazing how interesting it is for me to visit you very often.

    Regards


    villas in orlando United States

    8/28/2010 5:35:11 PM #

    Lawerence Kintner

    do you have an rss feed? I want to add it to my reader but I can't find it...

    Lawerence Kintner United States

    8/28/2010 5:55:50 PM #

    Denver Mines

    are you using wp for this blog?

    Denver Mines United States

    8/29/2010 2:28:47 AM #

    Rhoda Marocco

    What a comment!! Very informative and easy to understand. Looking for more such posts!! Do you have a myspace?
    I recommended it on digg. The only thing that it's missing is a bit of color. Nevertheless thank you for this blog.

    Rhoda Marocco United States

    8/29/2010 5:52:10 AM #

    dating affiliate

    hey, with so many comments you should add an affiliate program to your blog and make some money...

    dating affiliate United States

    8/29/2010 1:26:46 PM #

    euro umrechner

    Thank you for your important  criticize, this was very nice reading and assisted me in my prime cast about some currency contrast during our time.

    euro umrechner United States

    8/29/2010 2:23:27 PM #

    trance

    Going to see Astrix Tonight. Its gonna be really good. There playing with Tiesto. Then next weekend ill be seeing Pete Tong. a night to remember nights ahead

    trance Ireland

    8/29/2010 2:29:36 PM #

    Callaway Golf

    Tiger Woods is now a Cheetah

    Callaway Golf United States

    8/29/2010 5:42:38 PM #

    Sol Follin

    Hello all, just letting you know if you need 2 order the newest gran turismo 5 game www.buygranturismo5.info is supplying the best prices. If you are looking to purchase go to www.buygranturismo5.info. Dont miss out on your chance 2 purchase this legend of a game on ps3.

    Sol Follin United States

    8/29/2010 6:40:31 PM #

    does quick extender pro work,

    I am having problem with the first link. It gives a 404 error?  

    Regards


    does quick extender pro work, United States

    8/29/2010 7:24:48 PM #

    star tattoos

    When I originally commented I clicked the "Notify me when new comments are added" checkbox and now each time a comment is added I get three emails with the same comment. Is there any way you can remove me from that service? Thanks!

    Regards


    star tattoos United States

    8/30/2010 8:50:28 AM #

    seduction affiliates

    you may make some money if you add an affiliate program to your blog...

    seduction affiliates United States

    8/30/2010 7:34:17 PM #

    air jordan 15

    This blog is one of a kind, that's for sure. It looks like it has got a lot of people interested.

    air jordan 15 United States

    8/31/2010 3:01:39 AM #

    Irving Yauger

    Intimately, the article is in reality the freshest on this valuable topic. I suit in with your conclusions and also can eagerly look forward to your coming updates. Simply saying thanks will certainly not simply just be enough, for the extraordinary lucidity in your writing. I will certainly ideal away grab your rss feed to stay abreast of any updates. De lightful work and much success in your business endeavors!

    Irving Yauger United States

    8/31/2010 3:24:38 AM #

    Archie Massingill

    are you using a custom template?

    Archie Massingill United States

    8/31/2010 4:15:28 AM #

    get girls

    great blog! I too have one ob dating and seduction tips for guys...

    get girls United States

    8/31/2010 8:05:56 AM #

    Hipolito @ web design shanghai

    This article was really helpful and interesting. Thank you for sharing this. Those commented before me could have given some interesting views.

    Hipolito @ web design shanghai Canada

    8/31/2010 10:36:37 AM #

    blonde joke

    nice blog! keep the good work!

    blonde joke United States

    8/31/2010 8:42:56 PM #

    refurbished garmin

    I think the information written in your post is actually excellent. I've been working on a preliminary research project regarding this topic and your article really helped with a number of concerns that I had. I am composing a research paper for college and I?m currently following several blogs for review.

    refurbished garmin United States

    8/31/2010 10:51:46 PM #

    gold overmantle mirror

    You really make it seem so uncomplicated with your presentation but I discover that topic to be really something that I think I would under no circumstances understand. It seems too complicated and incredibly broad for me. I am looking forward for your next post, I will try to get the hang up of it!

    gold overmantle mirror United States

    9/1/2010 12:45:08 AM #

    learn to type

    Great post! Looking for a typing tutor? Check out my website.

    learn to type United States

    9/1/2010 10:50:17 AM #

    ETF newsletters

    it will be great if you post more...

    ETF newsletters United States

    9/1/2010 2:20:23 PM #

    fitness training

    don't forgot to get out and exercise...

    fitness training United States

    9/1/2010 8:11:06 PM #

    learn to type

    I enjoyed reading your post! Learn to type faster with typing tutor online.

    learn to type United States

    9/2/2010 10:22:00 PM #

    Halina Mazzanti

    Considerably, the article is actually the freshest on this worthw hile topic. I harmonise with your conclusions and definitely will thirstily look forward to your coming updates. Saying thanks will not simply just be enough, for the extraordinary lucidity in your writing. I will certainly directly grab your rss feed to stay privy of any updates. Gratifying work and much success in your business dealings!

    Halina Mazzanti United States

    9/3/2010 2:14:01 AM #

    gift ideas

    try to change your template, there are some great ones...

    gift ideas United States

    9/3/2010 3:33:48 AM #

    xxx

    Cool blog you got here and thank you for the valuable info.

    xxx United States

    9/3/2010 6:38:33 AM #

    Ernesto Tyer

    That is exactly the variation concerning the key mainstream press and information sites. The posts are well created and not corrected by an editor was. That's the purpose why I love to research for blogs. The comments are generally quite unique, we could all use more fun reading. Please go on like this, it really is really worth.

    Ernesto Tyer United States

    9/3/2010 2:02:58 PM #

    Raffle Tickets

    When can I expect a new post?

    Raffle Tickets United States

    9/3/2010 3:05:51 PM #

    dating tips

    do you have a xml feed for your blog?

    dating tips United States

    9/3/2010 9:47:31 PM #

    mixed wrestling

    I have been reading your posts during my smoke break, and I must admit the entire article has been very useful and very well written. I thought I would let you know that for some reason this blog does not view well in IE 8. I wish Microsoft would quit upgrading stuff. I have a question for you. Do you mind swapping blog roll links? That would be really neat!

    mixed wrestling United States

    9/4/2010 12:07:10 AM #

    Pozycjonowanie

    I wanted to be the first to comment on this post and say that the points you have touched here are really interesting and inspiring. I think you'll get lots of approvals here. Great job !

    Pozycjonowanie United States

    Add comment


    (Will show your Gravatar icon)

      Country flag

    biuquote
    • Comment
    • Preview
    Loading



    Powered by BlogEngine.NET 1.6.1.0
    Theme by Mads Kristensen | Modified by Mooglegiant