Friday, May 28, 2010

Exporting a Specific Party from BizTalk Server 2009

Here is the small piece of code that can be use to Export specific party binding from BizTalk Server Management database. This works on BizTalk Server 2009 and should probably work on BizTalk Server 2006 R2

Here is the code


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using Microsoft.Win32;
using System.Globalization;
using Microsoft.BizTalk.ExplorerOM;
using System.Xml;

namespace PartyExport
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] parties = { "CompanyA", "CompanyD" };
            ExportSelectedPartiesToFile(parties, @"C:\Bindings.xml");
        }

        public static void ExportSelectedPartiesToFile(string[] parties, string exportPath)
        {
            
            BtsCatalogExplorer catalog = new BtsCatalogExplorer();
            catalog.ConnectionString = BtsManagementDatabase.ConnectionString;

            Microsoft.BizTalk.Deployment.Binding.BindingInfo bindingInfo = new Microsoft.BizTalk.Deployment.Binding.BindingInfo();

            using (SqlConnection sqlConnection = new SqlConnection(BtsManagementDatabase.ConnectionString))
            {
                Microsoft.BizTalk.Deployment.Binding.BindingParameters bindingParameters = new Microsoft.BizTalk.Deployment.Binding.BindingParameters(new Version());

                bindingInfo.PartyCollection = new Microsoft.BizTalk.Deployment.Binding.PartyCollection();
                Microsoft.BizTalk.Deployment.EdiParties ediParties = new Microsoft.BizTalk.Deployment.EdiParties();

                List<Party> lookupCollection = (from catalogParty in catalog.Parties.Cast<Party>()
                                                join lookupParty in parties on  catalogParty.Name equals lookupParty
                                                select catalogParty).ToList();

                foreach (Party party in lookupCollection)
                {
                    Microsoft.BizTalk.Deployment.IEdiParty ediParty = ediParties.GetParty(party.Name);
                    Microsoft.BizTalk.Deployment.Binding.Party bindingParty = new Microsoft.BizTalk.Deployment.Binding.Party();

                    bindingParty.Name = party.Name;
                    bindingParty.Aliases = new Microsoft.BizTalk.Deployment.Binding.PartyAliasCollection();

                    foreach (PartyAlias partyAlias in party.Aliases)
                    {
                        Microsoft.BizTalk.Deployment.Binding.PartyAlias bindingPartyAlias = new Microsoft.BizTalk.Deployment.Binding.PartyAlias();
                        bindingPartyAlias.IsAutoCreated = partyAlias.IsAutoCreated;
                        bindingPartyAlias.Name = partyAlias.Name;
                        bindingPartyAlias.Qualifier = partyAlias.Qualifier;
                        bindingPartyAlias.Value = partyAlias.Value;
                        bindingParty.Aliases.Add(bindingPartyAlias);
                    }
                    bindingParty.SendPorts = new Microsoft.BizTalk.Deployment.Binding.SendPortRefCollection();

                    foreach (SendPort sendPort in party.SendPorts)
                    {
                        Microsoft.BizTalk.Deployment.Binding.SendPortRef sendPortRef = new Microsoft.BizTalk.Deployment.Binding.SendPortRef();
                        sendPortRef.Name = sendPort.Name;
                        bindingParty.SendPorts.Add(sendPortRef);
                    }

                    if (party.SignatureCert != null)
                    {
                        bindingParty.SignatureCert = new Microsoft.BizTalk.Deployment.Binding.CertificateInfo();
                        bindingParty.SignatureCert.LongName = party.SignatureCert.LongName;
                        bindingParty.SignatureCert.ShortName = party.SignatureCert.ShortName;
                        bindingParty.SignatureCert.ThumbPrint = party.SignatureCert.ThumbPrint;
                    }
                    bindingParty.CustomData = party.CustomData;
                    if (ediParty != null)
                    {
                        string xml = ediParty.Xml;
                        bindingParty.EdiData = new XmlDocument();
                        bindingParty.EdiData.PreserveWhitespace = true;
                        bindingParty.EdiData.LoadXml(xml);
                    }

                    bindingInfo.PartyCollection.Add(bindingParty);
                }
                bindingInfo.SaveXml(exportPath);
            }
        }

        internal class BtsManagementDatabase
        {
            private static string serverName;
            private static string databaseName;

            static BtsManagementDatabase()
            {
                RegistryKey key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\BizTalk Server\3.0\Administration");
                serverName = (string)key.GetValue("MgmtDBServer");
                databaseName = (string)key.GetValue("MgmtDBName");
                key.Close();
            }

            public static string ServerName
            {
                get { return serverName; }
            }
            public static string DatabaseName
            {
                get { return databaseName; }
            }

            public static string ConnectionString
            {
                get
                {
                    string format = "Server={0};Database={1};Integrated Security=SSPI";
                    return string.Format(CultureInfo.CurrentCulture, format, new object[] { ServerName, DatabaseName });
                }

            }
        }
    }

          
}