Saturday, 4 December 2010

How to change the Output file name in BizTalk File Adapters?

In Many situations we may have to save the file in the Output folder in a proper format.

BizTalk Send Port with File Adapter supports the following formats (macros) for the files saved.

%MessageID%
%datetime%
%SourceFileName%
%time%

Incase if we want the output file name to be some thing other than the available macros, we need to do some coding either in the Orchestration or in the Pipeline.

This Post will show you how you set the output file name in an Orchestration.

Scenario: Create a schema, receive a file of that type and set a name for the file and save it back to an output folder.

1. Create a sample schema in a BizTalk Project and add an Orchestration to the project.
2. Create two messages of the Schema Created in above step
3. Add a Receive, Message assignment and Send Shape as shown below.
4. Configure the Receive shape to Receive Message_1, set activate to true.
5. Configure the Construct Message shape to construct Message_2 and Send shape to Send Message_2. Resulting Orchestration should look like below.



6. Add the following code in the Message Assignment shape.

Message_2 = Message_1;
Message_2(FILE.ReceivedFileName) = "MyOutputFile";

7. Add a receive and send ports and complete the Orchestration.



8. Deploy the Orchestration.

9. Configure the Send Port File Adapter as show below. (Change the file name to use %SourceFileName% Macro. (Case Sensitive)


10. Drop an instance of the Schema in the input folder, and see that the Output file name will be the same as what I have given in Step6.



The idea behind this is that, I have change the ReceivedFileName Context Property of the message received to my desired format and then I have used the % SourceFileName% macro.

The same can also be done using a Pipeline.

Hope it helps.
- Shiv

Issue: Schema not found in BizTalk Type Picker of a BizTalk Map

Today I had a different issue when in I started to work on a BizTalk Sample created by a colleague.
It has a source and destination schema, which are to be mapped.



A new map is then created and a source schema is added. When I clicked on Select Destination Schema, I was not able to find the destination schema in the BizTalk Type Picker of the BizTalk Map.


There are no build errors and every thing looked fine.

The culprit which led to this is the Build Action property of the schema.

This property will be shown when you select the schema in the Solution Explorer.



If this property is set to None, it will not be included in the Build and hence will not be able to use that in BizTalk Tools.

I made the Build Action to BTSCompile and it showed up in the BizTalk Type Picker of the BizTalk Map.



The reason being for this is that the schema was not created using BizTalk Schema Editor rather using an external tool. Hence when this schema was imported to BizTalk Project, its Build Action property was set to None.



So, care should be taken for the schemas that are created outside BizTalk Schema Editor.

Hope it helps.

- Shiv

Wednesday, 1 December 2010

How to modify a BizTalk Message using C# or External Dll ?

In many situations the Mapper available with BizTalk Server or Xpath functions might not be sufficient to construct the Required Message in BizTalk Server.

In these scenarios, we can pass the necessary inputs to an external library and construct the message there using C# code.

This article will show two possible ways of achieving this.

Scenario:

You have a schema in your BizTalk Project. You receive a message of this type in a receive folder and pass this message to an External Library, update the message and send it back to BizTalk. Save the updated message to a new location.

Solution:
There are two possible ways of doing this.

Create an equivalent class file for the Schema and pass the message as class object to the External dll

Pass the message contents as an XML Document to the External Dll and modify the message using XMLDocument or XLinkq or any other thing else.

1. Create a new BizTalk Project and add a Sample schema Schema1 as shown below.


2. Use the XSD.EXE utility to convert the schema created above into a serializable class. For this, Go to Visual Studio Command Prompt and give the following Command at the directory where Schema is located.

XSD Schema1.xsd /c /o:c:\

Note: See this for more details on xsd.exe

3. Create a Class Library Project and add the above class file to the Project. Now we have an equivalent class file for the schema.
4. Create a Static method in the Class that updates the Received Object and sends it back using C #as shown below.


5. Build it and add a reference of this class library to the BizTalk Project.
6. Add an orchestration to the BizTalk Project and create two messages of the Type Schema1
7. Add a Receive Shape and Message Constructor shape and a send shape and complete the orchestration as shown below.


8. Add the below code in the Message assignment shape.

Message_2 = ClassLib.Root.GetClassReference(Message_1);

9. This will call the GetClassReference and the Message Message_1 will be automatially serialized into the class Object. Within the method, schema elements and attributs can be accessed just like class properties.
10. To Test this project, deploy both BizTalk project and Class Library to GAC and put an input file, you will see that the values are updated.
11. The same can also be achieved by passing the Message Contents as an XML object as given below.

Variable_1 = Message_1;
Variable_1.LoadXml(ClassLib.Root.GetClassReferenceUsingXML(Variable_1.OuterXml));
Message_3 = Variable_1;

Note: Variable_1 is of tpye XMLDocument.

12. Now you should have a method in the External Library that modifies the message using XML Operations. A sample is given below.


While I tested with Schemas with more than 50 Elements, I find that using XMLDocument to modify the Contents was very much faster that using Class Objects.

Hope it helps.
- Shiv


Web Counters

Project Reference Errors in BizTalk

While developing BizTalk projects that referencing several other projects, many errors will be shown while compiling the BizTalk Projects. These errors will go away once the references are removed and added again.

Also, sometimes, updated made in the referenced projects will not be updated automatically.

This is a known issue with BizTalk Server 2009 and after googling I found that Microsoft has given a Hot Fix for this which can be downloaded here.

Web Counters