Share via


How to Bulk Update Purchase Orders

For the latest version of Commerce Server 2007 Help, see the Microsoft Web site.

You can update individual properties of purchase orders or their components by using the UpdatePurchaseOrderProperties method of the PurchaseOrderManager object. Line-of-business (LOB) applications frequently use the UpdatePurchaseOrderProperties method to communicate with a Commerce Server application by sending XML code. For example, a distribution system might create an XML file that identifies the orders that have been shipped and update the status of the orders to "Shipped". You could write a program that reads the XML file and calls the UpdatePurchaseOrderProperties method to update the status of the orders in the database.

Bb219323.alert_caution(en-US,CS.70).gifImportant Note:

The UpdatePurchaseOrderProperties method does not validate the properties.

An application that calls the UpdatePurchaseOrderProperties method must run under an account that has the OrdersViewer role and must have permission to perform the ViewPurchaseOrderPayments task.

To update purchase order properties

  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 XmlDocument object that contains the XML code that indicates how to update the purchase order.

  4. Call the UpdatePurchaseOrderProperties method of the PurchaseOrderManager object, and pass the DocumentElement member of the XmlDocument object as the first parameter. Pass the ID that you use for your application as the second parameter.

    For more information about the application ID and its use, see How to Coordinate Updates to Orders.

Example

The following code example modifies several properties of a purchase order and a line item. The XML code that follows the example specifies which properties to modify.

using System;
using System.Data;
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 context = OrderManagementContext.Create(ordersAgent);
                PurchaseOrderManager manager = context.PurchaseOrderManager;

                // Read in the XML document that contains the changes
                // to make.

                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load("PurchaseOrderUpdates.xml");

                // Update the changed properties.
                manager.UpdatePurchaseOrderProperties(xmlDoc.DocumentElement, "Site");
            }
            catch (OrderNotSavedException ex)
            {
                Console.WriteLine("The purchase order was not saved to the database.");
                Console.ReadLine();
            }
            catch (ArgumentNullException ex)
            {
                Console.WriteLine("The application ID cannot be null.");
                Console.ReadLine();
            }
            catch (NotAuthorizedException ex)
            {
                Console.WriteLine("You do not have permission to update the database.");
                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();
            }
        }
    }
}

The following XML code shows the contents of the PurchaseOrderUpdates.xml file that the previous example references.

<?xml version="1.0" encoding="utf-8"?>
<PurchaseOrderUpdates>
  <PurchaseOrder 
    OrderGroupId="c3441414-4445-4daf-a4b0-13c369432d22" 
    PropertyName="Status" 
    PropertyValue="InProcess" />
  <PurchaseOrder 
    OrderGroupId="c3441414-4445-4daf-a4b0-13c369432d22" 
    PropertyName="ModifiedBy" 
    PropertyValue="TestApplication" />
  <PurchaseOrder 
    OrderGroupId="fe41d8b9-ff5b-47e0-b396-30fd5d5437b5" 
    PropertyName="Status" 
    PropertyValue="InProcess" />
  <PurchaseOrder 
    OrderGroupId="fe41d8b9-ff5b-47e0-b396-30fd5d5437b5" 
    PropertyName="ModifiedBy" 
    PropertyValue="TestApplication" />
  <LineItem 
    LineItemId="d301935d-b159-4a9c-92af-eaa82c858939" 
    PropertyName="Status" 
    PropertyValue="InProcess" />
  <LineItem 
    LineItemId="d301935d-b159-4a9c-92af-eaa82c858939" 
    PropertyName="ListPrice" 
    PropertyValue="789" />
</PurchaseOrderUpdates>

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

Copy the XML code provided in the previous section into a file, and save the file as PurchaseOrderUpdates.xml in the \bin\debug directory of your application.

See Also

Other Resources

How to Create an OrderManagementContext Object

Updating Purchase Orders