Friday, March 13, 2015

How to Access an Orchestration variable in Map

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:
               msgOutgoing.ReceivedFileName=msgIncoming(File.ReceivedFileName);

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

To answer all questions above, have considered following scenario:

Scenario:

Order is received on file location and it is to be inserted in database along with FileName.

Creating the Solution:


Create Order table


Create Incoming Order Schema



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




 Create Orchestration



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)
4. Add construct shape to create msgSQLOrder, this construct shape uses transform shape to create destination message from incoming message and the supporting message which we created in previous step
5.Thus there are two source to this map, msgIncomingOrder and msgOrchestrationVariables and one destination – msgSQLOrder
Mapping is straight forward
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.

Testing


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 



Download Sample:


Related Post:


2 comments:

  1. Hi Mahesh,

    Can I request you for the sample test file which you have used for this Demo... I have done exactly the same as mentioned over here, but still facing the following issue :
    ===========ERROR================
    A message received by adapter "FILE" on receive location "Receive Location RFN" with URI "E:\CaptureFN\IN\*.xml" is suspended.
    Error details: There was a failure executing the receive pipeline: "Microsoft.BizTalk.DefaultPipelines.XMLReceive, Microsoft.BizTalk.DefaultPipelines, Version=3.0.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" Source: "XML disassembler" Receive Port: "ReceivePortRFN" URI: "E:\CaptureFN\IN\*.xml" Reason: Finding the document specification by message type "InputMessagePart_0" failed. Verify the schema deployed properly.
    MessageId: {5B03D811-E741-4D2F-8EF7-611D003AD5E1}
    InstanceID: {48D0CCD5-7599-41EB-ADC9-455EA149C0B4}
    =================================


    ==== Test File which I have used =====



    1
    1
    1
    1
    1
    1


    ==================================

    ReplyDelete
  2. Hi Ajit,

    I have uploaded whole sample , along with sample files : https://code.msdn.microsoft.com/How-to-Access-an-8b12bdd2

    ReplyDelete