Custom Pipeline to Add DocType in Message

Sometimes we require to add some element, tag, DocType in incoming or outgoing message from BizTalk. We can achieve this by using custom pipeline. But we should use this pipeline component in Decode or encode stage of Pipeline.

In this sample custom pipeline, we convert the message in string. So you can take this code and change according your business requirement.

Below link is defined for Custom Pipeline setup details

https://vkbiztalk.com/2017/08/04/custom-pipeline/

Here in this sample code I have added DocType on header of message when sending the message to send port. DocType will defined at design time when consume this pipeline component on encode stage on send pipeline.

using System;

using System.IO;

using System.Text;

using System.Drawing;

using System.Resources;

using System.Reflection;

using System.Diagnostics;

using System.Collections;

using System.ComponentModel;

using Microsoft.BizTalk.Message.Interop;

using Microsoft.BizTalk.Component.Interop;

using Microsoft.BizTalk.Component;

using Microsoft.BizTalk.Messaging;

using System.IO;

using System.Xml;

 

namespace BizTalk.Custom.PipelineComponents

{

[ComponentCategory(CategoryTypes.CATID_PipelineComponent)]

[ComponentCategory(CategoryTypes.CATID_Any)]

[System.Runtime.InteropServices.Guid(“CB814714-BC81-4BDA-B361-69CA270997E8”)]

public class AddDocFileName : IBaseComponent, IComponentUI, Microsoft.BizTalk.Component.Interop.IComponent, IPersistPropertyBag

{

#region Configuration Properties

private string _DocTypeName;

public string DocTypeName

{

get

{

return _DocTypeName;

}

set

{

_DocTypeName = value;

}

}

#endregion

public string Description

{

get

{

return “Pipeline component To Add DocType in Outgoing XML”;

}

}

public string Name

{

get

{

return “AddDocNameinXML”;

}

}

public string Version

{

get

{

return “1.0.0.0”;

}

}

public IntPtr Icon

{

get

{

return new System.IntPtr();

}

}

public System.Collections.IEnumerator Validate(object projectSystem)

{

return null;

}

public void GetClassID(out Guid classID)

{

classID = new Guid(“CB814714-BC81-4BDA-B361-69CA270997E8”);

}

public void InitNew()

{

}

public virtual void Load(Microsoft.BizTalk.Component.Interop.IPropertyBag pb, int errlog)

{

object val = null;

val = this.ReadPropertyBag(pb, “DocTypeName”);

if ((val != null))

{

this._DocTypeName = ((string)(val));

}

}

public virtual void Save(Microsoft.BizTalk.Component.Interop.IPropertyBag pb, bool fClearDirty, bool fSaveAllProperties)

{

this.WritePropertyBag(pb, “DocTypeName”, this.DocTypeName);

}

#region utility functionality

/// <summary>

/// Reads property value from property bag

/// </summary>

/// <param name=”pb”>Property bag</param>

/// <param name=”propName”>Name of property</param>

/// <returns>Value of the property</returns>

private object ReadPropertyBag(Microsoft.BizTalk.Component.Interop.IPropertyBag pb, string propName)

{

object val = null;

try

{

pb.Read(propName, out val, 0);

}

catch (System.ArgumentException)

{

return val;

}

catch (System.Exception e)

{

throw new System.ApplicationException(e.Message);

}

return val;

}

/// <summary>

/// Writes property values into a property bag.

/// </summary>

/// <param name=”pb”>Property bag.</param>

/// <param name=”propName”>Name of property.</param>

/// <param name=”val”>Value of property.</param>

private void WritePropertyBag(Microsoft.BizTalk.Component.Interop.IPropertyBag pb, string propName, object val)

{

try

{

pb.Write(propName, ref val);

}

catch (System.Exception e)

{

throw new System.ApplicationException(e.Message);

}

}

#endregion

public IBaseMessage Execute(IPipelineContext pContext, IBaseMessage pInMsg)

{

IBaseMessagePart bodyPart = pInMsg.BodyPart;

XmlDocument xDoc = new XmlDocument();

if (bodyPart != null)

{

Stream originalStream = bodyPart.GetOriginalDataStream();

string originalDataString;

try

{

//fetch original message

Stream originalMessageStream = bodyPart.GetOriginalDataStream();

byte[] bufferOriginalMessage = new byte[originalMessageStream.Length];

originalMessageStream.Read(bufferOriginalMessage, 0, Convert.ToInt32(originalMessageStream.Length));

originalDataString = System.Text.ASCIIEncoding.ASCII.GetString(bufferOriginalMessage);

}

catch (Exception ex)

{

ex.Source = “AddDocTypePipelineComponent”;

throw ex;

}

XmlDocument originalMessageXml = new XmlDocument();

if (originalStream != null)

{

originalDataString = originalDataString.Remove(0, 41);

originalDataString = @”<?xml version=’1.0′ encoding=’UTF-8′?>” + DocTypeName + originalDataString;

byte[] outBytes = System.Text.Encoding.ASCII.GetBytes(originalDataString);

MemoryStream memStream = new MemoryStream();

memStream.Write(outBytes, 0, outBytes.Length);

memStream.Position = 0;

bodyPart.Data = memStream;

pContext.ResourceTracker.AddResource(memStream);

 

}

}

return pInMsg;

}

}

}

 

Sample Input File:

<?xml version=”1.0″ encoding=”UTF-8″?>

<Test xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” xmlns:xsd=”http://www.w3.org/2001/XMLSchema“><Name> </Name></Test>

Sample Output File:

<?xml version=”1.0″ encoding=”UTF-8″?>

<!DOCTYPE Test SYSTEM “http://localhost/Test/DTD/TestDocType.dtd”&gt;

<Test xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” xmlns:xsd=”http://www.w3.org/2001/XMLSchema“><Name> </Name></Test>

 

 

 

Advertisements

Same Schema in different BizTalk applications

Error: Cannot locate document specification because multiple schema matched the message type

This is small topic in BizTalk, But I have seen that people also stuck in this situation. Sometimes when this question or (can you have several same types of Messagetype in BizTalk Admin Console) asks in interview mostly people directly say that BizTalk cannot have two similar kind of schema although these schemas belong to different BizTalk applications.

They can say that because if they only use default pipeline and when any message process then you got exception that multiple schema match with the incoming file.

Sometime in BizTalk we require to use same schema in different application and those application should not interlink with each other with schema references.

You can have several same kinds of schemas in different BizTalk applications. Now question is how to implement that.

You can implement in two ways.

  1. Provide Schema name in DocumentSpecNames in XML Receive Pipeline

Configure Specific Schema in DocumentSpecNames property in XML Receive Pipeline on Receive Location. So, after receiving message from Receive Port. In this XML Receive Pipeline BizTalk will know that which BizTalk applications should processed.

XMLReceivePipeline.jpg

To provide schema name, you select particular schema and open the properties on schema as below:

SchemaProperty.jpg

You provide Schema’ name and assembly in DocumentSpecNames in XML Receive pipeline.

DocumentSpecNames: <Name>, <Assembly>

e.g. Test.DocSchema, Test, Version=1.0.0.0, Culture=neutral, PublicKeyToken=28e25aa7767eb2e2

2. Add schema in XML disassembler in pipeline component:

You can do the same thing through creating pipeline in BizTalk Application. In Disassemble stage you need to add XML Disassembler and in the property for XML disassembler component, Document Schema tab is defined.

XMLDIsassemble.jpg

You need to open the collection of Document schemas tab and select the respective schema which need to process in the BizTalk application. And add this pipeline on Receive Port.

Disassembler.jpg

Note: For BizTalk best practices you should always add schema in XML disassembler stage. So that in future if any other application will deploy with same schema then it will not affect the current application.