Search This Blog

Thursday 8 March 2018

Promote the received FileName and FileExtension in BizTalk Server Receive location using the Custom Pipeline Component

Hi All,

Hope everyone is doing great. It has been a hectic couple of days with my personal life. I am slowly but surely coming back to normal.

Let's get into the real stuff!!!

It is very common requirement that, BizTalk moves files from FileLocation to another location or one of the following Transport protocols

FILE
FTP
SQL
SMTP
etc...

It is also common that, some systems generate a lot of different types of files(it can be different file formats, file with different content and different fileNames) and dump them all into one folder location.

let's say, we come across the requirement to route the files based on the file name and/or the file Extension.

To achieve the above requirement, we can create 1 receive port per file and change file mask on the receive location accordingly such that routing can be achieved. The problem with this approach is that if there are 10's and 100's of files that needed to be routed to the different destinations based on the file name and/or the file Extension the above solution is not the ideal solution for the problem as it requires 10's and 100's of receive ports and locations created.

Instead, I have written a simple receive pipeline component which is used in a custom pipeline. This pipeline component has the very basic code; it extracts the file name(ReceivedFileName), file extension(ReceivedFileExtension) from the file path and promotes it to the Context.

The pipeline is selected on the receive location as shown below.

As the ReceivedFileName, ReceivedFileExtension is promoted to Context they are available on the send Port filters; this way even if you have 100's of files that need to be routed,1 receive port with 1 receive location solves the problem.

The complete class code is below. You have to generate new GUID's for classID, and PipelineComponent and change it accordingly.

//******========================================================*****

    [ComponentCategory(CategoryTypes.CATID_PipelineComponent)]
    [System.Runtime.InteropServices.Guid("89X14D30-191F-4FYC-9EED-057B84144588")]
    [ComponentCategory(CategoryTypes.CATID_Any)]
    public class PromoteFileNameAndExtension
    {
        #region IPersistPropertyBag members
        /// <summary>
        /// Gets class ID of component for usage from unmanaged code.
        /// </summary>
        /// <param name="classid">
        /// Class ID of the component
        /// </param>
        public void GetClassID(out System.Guid classid)
        {
            classid = new System.Guid("1F0CGFB2-2729-43A7-A080-17GH29E9HH85");
        }

        /// <summary>
        /// not implemented
        /// </summary>
        public void InitNew()
        {
        }

        #endregion

        #region IBaseComponent members
        /// <summary>
        /// Name of the component
        /// </summary>
        [Browsable(false)]
        public string Name
        {
            get
            {
                return "PromoteFileNameAndExtension";
            }
        }

        /// <summary>
        /// Version of the component
        /// </summary>
        [Browsable(false)]
        public string Version
        {
            get
            {
                return "1.0";
            }
        }

        /// <summary>
        /// Description of the component
        /// </summary>
        [Browsable(false)]
        public string Description
        {
            get
            {
                return "Extracts the exact FileName, FileExtension and promotes to the context.";
            }
        }
        #endregion

        #region IComponentUI members
        /// <summary>
        /// Component icon to use in BizTalk Editor
        /// </summary>
        [Browsable(false)]
        public IntPtr Icon
        {
            get
            {
                return new System.IntPtr();
            }
        }

        /// <summary>
        /// The Validate method is called by the BizTalk Editor during the build
        /// of a BizTalk project.
        /// </summary>
        /// <param name="obj">An Object containing the configuration properties.</param>
        /// <returns>The IEnumerator enables the caller to enumerate through a collection of strings containing error messages. These error messages appear as compiler error messages. To report successful property validation, the method should return an empty enumerator.</returns>
        public System.Collections.IEnumerator Validate(object obj)
        {
            return null;
        }
        #endregion

        #region IComponent members
        /// <summary>
        /// Implements IComponent.Execute method. This Method extracts the fileName from the complete filePath and promote to ReceivedFileName property in http://abcCompany.Ei.Utils.Schemas.Utils_PropertySchema namespace
        /// </summary>
        /// <param name="pc">Pipeline context</param>
        /// <param name="inmsg">Input message</param>
        /// <returns>Original input message</returns>
        /// <remarks>
        /// IComponent.Execute method is used to initiate
        /// the processing of the message in this pipeline component.
        /// </remarks>
        public Microsoft.BizTalk.Message.Interop.IBaseMessage Execute(Microsoft.BizTalk.Component.Interop.IPipelineContext pc, Microsoft.BizTalk.Message.Interop.IBaseMessage inmsg)
        {
            try
            {
                //Read the file Name from the Context using the Context Property "ReceivedFileName". This property contains the full path of the file
                string fileName = (string)inmsg.Context.Read("ReceivedFileName", "http://schemas.microsoft.com/BizTalk/2003/file-properties");
                if (!string.IsNullOrEmpty(fileName))
                {
                    //Promote the fileName to the Context. Before this you need to create the property Schema with "ReceivedFileName", "ReceivedFileExtension" elements and deployed
                    inmsg.Context.Promote("ReceivedFileName", "http://abcCompany.Ei.Schemas.CustomFiles_PropertySchema", System.IO.Path.GetFileName(fileName));
                    inmsg.Context.Promote("ReceivedFileExtension", "http://abcCompany.Ei.Schemas.CustomFiles_PropertySchema", System.IO.Path.GetExtension(fileName).Replace(".", ""));
                }
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine("\n PromoteFileNameAndExtension Exception while writing promoting. Details : " + ex.Message);
                throw ex;
            }

            return inmsg;
        }

        #endregion
    }

//******========================================================*****

Please comment below if you have any queries or don't understand any portion of the code.

Have awesome day guys and girls.

Hope I have helped someone who is new to BizTalk Custom Pipeline Component development.

Sending Email to Multiple recipients using PowerShell

Hi All,  Below is the working code to Send emails to Multiple recipients using PowerShell. [STRING]$PSEmailServer = "YourSMTPServerIPOr...