Share via


How to Retrieve Purchase Orders as XML

After you get a value for the OrderGroupId member of a PurchaseOrder object, you can use the OrderGroupId property to retrieve the purchase order in XML format. You might use the XML format of a purchase order to send data to a line of business (LOB) system by using a Commerce Server Core Systems BizTalk adapter.

To retrieve purchase orders as XML

  1. Create an OrderManagementContext object.

    For more information about how to perform this step, see How to Create an OrderManagementContext Object.

  2. Get the PurchaseOrderManager object from the OrderManagementContext object.

  3. Create an array of GUIDs that contains the values of the OrderGroupId members of the purchase orders that you want to retrieve.

  4. Call the GetPurchaseOrdersAsXml method of the PurchaseOrderManager object and pass it the list of GUIDs.

Example

The following code example retrieves all new orders in XML format.

using System;
using System.Data;
using System.Collections.Generic;
using System.Globalization;
using System.Xml;
using Microsoft.CommerceServer;
using Microsoft.CommerceServer.Orders;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                // Create the OrderManagementContext object. This
                // example accesses the Orders System in local mode by
                // creating an OrderSiteAgent object. You could also
                // access the Orders System in agent mode by creating
                // an OrderServiceAgent object.

                // In the following, replace "StarterSite" with the
                // name of your site.
                OrderSiteAgent ordersAgent = new OrderSiteAgent("StarterSite");
                OrderManagementContext mgmtContext = OrderManagementContext.Create(ordersAgent);
                PurchaseOrderManager manager = mgmtContext.PurchaseOrderManager;

                // Find the purchase orders that you want to retrieve
                // as XML. For this example, find all new orders.
                DataSet searchableProperties = manager.GetSearchableProperties(CultureInfo.CurrentUICulture.ToString());
                SearchClauseFactory searchClauseFactory = manager.GetSearchClauseFactory(searchableProperties, "PurchaseOrder");
                SearchClause clause = searchClauseFactory.CreateClause(ExplicitComparisonOperator.Equal, "Status", "NewOrder");
                DataSet results = manager.SearchPurchaseOrders(clause);
                
                // Get the value of the OrderGroupId property of each
                // purchase order.
                List<Guid> poIds = new List<Guid>();
                foreach (DataRow row in results.Tables[0].Rows)
                {
                    string poId = row["OrderGroupId"].ToString();
                    poIds.Add(new Guid(poId));
                }

                // Get the XML representation of the purchase orders.
                XmlElement poXml = manager.GetPurchaseOrdersAsXml(poIds.ToArray());

                // Display the XML.
                Console.WriteLine(poXml.OuterXml);
                Console.ReadLine();
            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception: {0}\r\nMessage: {1}", ex.GetType(), ex.Message);
                if (ex.InnerException != null)
                {
                    Console.WriteLine("\r\nInner Exception: {0}\r\nMessage: {1}", ex.InnerException.GetType(), ex.InnerException.Message);
                }
                Console.ReadLine();
            }
        }
    }
}

This code example will produce output only if your database contains purchase orders whose status is "NewOrder".

Compiling the Code

To run this code example, create a console application and add references to the following assemblies:

  • Microsoft.CommerceServer.CrossTierTypes.dll

  • Microsoft.CommerceServer.Orders.CrossTierTypes.dll

  • Microsoft.CommerceServer.Orders.DataManagement.dll

See Also

Other Resources

Sample Purchase Order Search Implementation

Searching the Orders Database

Working with Orders Data Management Objects