Wednesday, January 22, 2014

Programmatically (Dynamically) change the properties of a Receive Location


We can update the properties of a File adapter Receive Location using the Microsoft.BizTalk.ExplorerOM (from {BizTalkInstallation}\DeveloperTools) and I refereed useful article from MSDN for my development.

   We have a requirement to programmatically(dynamically) change the File Mask property of the File receive location.
   The following code only deals with a File Mask property that can be changed in the File receive location.
It accepts few things as input, we accept the port name, which receive location you want to change, file mask and it will change the file mask property in the receive location.


Add reference "Microsoft.BizTalk.ExplorerOM" component to your application.

I have written below as per the MSDN article and this trick didn't worked for me.

BtsCatalogExplorer bceExplorer = new BtsCatalogExplorer();
//Edit the following connection string to point to the correct database and server
bceExplorer.ConnectionString = "Integrated Security=SSPI;database=BizTalkMgmtDb;server=localhost";

public bool UpdateReceiveLocationFileMask(string receivePortName, string receiveLocationName, string fileMask)
{
            bool returnValue = false;
            string transportData = string.Empty;
            XmlDocument doc = null;
            XmlNode root = null;
            XmlNode fileMaskNode = null;
            try
            {
                if (!string.IsNullOrEmpty(fileMask))
                {
                    ReceivePort receivePort = bceExplorer.ReceivePorts[receivePortName];
                    ReceiveLocation receiveLocation = receivePort.ReceiveLocations.Cast<ReceiveLocation>().Where(item => item.Name == receiveLocationName).FirstOrDefault();

                    if (receiveLocation != null)
                    {                        
                        transportData = receiveLocation.TransportTypeData;
                        doc = new XmlDocument();
                        doc.LoadXml(transportData);
                        root = doc.DocumentElement;
                        fileMaskNode = root.SelectSingleNode("FileMask");

                        if (fileMaskNode != null)
                        {
                            fileMaskNode.InnerText = fileMask;
                            receiveLocation.TransportTypeData = doc.OuterXml;
                            bceExplorer.SaveChanges();
                            returnValue = true;
                        }
                        bceExplorer.SaveChanges();
                        returnValue = true;
                    }
                }
            }
            catch (Exception e)
            {
                System.Diagnostics.EventLog.WriteEntry("UpdateReceiveLocationFileMask", e.Message);
                bceExplorer.DiscardChanges();
                returnValue = false;
            }

            return returnValue;


The above code didn't worked, i have used Address property of the receive location and this trick worked for me.

public bool UpdateReceiveLocationFileMask(string receivePortName, string receiveLocationName, string fileMask)
{
            bool returnValue = false;
            string transportData = string.Empty;
            XmlDocument doc = null;
            XmlNode root = null;
            XmlNode fileMaskNode = null;
            try
            {
                if (!string.IsNullOrEmpty(fileMask))
                {
                    ReceivePort receivePort = bceExplorer.ReceivePorts[receivePortName];
                    ReceiveLocation receiveLocation = receivePort.ReceiveLocations.Cast<ReceiveLocation>().Where(item => item.Name == receiveLocationName).FirstOrDefault();

                    if (receiveLocation != null)
                    {
                        receiveLocation.Address = Path.Combine(Path.GetDirectoryName(receiveLocation.Address), fileMask);                        
                        bceExplorer.SaveChanges();
                        returnValue = true;
                    }
                }
            }
            catch (Exception e)
            {
                System.Diagnostics.EventLog.WriteEntry("UpdateReceiveLocationFileMask", e.Message);
                bceExplorer.DiscardChanges();
                returnValue = false;
            }

            return returnValue;
}

No comments:

Post a Comment