BizTalk EDI Batching Custom Naming

So there are a few companies out there that want to send out a file with a standard naming convention.

I thought, this will be EASY, you simply assign the file name to the transactions, and the Batching orchestration will re attach the context properties after its batching process completes and it sends it out so I can use %SourceFileName% in the Send Port.

WRONG, I get a file named %SourceFileName% in the folder.

To get a custom filename you have to attach the filename into the payload of the message somewhere. In my case I was needing to put the batch run id in the filename.

How I did it was put it in the ST03 element (since it gets rewritten in the EDI Send Pipeline.

I then created an orchestration that picks up messages using the filter recommendations in the party definition. Since it is a batched message, there is no schema that can be made that actually represents the data that the batching orchestration creates, I used an XMLDocument message.

Now came the problem of extracting the value from the message. I thought it would be as easy to use

BatchId=System.Convert.ToString(InMsg.MessagePart,”//ST03[1]/text()”);

However I was getting the error that the first argument needed to be a message, and since it was not a message, but a XLANGs.BastTypes. Any message, I converted to a XMLDocument in an expression:

TempXML=new System.XML.XMLDocument(); TempXML=InMsg.MessagePart;

However I could not use the XLANGs function xpath to extract the value because I did not have a true message, so I had to use C#.

I had to create the following variables (in an atomic scope because it was late and I was tired and did not want to see if the variables were serializable or not)

Variable Name .NET Type
TempXML System.XML.XMLDocument
StringRdr System.IO.StringReader
XPathDoc System.Xml.XPath.XPathDocument
XPathNav System.Xml.XPath.XPathNavigator
XPathExpression System.Xml.XPath.XPathExpression
XPathNodeIterator System.Xml.XPath.XPathNodeIterator

In the expression shape I have the following code:

TempXML=InMsg.MessagePart; StringRdr=New System.IO.StringReader(TempXML.InnerXml); XPathDoc=new System.Xml.XPath.XPathDocument(StringRdr); XPathNav=XPathDoc.CreateNavigator; XPathExpression=XPathNav.Compile("//ST03[1]"); XPathNodeIterator=XPathNav.Select(XPathExpression); while(EPathNodeIterator.MoveNext()) { BatchId=System.Convert.ToString(XPathNodeIterator.Current.Value); }

Which extracts the value into BatchId from the message for me, now I can set the output file name to whatever is in the ST03 element.

ACSUG February Meeting – “Oslo” modelling platform overview – Jeremy Boyd

ACSUG February Meeting – “Oslo” modelling platform overview – Jeremy Boyd

Here are the next Auckland Connected Systems User Group meeting:
"Oslo" modelling platform overview – Jeremy Boyd
Jeremy Boyd (JB) is an experienced solutions architect, developer and consultant with over 7 years experience in the New Zealand market. He is a Microsoft MVP and Regional Director, and a Director of Mindscape.
JB will be […]

Reconfigure BizTalk with MsBuild

The other day I was having an issue with our build server so I decided to write a script to reconfigure BizTalk on our build or development servers. My plan was to run this regularly on a scheduled basis to ensure our build servers were kept clean.

I thought id write a little about this as it may be useful to others. Note the script is aimed at a single machine hosting BizTalk 2006 R2 and SQL Server.

Before I started I exported the configuration of our BizTalk Group to a file and then amended the credentials etc.

The list of tasks I would perform to clean and reconfigure our server is as follows:

Task

Description

Delete log file

When I run the BizTalk Configuration tool from the command line I will make it log to a file for troubleshooting any problems. Before I start the first clean up activity is to delete any old logs

Stop the following services:

  • W3SVC
  • SQLServerAgent

I stop any services which may want to access the BizTalk databases

Restart the following services:

  • MsSQLServer
  • MsDTSServer

Remove the EDI BAM definitions

When EDI is setup the BAM definitions for this feature are configured creating the usual BAM components. We need to remove these using BM.exe before we unconfigure BizTalk because unconfiguring the group will not remove the BAM Definitions

Use the BizTalk Configuration Editor to unconfigure the group.

Stop the WMI Service and any dependant services as advised by the BizTalk documentation

Delete the backup of the SSO Key

Note this is just for a development machine, you need to be careful about this on any real environment. I just delete the backup file here so I can recreate it when configuring the group

Unregister and delete the SSNS application for BAM Alerts

I will use NsControl.exe to do this

Run a SQL Script to drop the BizTalk databases

Delete the share and folder used by BAM Alerts

These are not removed when the group is unconfigured so need to be done manually

Start the following services:

  • Winmgmt (and any dependant services which I stopped earlier)
  • Start w3svc

Ensure any services which may be used while configuring BizTalk are started

Run the BizTalk Configuration Editor passing in the file containing the desired configuration.

To implement this I created a Visual Studio solution and wrote an MsBuild script and used some of the Microsoft.Sdc tasks. The below picture shows the files in this solution.

Its pretty simple and I will provide a link to the sample at the end of this post.

Some of the other ways I have extended this script since include:

  • Using an MsBuild task to register some custom adapters
  • Using an MsBuild task to register the WSE2 and Enterprise LOB adapters
  • Using an MsBuild task to add adapter handlers
  • Using the MsBuild configuration dictionary stuff I have blogged about in previous posts to configure the BizTalk Configuration xml file for different environments

The link to the sample for this post is: http://www.box.net/shared/t4k0m6ps5y

FOR XML PATH

In creating xml data from table data, I came across the need to create empty elements even in there are null values in the columns I am querying.

I first used this logic to force the generation of the tag:

SELECT EmployeeID as "@EmpID", FirstName as "EmpName/First", MiddleName as "EmpName/Middle", LastName as "EmpName/Last" FROM HumanResources.Employee E, Person.Contact C WHERE E.EmployeeID = C.ContactID AND E.EmployeeID=1 FOR XML PATH, ELEMENTS XSINIL

However that returned the result:

<row xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" EmpID="1"> <EmpName> <First>Gustavo</First> <Middle xsi:nil="true" /> <Last>Achong</Last> </EmpName> </row>

I just wanted:

<row EmpID="1"> <EmpName> <First>Gustavo</First> <Middle></Middle> <Last>Achong</Last> </EmpName> </row>

So I changed the code to:

SELECT EmployeeID as "@EmpID", ISNULL(FirstName,'') as "EmpName/First", ISNULL(MiddleName,'') as "EmpName/Middle", ISNULL(LastName,'') as "EmpName/Last" FROM HumanResources.Employee E, Person.Contact C WHERE E.EmployeeID = C.ContactID AND E.EmployeeID=1 FOR XML PATH, ELEMENTS