What is the best way to create a new message within a Biztalk Orchestration?

There are several options when wanting to create a new instance of a message in a BizTalk orchestration.

I've described the three I usually end up using as well as adding some links at the bottom of the answer.

How to define which is the best method really depends - the XMLDocument method is in some regards the tidiest except that if your schema changes this can break without you knowing it. Scott Colestock describes some methods of mitigating that risk.

The BizTalk Mapping method is probably the simplest to understand and won't break when the schema changes. For small schemas this can be a good choice.

For all of these methods an important thing to remember is that if you want to use distinguished fields or promoted properties you will want to create empty elements to populate. You will hit runtime XLANG errors if you try to assign values to elements that are missing (even though those elements may be optional)

BizTalk Map

The simplest option is to just use a BizTalk map - you don't even necessarily need to map anything into the created instance.

To create empty elements you can just map in a string concatenation functoid with an empty string parameter.

Assign one message to another

If you want to create a new instance of a message you can simply copy one mesage to another message of the same schema, in a message assignment shape.

Use an XMLDocument variable

For this you create an orchestration variable of type XMLDocument and then in a message assignment use the LoadXML method to load an XML snippet that matches your schema. You then assign the XMLDocument to the desired BizTalk message.

varXMLDoc.LoadXml(@"<ns0:SomeXML><AnElementToPopulate></AnElementToPopulate></SomeXML>");  
msgYourMessage = varXMLDom;

The inclusion of AnElementToPopulate allows you to using property promotion to assign to it.

I seldom remember the syntax to do this off the top of my head, this is my go to blog entry for reminding myself of the syntax.

Another link here details some methods.


What exactly are you looking for? Is it just creating a new message with a fixed content (like a sort of template)? Or based on something else? You really need to clarify the question and be more specific to get a proper answer.

If you're referring to just creating a message from scratch based with sort of hardcoded content (or close to), then I've found that putting them as embedded resources in a helper C# assembly to be a pretty clean way of doing it.

Tags:

C#

Biztalk