Automate turning a BizTalk Receive Location and Send Port on or off through code

I found a solution. It appears that the Microsoft.BizTalk.ExplorerOM.dll is what I wanted. Here's is some excerpts of code I used in a Windows console app to enable a Receive Location remotely:

Below code for Console Application (Program.cs)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.BizTalk.ExplorerOM;
using System.Xml;
using System.IO;
using System.Configuration;

namespace CustomBizTalkAPI
{
    class Program
    {
        static void Main(string[] args)
        {
            XmlDocument doc = new XmlDocument();
            XmlNodeList rlXmlNode;
            XmlNodeList spXmlNode;
            int i = 0, j =0;

            doc.Load(ConfigurationManager.AppSettings["xmlPath"]);

            CustomBizTalkAPI.Class2 myClass = new Class2();

            rlXmlNode = doc.GetElementsByTagName("receivelocation");
            myClass.GetReceiveLocationFromBizTalk();

            for (i = 0; i <= rlXmlNode.Count - 1; i++)
            {
                string rl = rlXmlNode[i].Attributes["rlname"].InnerText.Trim();
                bool rlstatus = Convert.ToBoolean(rlXmlNode[i].Attributes["enabled"].InnerText.Trim());

                myClass.TurnOffReceiveLocation(rl, rlstatus);
            }

            spXmlNode = doc.GetElementsByTagName("sendport");
            myClass.GetSendLocationFromBizTalk();

            for (j = 0; j <= spXmlNode.Count - 1; j++)
            {
                string sp = spXmlNode[j].Attributes["spname"].InnerText.Trim();
                string spstatus = spXmlNode[j].Attributes["enabled"].InnerText.Trim();

                myClass.TurnOffSendPort(sp, spstatus);
            }
    }
}

Below code for Class file in Same Application (Class2.cs)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.BizTalk.ExplorerOM;
using System.Configuration;

namespace CustomBizTalkAPI
{
    class Class2
    {
        private BtsCatalogExplorer BizTalkCatalog { get; set; }
      
        Dictionary<string, ReceiveLocation> receiveLocations = new Dictionary<string, ReceiveLocation>();
        Dictionary<string, SendPort> sendLocations = new Dictionary<string, SendPort>();

        private void InitializeBizTalkCatalogExplorer()
        {
            // Connect to the local BizTalk Management database
            BizTalkCatalog = new BtsCatalogExplorer();
            var connectionString = ConfigurationManager.ConnectionStrings["myconnString"].ConnectionString;
            BizTalkCatalog.ConnectionString = connectionString;
        }

        public IDictionary<string,ReceiveLocation> GetReceiveLocationFromBizTalk()
        {
            InitializeBizTalkCatalogExplorer();

            ReceivePortCollection receivePorts = BizTalkCatalog.ReceivePorts;
            foreach (ReceivePort port in receivePorts)
            {
                foreach (ReceiveLocation location in port.ReceiveLocations)
                {
                    receiveLocations.Add(location.Name, location);
                }
            }
            return receiveLocations;
            //throw new ApplicationException("The following receive location could not be found in the BizTalk Database: ");
        }

        public void TurnOffReceiveLocation(string vendorName, bool status)
        {
            ReceiveLocation location = receiveLocations[vendorName];
            if (location.Enable != status)
            {
                location.Enable = status;
                BizTalkCatalog.SaveChanges();
            }
        }

        public IDictionary<string, SendPort> GetSendLocationFromBizTalk()
        {
            InitializeBizTalkCatalogExplorer();

            SendPortCollection sendPorts = BizTalkCatalog.SendPorts;
            foreach (SendPort port in sendPorts)
            {
                sendLocations.Add(port.Name, port);
            }
            return sendLocations;
            //throw new ApplicationException("The following receive location could not be found in the BizTalk Database: " + locationName);
        }

        public void TurnOffSendPort(string vendorName, string status)
        {
            SendPort location = sendLocations[vendorName];
            switch (status)
            {
                case "true":
                    if (location.Status != PortStatus.Started)
                    {
                        location.Status = PortStatus.Started;
                        BizTalkCatalog.SaveChanges();
                    }
                    break;
                case "false":
                    if (location.Status != PortStatus.Stopped)
                    {
                        location.Status = PortStatus.Stopped;
                        BizTalkCatalog.SaveChanges();
                    }
                    break;
                case "na":
                    if (location.Status != PortStatus.Bound)
                    {
                        location.Status = PortStatus.Bound;
                        BizTalkCatalog.SaveChanges();
                    }
                    break;
            }
        }
    }
}

Below code for XML file in Same Application (XMLFile1.xml) which i am using as configuration file.
<?xml version="1.0" encoding="utf-8" ?>
<root>
    <!-- Receive Location Start Here -->
    <receivelocation interfacename="yyyyy" rlname="zzzzz" enabled="true"/>
    <receivelocation interfacename="yyyyy" rlname="zzzzz" enabled="false"/>
    <!-- Receive Location End Here -->
  
    <!-- Send Port Start Here -->
    <sendport interfacename="yyyyy" spname="zzzzz" enabled="true"/>
    <sendport interfacename="yyyyy" spname="zzzzz" enabled="false"/>
    <sendport interfacename="yyyyy" spname="zzzzz" enabled="na"/>
    <!-- Send Port End Here -->

</root>

Below code for config file for connection string and xml path in Same Application (app.config)
<?xml version="1.0"?>
<configuration>
  <appSettings>
    <add key="xmlPath" value="D:\CustomBizTalkAPI\XMLFile1.xml"/>
  </appSettings>
  <connectionStrings>
    <add name="myconnString" connectionString="server=xxxxxxxxx;database=BizTalkMgmtDb;integrated security=true"/>
  </connectionStrings>
</configuration>

Please don't forgot to comment you experience with code. Cheers!!!

Post a Comment

0 Comments