Monday, July 19, 2010

EDI Batching Automation

If you are trying to automate EDI Properties in BizTalk and struggling to find out how can you find out the status of a batch whether it's active. Here is the piece of code you may find quite useful.



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;
using Microsoft.BizTalk.Edi.PartnerAgreementManager;

namespace PartyExport
{
    class Program
    {
        static void Main(string[] args)
        {
            ShowBatchesStatus();       
        }

      
        public static void ShowBatchesStatus()
        {
         
            using (BtsCatalogExplorer explorer = new BtsCatalogExplorer())
            {
                explorer.ConnectionString = BtsManagementDatabase.ConnectionString;
                foreach (Party party in explorer.Parties)
                {
                    Partner partner = new Partner(party.Name);
                    foreach (IPartnerBatch batch in partner.Batches.Batches)
                    {
                        Console.WriteLine(string.Format("Batch {0} Status is:{1}",batch.Name,batch.BatchingActivated?"Active":"Inactive"));     
                    }
                }
            }
    }

        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 });
                }

            }
        }
    }

          
}