BizTalk: Output String Content
Introduction
Recently, a few questions have come up in the forums on how to produce and send string content from an Orchestration. While sending something as simple as a string sound like a trivial operation, it’s not. For clarity, it’s not exactly complicated, it just requires pretty much the same process as any other type of output.
Why the confusion? Because when attempting to work with String content in an Orchestration, new BizTalk Developers quickly discover the System.String Message Type option when creating a new Message. While this does allow the Message to be used as a string within the Orchestration, there are downstream consequences as a result of SOAP support being so integral the original usage scenarios BizTalk was meant to address.
Because the Message (Part) is intended to be used as a SOAP Parameter of Type String, the output is serialized, correctly I must say, as <string>Your content.</string>. Other primitive Types would be serialized similarly. You can see this result in the StringMessage_%MessageID%.txt output from the sample project.
While correct, this is not what we expect. Nevertheless, creating string output from an Orchestration is relatively easy. This article describes two already well knows ways to create plain text (string) content in an Orchestration.
Note: I am aware of the ability to create a custom Formatter and the sample that changes the serialization behavior of the Orchestration String Type. However, I view that as solving the wrong problem since a ‘string’ output is never the actual goal.
Download
The sample solution can be downloaded from the MSDN Gallery at: https://code.msdn.microsoft.com/BizTalk-Output-String-dec5d852
String As A Flat File
Most of us have been producing string content from our BizTalk apps since the very beginning in the form of Flat Files. In fact, when this situation does come up, my recommendation is to simply treat the string as a Flat File message.
Unlike other Articles, I’m not going to detail the process of creating the Flat File, there are many other great resources for that. The Sample application uses a Flat File Schema to produce string output.
There is just a few points worth specifying:
- The Flat File Schema can be just one line. Headers, Footers, Repeating Elements are not at all required.
- You can produce any number of lines by setting the single element to maxOccurs=unbounded and using CR/LF as the delimiter.
- Using a Flat File Schema gives you access to all other features in BizTalk such as Maps, Distinguished Fields, Property Demotion and anything else we normally do.
String As XSLT
For simple string content, a Flat File is the best choice in the vast majority of cases, but if you string is a bit more than just a string, such as HTML, or is composed of many incoming elements, then producing the string from XSLT is the next option.
Like using a Flat File Schema, the XSLT option is mostly basic BizTalk. The only two concepts likely to be new are:
- Custom XSLT Path property of the Map design surface.
- Microsoft.XLANGs.BaseTypes.Any Message Type.
Even when using a custom XLST Template, we have to create Map file, .btm, and set the Source and Destination Schemas.
The Map Source is the input Message Type. The Destination Schema is the Microsoft.XLANGs.BaseTypes.Any Type. To access this Schema, you must have a Reference to Microsoft.XLANGs.BaseTypes.dll.
<?xml version="1.0" encoding="UTF-16"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt"
xmlns:var="http://schemas.microsoft.com/BizTalk/2003/var"
exclude-result-prefixes="msxsl var" version="1.0" >
<xsl:output omit-xml-declaration="yes" method="xml" version="1.0" />
<xsl:template match="/">
<xsl:apply-templates select="/InputMessage" />
</xsl:template>
<xsl:template match="/InputMessage">
<xsl:value-of select="HeaderInfo/text()" />
<xsl:text> </xsl:text>
<xsl:value-of select="DataField/text()" />
<xsl:text> </xsl:text>
<xsl:text>Footer content from xsl.</xsl:text>
</xsl:template>
</xsl:stylesheet>
For the XSL, we’ll use a basic transform that emits only the text of the incoming message with no XML or other markup surrounding it. While BizTalk is built around the XML model, there are actually very few places where XML is required. The output of a Transform can be any content type in both an Orchestration and Port.
Finally, we set the XSLT file in the Custom XSLT Path property of the Map Design Surface. One thing to point here is that that this property seems to not handle relative paths too well. To successfully build and deploy, you may need to update your environment.
The Sample Orchestration
The sample Orchestration consists only of a Parallel Shape and Message Assignment and Transform shapes to generate the three types of output.
Conclusion
Creating string output from BizTalk apps is an easy and straightforward process when considered like any other Message output.
Happy BizTalking!
See Also
Another important place to find a huge amount of BizTalk related articles is the TechNet Wiki itself. The best entry point is BizTalk Server Resources on the TechNet Wiki