Have you ever wanted to get the Commerce Server managed OrderForm transformed into the Dictionary key pair values? If the answer is yes then keep reading.

There are some scenarios where you just want the OrderForm in Dictionary format so you can call the pipeline components directly or execute the managed pipelines. Please be aware that you can execute the pipeline components outside ASP.NET. For more info please read the following link.

If you have read my pipeline posts then you know that when executing a pipeline, Commerce Server under the covers will transform your managed OrderForm(s) into Commerce Server Dictionary object(s). For whatever reason, the Commerce Server Product Group did not make the PipelineAdapter public. The PipelineAdapter object is responsible for transformation of the OrderForm. So how can you do this if the class is not public? Reflection.

The following code will show you how to transform the OrderForm. You will need to reference the Microsoft.CommerceServer.Runtime.dll and Microsoft.CommerceServer.Interop.dll. You will also need to make sure that you have an app.config with the order node.

// Get an order context to work with
OrderContext context = OrderContext.Create("CSharpSite");

// Get a user basket
Basket cart = context.GetBasket(new Guid(userId));

// Make sure that we have a OrderForm
if (cart.OrderForms.Count == 0)
    cart.OrderForms.Add(new OrderForm());

// Do magic with reflection
Type orderContext = context.GetType();

// PipelineAdapterMap is a property of the order context
PropertyInfo property = orderContext.GetProperty("PipelineAdapterMap", BindingFlags.Instance | BindingFlags.NonPublic);

// Get the value of the PipelineAdapterMap property
object obj = property.GetValue(context, null);
Type pipelineAdapter = obj.GetType();           
MethodInfo[] methods = pipelineAdapter.GetMethods(BindingFlags.Instance | BindingFlags.NonPublic);

object[] orderform = new object[1];
orderform[0] = cart.OrderForms[0];


// Method 15 has is what we want and pass it our managed OrderForm
object dict = methods[16].Invoke(obj, orderform);


// Do something with the Dictionary OrderForm
Dictionary dictionary = (Dictionary)dict;