Here is a good question that I recently answered in a BizTalk Forum. I thought it would be of interest to others so I’m posting my response in this blog.

“I also have an orchestration that includes a custom .net component for writing BAM activities.  I'm utilizing serializable classes and strongly typed APIs (GenerateTypedBAMAPI tool) to write to the BAM db.  My question centers around how to access the unique Activity ID in my net code, which was initially created in the pipeline.   I presume that I have to pass this data to the orchestration.  I would really like to see an example of how this is done.”

This is a good question, not hard, just good!

You have a number of options here.  Logically, you need a way of storing the Activity ID and then passing it along to the orchestration from the time the message is processed through the pipeline to when it is processed inside the Orchestration. One way to keep track of this ActivityID is to push the id to the header section of the Message (aka… the MessageContext). To push it here you need to "Promote" the Activity ID value. However, property promotion does come at a cost because you're adding overhead to the message process for purposes of BAM Tracking.  If you are not concerned with the additional processing overhead then this option may work for you.

You can promote the value two ways, 1) you can use Property Field promotion, or 2) distinguished field promotion. Property Field promotion dictates that you need a special "property" schema deployed that contains a element and element type of the data (a string for ActivityID) that you are promoting eventually inside the pipeline.  Distinguished field promotion is just when you add a value to the MessageContext.

Code:
//For writing a distinguished field value
iinMsg.Context.Write( "/*[local-name()='MyBamInfo' and namespace-uri()='http://mycompany.com']/*[local-name()='ActivityID' and namespace-uri()='']",
http://schemas.microsoft.com/BizTalk/2003/btsDistinguishedFields", ActivityID);
 
//Create and deploy a custom property schema, and add an element and derive it from the MessageContextPropertyBase (if not in message payload, otherwise MessageDataPropertyBase if it will be in the message: this is set underneath the Propery Base Schema property)
//For writing a property field value
inMsg.Context.Promote( "propertySchem.ActivityID", "
http://schemas.microsoft.com/BizTalk/2003/system-properties", ActivityID);


In either case, you can use these promoted values inside the Orchestration instance for your BAM tracking purposes. Depending on whether you use Property Field or a Distinguished Field, the methods you use to access promoted values are going to be different. For Distinguished fields, you must use code within the orchestration to get the distinguished field values. Whereas, Property promoted values can be exposed with the Message(PropertySchema.ActivityID) syntax for the value that you promoted.
 
Another option is to add this ActivityID to your message schema and populate the ID within the message (inside the pipeline if you want) and then promote the values using either a Distinguished or Property field. Then you can access the values using the normal msg.ActivityID, or Msg(PropertySchema.ActivityID) options.

Note: if you're using continuation you don't want to call EndActivity within the pipeline component, because that will mark the activity instance record as "IsComplete" which commits the tracking record.

Here is a link for more help:
http://blogs.msdn.com/keithlim/archive/2006/02/08/527227.aspx
(It's kind of old but it's still good.)