Wednesday 14 October 2015

What to do if you can't get the time into BAM via orchestration.

I've experienced a problem with trying to get the current datetime into BAM from within an orchestration. All my attempts of using the PortStartTime and PortEndTime in any of the orchestration shapes have ended up with Null values or a date like '1900-01-01 00:00:00.000'. Normally I think this works for other orchestrations I've worked on but this particular orchestration I'm currently working on seems to not want to work.

Before I move onto the solution, I'll mention that as an alternative you could take the PortStartTime from the receive location and use a continuation to link it to your orchestration.

To work around this I'm going to call the BAM API directly. Firstly we drop an expression shape in the orchestration and call BeginActivity


Microsoft.BizTalk.BAM.EventObservation.OrchestrationEventStream.BeginActivity(<<activity name>>, <<activity instance>>);
 The activity name can be taken from the activity you created from the BAM addin for Excel. The activity instance is any id. Normally TPE uses ActivityID. You won't have access to this value from within your orchestration so you will need to provide a new one. So go ahead and drag a new id such as MessageID or InterchangeID onto the ActivityID in TPE like so


Now from your expression shape in you orchestration, use the following code.
Microsoft.BizTalk.BAM.EventObservation.OrchestrationEventStream.BeginActivity("my activity", receivedMessage(BTS.MessageID));
 Microsoft.BizTalk.BAM.EventObservation.OrchestrationEventStream.UpdateActivity("my activity", receivedMessage(BTS.MessageID), "DateTime", System.DateTime.UtcNow);
 Microsoft.BizTalk.BAM.EventObservation.OrchestrationEventStream.EndActivity("my activity", receivedMessage(BTS.MessageID));
And that's it. We call the BAM API and update the activity providing it the current datetime.