Share via


Creating BizTalk Server Message In Code

I had to create  a message instance out of orchestrations  in the one of my recently projects so I searched internet a bit and I found a simple way about this. Following code indicates how we can create a specific message instance of a schema. I created helper class for this in my project. Key points are the class BtsCatalogExplorer and DocumentSpec. You can use class BtsCatalogExplorer to reach BizTalk Server artifacts and objects such as schemas,ports, hosts etc. It has so useful data and object references indeed. We can obtain this catalog by giving Server Name and BizTalk Server Management Database Name to its ConnectionString. Next step to create a message instance from a schema is to get related schema from catalog object. The final step is to create the message through method DocumentSpec.CreateXmlInstance . You have to give current root name for the schema when creating DocumentSpec. When created the Xml message instance you will be available to use it from BizTalk Orchestrations also. Note that you hive to add Assembly Microsoft.BizTalk.ExplorerOM to references.

public static  XmlDocument CreateBTSMessage(string schemaName, string rootNode) {
           DocumentSpec docSpec;
           XmlDocument doc = new  XmlDocument();
 
           StringBuilder sb = new  StringBuilder();
           StringWriter sw = new  StringWriter(sb);
 
           try {
               BtsCatalogExplorer catalog = new  BtsCatalogExplorer();
               catalog.ConnectionString = "Data Source=YourServer;Initial Catalog=BizTalkMgmtDb;Integrated Security=True";
 
               Schema schema = catalog.Schemas[schemaName];
               if (schema != null) {
                   docSpec = new  DocumentSpec(rootNode, schema.BtsAssembly.DisplayName);
                   if (docSpec != null) {
                       doc.Load(docSpec.CreateXmlInstance(sw));
                   }
               }
           } catch  (Exception ex) {
               //Log exception message, ex.Message
           } finally  {
               sw.Dispose();
           }
 
           return doc;
       }

See Also

Another important place to find a huge amount of BizTalk related articles is the TechNet Wiki itself. The best entry point is BizTalk Server Resources on the TechNet Wiki.