Click here to Skip to main content
13,193,650 members (43,913 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


1 bookmarked
Posted 28 Jan 2013

BizTalk - Looping through repeating message nodes in orchestrations

, 28 Jan 2013
Rate this:
Please Sign up or sign in to vote.
BizTalk - Looping through repeating message nodes in orchestrations

Say that you have an incoming BizTalk message with many repeating nodes, and you want to process each one the same. The easiest thing to do would be to use the envelope to split the single message into multiple messages, then have multiple instances of your orchestration handle each one (similar to how the HIPAA “multiple” message schemas work). But what if you need to do some work after every individual message has been processed? I’ll show you how to use the Loop functoid along with xpath queries to loop through the message.

In my case, I’m receiving a flat file where each line is a response to a transaction we have submitted. After mapping the flat file to XML, I get something like this:


I want to insert each Record into the database, then do some work once they’ve all been inserted.

The first step is to create the single Record version of the message. It’ll be basically the same as the incoming Response message, just without the Response node at the top:


The schema needs to be created without a Target Namespace; you’ll see why later.

Next, create the orchestration, two messages:

  • FullResponse (using the full flat file schema)
  • SingleResponse (using the new single Record schema)

…and three variables:

  • counter - int
  • counterString – string
  • responseCount - int

Drag various shapes onto your orchestration until you wind up with something that look like this:


I’ll start at the top and detail how each shape should be configured.

Receive – receives FullResponse message

Expression - here you’re going to use an xpath query to count how many Record nodes are in the FullResponse message:

responseCount = xpath(InstaMedResponse, "count(/*[local-name()='Response' and namespace-uri()='http://MyProject.Response_FF']/*[local-name()='Record' and namespace-uri()=''])");       
counter = 1;       
counterString = "1";

(Pay attention to the namespace, yours may be different)

Loop - what’s the loop condition?

counter <= responseCount

Message Assignment – here we’re grabbing a single Record node and assigning it to the SingleResponse message:

SingleResponse = xpath(InstaMedResponse, "/*[local-name()='Response' and namespace-uri()='http://AnciPay.InstaMedResponse_FF']/*[local-name()='Record' and namespace-uri()=''][" + counterString + "]");

Remember how I said the new schema needs to be created without a target namespace? Your xpath query will return the Record node without a namespace. Also, we have to (unfortunately) use c<code>ounterString rather than just counter.ToString(), otherwise BizTalk will complain about an xpath error.

Map – this one is optional for the demo; in my case I’m mapping it to the Request Side of a generated schema.

Expression – here we need to increment the counter:

counter = counter + 1;      
counterString = counter.ToString();

Send & Receive - I’m using a WCF port to save to a database, but you could omit these steps if you want.

Expression – this can really be anything that you want to happen after every individual Record node has been processed – run a stored procedure, send email, whatever.

Compile, deploy and test. If you just drop the SingleResponse message to a send port (instead of the database), you should see your incoming message split into multiple messages.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

QuestionGood Article Pin
Member 265615822-May-15 10:03
memberMember 265615822-May-15 10:03 
GeneralExcellent Article Helped Me a Lot Pin
Praveen P N20-Nov-14 21:55
memberPraveen P N20-Nov-14 21:55 
GeneralCannot view the screenshot Pin
kunal_g18-Jul-13 19:43
memberkunal_g18-Jul-13 19:43 
GeneralRe: Cannot view the screenshot Pin
BillOsuch19-Jul-13 3:55
memberBillOsuch19-Jul-13 3:55 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.171018.2 | Last Updated 28 Jan 2013
Article Copyright 2013 by BillOsuch
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid