Many times while developing BizTalk application there arises a need to access some message context properties or some variables from orchestration in map. For message context properties you can manage with the help of message assignment shape, for example: filename of incoming message(received via file or ftp adapter) is to be passed to a field in destination message. Consider incoming message is declared as msgIncoming and outgoing message is declared as msgOutgoing and it has field as Receivedfilename,in that case:
- Mark the field ReceivedFileName as distinguished
- In message assignment shape, add following code:
Likewise any other message context property can be accessed provided that it is written in context either by adapter or pipeline, in above scenario if some other adapter is used then File.ReceivedFileName won’t be available as this property is only populated when file adapter is used. But what about orchestration variable? Well there is no direct way to access it in map.
So how to access orchestration variables in map?
This question is very frequently asked on forums, also I have received questions directly around it along with following questions(answers of all I intend to cover in this post):
1. How to pass ReceivedFileName in map/How to insert ReceivedFileName in database
2. How to construct message using LoadXml Method
3. How to pass orchestration variable in map
4. How to create map with multiple source to single destination
5. How to configure WCF-Custom adapter
6. How to generate schema from Table
6. How to generate schema from Table
To answer all questions above, have considered following scenario:
Order is received on file location and it is to be inserted in database along with FileName.
Creating the Solution:
Create Order table
Generate schema from Order table
Binding : sqlBinding as the database we are dealing is SQL Server
IntialCatalog : Demo as this is database which is to be connected and it holds the table we are dealing with
Server : “.” As database resides on same machine, if it was on other machine then it would have been that machine’s address.
Select contract type: Client (Outbound Operations) as message is going out of BizTalk
Operations: Insert as data will be inserted in the table
- Select sqlBinding
- Select how you would connect to SQL
- Provide the database where the table reside against Initial catalog and on which server (here it is dot as database in on local machine)
- Click Connect
- Select outbound operation as data is inserted into SQL from BizTalk (outbound in reference to BizTalk), if we were to receive then it would have been Inbound operation
- Select the table and four operations will be visible
- Provide filename prefix (not mandatory) but it helps in managing and reading when number of generated schemas are more in a project
- As highlighted above, four files are added
Create supporting schema
To hold Orchestration variable and context properties. In this schema add fields corresponding to the context properties and variables which you want to use in map, here as I am demonstrating only one context property so only one field in the schema.
And mark the fields as Distinguished, so as to make it available in message assignment shape to assign values to it
1. Declare messages and variable
- Create three messages tied to their respect respective Schema
- XmlDoc variable of type System.Xml.XmlDocument
Why XmlDocument variable? The class "XmlDocument" is a super class for all XML messages, and hence it can hold any type of XML message and subsequently any type of orchestration message.
2. Add Receive shape to receive Incoming Order
3. Add construct shape with message assignment shape to construct the msgOrchestrationVariables, here we use LoadXml method to construct it.
Apart from Map, Message Assignment, using .Net, the fourth way to construct message is with the help of LoadXML method – which provides a way to load the instance of schema of the message which is to be created (with or without actual values)
6. Add send shape to send the destination message, final orchestration is as below
Deploy and Configure Application
Sign with strong name key, assign Application name, Build the application and Deploy. After deploying configure the application in BizTalk Admin Console, Receive port with a receive location watching the file location (File Adapter) with xml as file mask.
Send Port with destination url set to the Order Table we created at start
We can directly import the binding which was added along with the schemas by the Add Generated Items wizard, this will create a preconfigured URI, Action and bindings. But here, I have created static Sendport static explicitly created and configured it manually
So how do I know what values to configure? It is the binding file which has all the information
WCF-SQL adapter is dedicated adapter for SQL which has pre configured SQL Binding, but here WCF-Custom adapter is used so binding has to be explicitly selected, click on the drop down against the Bindingtype and select sqlBinding, all the properties will be populated with default values.
To test the application, two sample files generated from the Order schema, and dropped at the file location which is configured as Receive Location, and below is what appears in table