Hi folks, recently I've been asked by several students on how to create Parties, Agreements, Profiles etc. via code in BizTalk 2010.

I played with this along time ago while at Redmond as BizTalk 2010 was in the process of being released.

So I've just rolled up my sleeves and provided a quick demo for you - the demo shows:
  1. How to enumerate and get at each of your TPM Partners.
  2. How to create Partners + Profiles within BizTalk 2010.
Note: I've only tried this on BizTalk 2010 (& needless to say I'm claiming 'works on my machine' :))

What we're talking about in BizTalk
This section here....




Show me the code....
Well the magic is found in this DLL -
C:\Program Files (x86)\Microsoft BizTalk Server 2010\Developer Tools\Microsoft.BizTalk.B2B.PartnerManagement.dll

- create a VS.NET 2010 app (for this demo I created a console app)
- we make a reference to the above DLL (we also need to reference system.data.entity)
- set a connectionstring to our BizTalk Management DB, mine is BizTalkDB (as I rolled all the BizTalk DBs into one - for dev)
- start enumerating.

C# Looks like this-

static void Main(string[]
args) { //enumerate
all the TPM Profiles in BizTalk var builder = new SqlConnectionStringBuilder("DATA
SOURCE=localhost;Initial Catalog=BizTalkDB;" + "Integrated
Security=SSPI;MultipleActiveResultSets=True"); var tmpCtx = TpmContext.Create(builder);
Console.WriteLine("Connected
to BizTalk Global Parties"); var partners = tmpCtx.Partners;
Console.WriteLine("Number
of Parters:{0}", partners.Count()); Console.WriteLine("------------"); foreach (var
ptr in partners)
{ var profiles = ptr.GetBusinessProfiles();
Console.WriteLine("{0}
Business Profiles:{1}", ptr.Name, profiles.Count); foreach (var
profile in profiles)
{ Console.WriteLine("\tProfile:{0}",
profile.Name); } } tmpCtx.Dispose(); if (bCreateProfile)
{ createProfile("Breeze
Partner #"); } Console.WriteLine("Finished");
Console.ReadLine(); }

Point to Note: in the connection string I set 'MARS=true' just so we can enumerate several collections at once through the one context. When updating or saving new, partners and/or profiles I get errors and can't save through a MARs enabled connection. (love to hear if you have different luck)

Creating a Partner + Profile
//
need to do this through a single threaded connection - no MARS private static void createProfile(string partnerName)
{ partnerName += DateTime.Now.ToString("yyyyMMdd-hhmmss") + (new Random().Next(0,
65535)); Console.WriteLine("Writing
a new Profile for {0}", partnerName); var builder = new SqlConnectionStringBuilder("DATA
SOURCE=localhost;Initial Catalog=BizTalkDB;Integrated Security=SSPI"); var
tmpCtx = TpmContext.Create(builder);
var ptr = tmpCtx.CreatePartner(partnerName);
var pname = "Breeze
Profile-#" + DateTime.Now.ToString("yyyyMMdd-hhmmss") + (new Random().Next(0,
65535)); var bp = ptr.CreateBusinessProfile(pname);
bp.Description = "Created
from Code"; var pcol = new AS2ProtocolSettings("BreezeProtocolSettings");
bp.AddProtocolSettings(pcol); tmpCtx.SaveChanges(); tmpCtx.Dispose(); }
And that's pretty much all there is to it folks, have a play around with the APIs for yourself - all undocumented of course.

Here's the Console App Solution I use (built for very demo purposes)

TPM API Demo.zip (32 KB)

Enjoy Mick!

Blog Post by: Mick Badran