Wednesday 12 June 2013

Create flat File Schema using Flat File schema Wizard and put validation for extra fields



This Article describe you how to create flat File Schema using Flat File schema Wizard.
Consider the Sample:

Id|Name|AddressLine1|AddressLine2|City|State
1|Alice Smith|123 Maple Street|Mill Valley|CA|US
2|Robert Smith|8 Oak Avenue|Old Town|PA|US

Right Click on project -> Add -> New Item -> Flat File schema Wizard.
Name your Schema as InputSchema.xsd.



















Select Your Input Instance File, Name your Root Record.
















Select all portions which are used to define the records.


















Select the option for which records are separated .Since records are separated by {CR}{LF} , then select combo box By delimiter symbol .


















Select {CR}{LF} as child delimiter.



















Define Element, Element Type for Records.
Then Select Delimiter for records (|).



















Define Elements Name and Data Type for each element.
Right Click on Schema -> Validate Instance.
It generates xml like this:


<Input xmlns="http://Biztalk.ProcessDelimiterFlatFile.InputSchema">
<Header xmlns="">
  <Id>Id</Id>
  <Name>Name</Name>
  <AddLine1>AddressLine1</AddLine1>
  <AddLine2>AddressLine2</AddLine2>
  <City>City</City>
  <State>State</State>
  </Header>
 <Details xmlns="">
  <Id>1</Id>
  <Name>Alice Smith</Name>
  <AddLine1>123 Maple Street</AddLine1>
  <AddLine2>Mill Valley</AddLine2>
  <City>CA</City>
  <State>US</State>
  </Details>
<Details xmlns="">
  <Id>1</Id>
  <Name>Robert Smith</Name>
  <AddLine1>8 Oak Avenue</AddLine1>
  <AddLine2>Old Town</AddLine2>
  <City>PA</City>
  <State>US</State>
  </Details>
  </Input>


Now Change Instance file by add one extra field
Id|Name|AddressLine1|AddressLine2|City|State
1|Alice Smith|123 Maple Street|Mill Valley|CA|US|US
1|Robert Smith|8 Oak Avenue|Old Town|PA|US

Then Vaidate instance , it will generate xml like this >





<Input xmlns="http://Biztalk.ProcessDelimiterFlatFile.InputSchema">
<Header xmlns="">
  <Id>Id</Id>
  <Name>Name</Name>
  <AddLine1>AddressLine1</AddLine1>
  <AddLine2>AddressLine2</AddLine2>
  <City>City</City>
  <State>State</State>
  </Header>
<Details xmlns="">
  <Id>1</Id>
  <Name>Alice Smith</Name>
  <AddLine1>123 Maple Street</AddLine1>
  <AddLine2>Mill Valley</AddLine2>
  <City>CA</City>
  <State>US|US</State>
  </Details>
<Details xmlns="">
  <Id>1</Id>
  <Name>Robert Smith</Name>
  <AddLine1>8 Oak Avenue</AddLine1>
  <AddLine2>Old Town</AddLine2>
  <City>PA</City>
  <State>US</State>
  </Details>
  </Input>

So this flat file schema is also validating incorrect flat file, to apply such kind of validation
We need to add Patterns at field level.




















Set Pattern as ^ [^|]*.
Now schema will not validate such instance and give error.

Friday 24 May 2013

Create XLANG Message using C# component.

Code to create XLANG Message inside Biztalk Orchestration

 [Serializable]
    public class MyStreamFactory : Microsoft.XLANGs.BaseTypes.IStreamFactory
    {
        private string m_mystringdata;

        public MyStreamFactory(string stringdata)
        {
            m_mystringdata = stringdata;
        }


        public System.IO.Stream CreateStream()
        {
            return new System.IO.MemoryStream(System.Text.ASCIIEncoding.ASCII.GetBytes(m_mystringdata));
        }
    }

    [Serializable]
    public class MyMessageCreator
    {
        public void CreateMyMessage(Microsoft.XLANGs.BaseTypes.XLANGMessage mydestmsg)
        {
            mydestmsg[0].LoadFrom(new MyStreamFactory("this is my data to create message from"));
        }
    }

This code can be used to create non xml message (Like PDF, Docx ) inside orchestration.

Thursday 23 May 2013

GNUPG Encoder - Biztalk custom pipeline component



This Article helps in creating Custom pipeline component for PGP encryption using GNUPG software.

Download and install GNUPG on machine.

Generate keys for Encryption using command 






























gpg --gen-key


Receipientid : Test@company.com

Passphase : Test@123

Remember Email Id and Passphase as these are required during encryption.

Code For creating Pipeline component is : 


public Microsoft.BizTalk.Message.Interop.IBaseMessage Execute(IPipelineContext pContext, Microsoft.BizTalk.Message.Interop.IBaseMessage pInMsg)
        {
            IBaseMessage outMsg = pInMsg;
            msgPart = pInMsg.BodyPart;
            Stream originalStream = pInMsg.BodyPart.GetOriginalDataStream();
            try
            {
                if (msgPart != null)
                {
                    if (originalStream != null)
                    {
                        byte[] arrByte = new byte[0];
                        originalStream = (new MemoryStream(arrByte));

                        // Code Added Here
                        GnuPG gpg = new GnuPG();
                        gpg.BinaryPath = Path.GetDirectoryName(textAppPath);
                        gpg.Recipient = textReceipientId;
                        //  Perform encryption

                        // convert string to stream
                        MemoryStream encryptedStream = new MemoryStream();
                        gpg.Encrypt(originalStream, encryptedStream);
                        encryptedStream.Seek(0, SeekOrigin.Begin);
                        msgPart.Data = encryptedStream;
                        outMsg = pInMsg;
                        outMsg.BodyPart.Data = encryptedStream;
                        //
                    }
                }
            }
            catch (Exception)
            {
                throw;
            }
            return outMsg;
        }

After creating pipeline component , you need to install dll in GAC and copy dll at location 

“C:\Program Files\Microsoft BizTalk Server 2010\Pipeline Components”..

Now creates send pipeline and add this component at encode stage.






















Deploy and configure solution using admin console.
 












Download sorce code from MSDN.