Geeks With Blogs

Bill Osuch - Random geek notes

In the last post I showed how to create the skeleton of a pipeline components, add it to the toolbox, integrate it into a BizTalk app and deploy & test it. This time I’ll go over working with the XML, reading and writing message context properties, and reading and writing custom component properties.

To get the xml portion of the message, you can get a Stream from IBaseMessage.Body.Part.GetOriginalDataStream() and load it into an XmlDocument. In our case we’re manipulating the structure of an 837 HIPAA claim, so I’m not going to going into all the details, but here’s a method to write the xml out to a file:

protected void WriteXmlToFile(IBaseMessage message, string fileName)
{
    IBaseMessagePart bodyPart = message.BodyPart;
    Stream originalStream = bodyPart.GetOriginalDataStream();
    XmlDocument xdoc = new XmlDocument();
    xdoc.Load(originalStream);
    xdoc.Save(fileName);
}

You can read the various message context properties (see this link for a list) by querying IBaseMessage.Context, like this:

protected void ReadContextProperties(IBaseMessage message)
{
    string inboundTransportType = (string)message.Context.Read("InboundTransportType", "http://schemas.microsoft.com/BizTalk/2003/system-properties");
    string receivedFileName = (string)message.Context.Read("ReceivedFileName", "http://schemas.microsoft.com/BizTalk/2003/file-properties");
    string interchangeID = (string)message.Context.Read("InterchangeID", "http://schemas.microsoft.com/BizTalk/2003/system-properties");
}

You can write (update) context properties with IBaseMessage.Context.Write:

protected void WriteContextPropertyValue(IBaseMessage message, string propertyName, string propertyNamespace, string propertyValue)
{
    message.Context.Write(propertyName, propertyNamespace, propertyValue);
}

Reading custom properties is done in the Load() method that was automatically generated by the Pipeline Component Wizard. In the last post I created a property called “TestProperty”, and this is the code that was created:

public virtual void Load(Microsoft.BizTalk.Component.Interop.IPropertyBag pb, int errlog)
{
    object val = null;
    val = this.ReadPropertyBag(pb, "TestProperty");
    if ((val != null))
    {
        this._TestProperty = ((string)(val));
    }
}

So, tying this all together in the Execute() method would look something like this:

public Microsoft.BizTalk.Message.Interop.IBaseMessage Execute(Microsoft.BizTalk.Component.Interop.IPipelineContext pc, Microsoft.BizTalk.Message.Interop.IBaseMessage inmsg)
{
    WriteXmlToFile(inmsg, _TestProperty);

    ReadContextProperties(inmsg);

    WriteContextPropertyValue(inmsg, "ReceivedFileName", "http://schemas.microsoft.com/BizTalk/2003/file-properties", "NewFileName");

   return inmsg;
}

Technorati Tags:

Posted on Wednesday, January 25, 2012 3:17 PM BizTalk | Back to top


Comments on this post: Creating a custom BizTalk 2010 pipeline component–Part 2

No comments posted yet.
Your comment:
 (will show your gravatar)


Copyright © Bill Osuch | Powered by: GeeksWithBlogs.net