Add documents from a Base64Binary xml element to SharePoint with Biztalk 2006

On my current project we wanted to add documents which come in as attachment through a webservice to a sharepoint document library to store them.

We used Biztalk 2006 to receive the file, get the different files from the xml and then send them to SharePoint. In this blogpost I’ll explain how to do this with a small POC I made.

First off we started with a receive port which is able to receive the xml message (the binary port on the picture) we receive this message into a BinaryMessage message which is linked to the following schema

An example message is looking like this: (where the <document> should have the attachments as a base64binary string.

After receiving the message we want to loop through all the messages in the document so I created a foreach to read all the document elements from the xml.

The code to build the loop is the following:

InitializeLoop :

iteration = 1;

documentCount = xpath(BinaryFile,"count(/*[local-name()='BinaryFileToSharePointTest' and namespace-uri()='']/*[local-name()='Document' and namespace-uri()=''])");

first I set the iteration variable to 1 and I set the documentCount to the amount of document elements that are in the incoming xml file.

Message assignment

BinaryFileWithMetadata = BinaryFile;

binaryMessageCreator = new BinaryToSharePoint.Helper.BinaryMessageCreator();

selectXpath = "string(/*[local-name()='BinaryFileToSharePointTest' and namespace-uri()='']/*[local-name()='Document' and namespace-uri()='']["+ System.Convert.ToString(iteration)+"]/text())";

base64String = xpath(BinaryFile,selectXpath);


configProperties = new VdCruijsenNet.Utilities.Sharepoint.ConfigPropertiesXML();


BinaryFileWithMetadata(WSS.ConfigPropertiesXml) = configProperties.ToString();

BinaryFileWithMetadata(WSS.Filename) = "bestandsnaamTest "+System.Convert.ToString(iteration)+".pdf";

In the message assignment we first Create the new outgoing message for each of the documents in the incoming xml file. Then we select the base64binary string with an xpath expression and send it to the binaryMessageCreator.

The binaryMessageCreator creates a new System.Xml.Document (which isn’t a xml file but biztalk doesn’t know that :) from the base64binary string. For the source of the BinaryStreamFactory see below:

After creating the new Message of the type System.Xml.Document, which is actually a byte[] containing the file we add a filename and some metadata which can be stored in SharePoint we send the message with the send shape to sharepoint.

The SendPort is a “Windows SharePoint service” sendport which can automatically add the file to a sharepoint document library.




As you can see it’s pretty easy to get the binary files from an xml file in Biztalk after you know how you can do it.

Happy Coding

Geert van der Cruijsen

Share on Facebook
Kick It on
Shout it
Post on Twitter

Changing the “object reference not set to an instance of an object” Exception in BizUnit 2.3 for Biztalk 2006

Lately I’ve been working with BizUnit 2.3 to create Unit tests for the Orchestrations we’ve build in BizTalk 2006 at our current customer.

BizUnit 2.3 is an opensource project on CodePlex ( ) to help you make the testing of these orchestrations easier. I’ve noticed that when you create a validation step and try to do a check with an xpath query you can get weird errors when the element you are looking for doesn’t exists.The Exception you’ll receive will be: 


object reference not set to an instance of an object

This Error doesn’t really say what is going wrong in your test report and that’s because the application isn’t throwing an ApplicationException but the code right in front of it will go wrong because checknode is null and BizUnit is trying to compare the checknode.InnerText.What I did was change the code in the XmlValidationStep.cs so it will give a nice ApplicationException when the element you are trying to check doesn’t exist in the xml file you are checking.The changes I made to the code are written down here and should be inserted into XmlValidationStep.cs in the BizUnit 2.3 project


string xpathExp = xpath.SelectSingleNode(“@query”).Value;
XmlNode valNode = xpath.SelectSingleNode(“.”);
string nodeValue = valNode.InnerText;
context.LogInfo(“XmlValidationStep evaluting XPath {0} equals \”{1}\”", xpathExp, nodeValue );
XmlNode checkNode = null;

  checkNode = doc.SelectSingleNode(xpathExp);
{ }
(checkNode != null)

(0 != nodeValue.CompareTo(checkNode.InnerText))
    throw new ApplicationException(string.Format(“XmlValidationStep failed, compare {0} != {1}, xpath query used: {2}”, nodeValue, checkNode.InnerText, xpathExp));

  throw new ApplicationException(string.Format(“XmlValidationStep failed, no element found, xpath query used: {0}”, xpathExp));

Hope this will help you understand the errors you get while testing with BizUnit on your project! 

Geert van der Cruijsen



Share on Facebook
Kick It on
Shout it
Post on Twitter