Share via


How to Retrieve a User's PurchaseOrder or PurchaseOrders

There are two ways to retrieve the PurchaseOrder or PurchaseOrders for a user:

In both cases, you must know the user ID of the owner of the PurchaseOrder.

To retrieve a user's PurchaseOrder or PurchaseOrders

  1. In Visual Studio, create a new Commerce Server Core Systems Web application.

  2. Add the Microsoft.CommerceServer.Runtime reference to the Web application.

  3. Add a using directive for the Microsoft.CommerceServer.Runtime.Orders namespace.

  4. Obtain the user ID of the current user (or the user ID of any user whose PurchaseOrder you want to retrieve.)

  5. Call the GetPurchaseOrder method to retrieve a single PurchaseOrder, or call the GetPurchaseOrdersForUser method to retrieve all of the user's PurchaseOrders.

Example

The first code example shows how to retrieve a user's PurchaseOrder by providing a specific tracking number.

When you save a PurchaseOrder, Commerce Server Core Systems assigns a tracking number to the PurchaseOrder. It is common practice to provide the tracking number to the customer. Since we need the tracking number to get the PurchaseOrder, this example saves tracking number after calling SaveAsOrder, then uses the saved tracking number to get the PurchaseOrder again. In a real Commerce Server Core Systems application you would get the tracking number from the user instead. 

The second code example shows how to retrieve all of a user's PurchaseOrders.

The example begins by creating a user ID, creating two Baskets for the user, and then saving the Baskets as PurchaseOrders. The example next retrieves the user's PurchaseOrders, and displays the name of each PurchaseOrder.

There is an overloaded version of the GetPurchaseOrdersForUser method that returns all of a user's PurchaseOrders within a given range of dates.

//Code Example 1
using System;
using System.Collections;
using System.Web;
using Microsoft.CommerceServer.Runtime.Orders;

public partial class Default_aspx : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        // For this example, create a new GUID for the user's ID.  
        // If this were part of a full Commerce Server site that
        // implemented logons, we could get the ID of the current
        // user from the CommerceContext.

        Guid userID = Guid.NewGuid();

        // Create a new Basket for the user.

        Basket myBasket = OrderContext.Current.GetBasket(userID, "default");

        // In a full Commerce Server application you would add
        // OrderForms, LineItems, Payments, and so on to the Basket, 
        // and then run the Basket, Payment, and Checkout pipeline
        // on the Basket before you converted the Basket to a
        // PurchaseOrder. In this small sample we'll just convert the 
        // Basket directly to a PurchaseOrder.

        PurchaseOrder myPurchaseOrder = myBasket.SaveAsOrder();

        // Commerce Server assigned a tracking number when we saved
        // the Basket as a PurchaseOrder. In a real application, we
        // would provide this number to the customer, and the customer
        // would provide the tracking number when inquiring about the
        // PurchaseOrder. For this example, we'll just store the tracking
        // number so that we can retrieve the correct PurchaseOrder.

        string myTrackingNumber = myPurchaseOrder.TrackingNumber;

        // Now retrieve the PurchaseOrder, and display its name.

        PurchaseOrder retrievedPurchaseOrder = OrderContext.Current.GetPurchaseOrder(userID, myTrackingNumber);
        Response.Write("PurchaseOrder name: " + retrievedPurchaseOrder.Name + "<br>");

    } // end Page_Load method
} // end Default_aspx class

//Code Example 2
using System;
using System.Collections;
using System.Web;
using Microsoft.CommerceServer.Runtime.Orders;

public partial class Default_aspx : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        // For this example, create a new GUID for the user's ID.  
        // If this were part of a full Commerce Server site that
        // implemented logons, we could get the ID of the current
        // user from the CommerceContext.

        Guid userID = Guid.NewGuid();

        // Create two new Baskets for the user.

        Basket myBasket1 = OrderContext.Current.GetBasket(userID, "default");
        Basket myBasket2 = OrderContext.Current.GetBasket(userID, "gifts");

        // In a full Commerce Server application you would add
        // OrderForms, LineItems, Payments, and so on to the Baskets, 
        // and then run the Basket, Payment, and Checkout pipeline
        // on the Baskets before you converted the Baskets to 
        // PurchaseOrders. In this small sample we'll just convert the 
        // Baskets directly to PurchaseOrders.

        PurchaseOrder myPurchaseOrder1 = myBasket1.SaveAsOrder();
        PurchaseOrder myPurchaseOrder2 = myBasket2.SaveAsOrder();

        // Retrieve all of the user's PurchaseOrders, and display the
        // name of each PurchaseOrder.

        OrderGroupCollection allPurchaseOrders = OrderContext.Current.GetPurchaseOrdersForUser(userID);

        foreach (PurchaseOrder currentPO in allPurchaseOrders)
        {
            Response.Write("PurchaseOrder name: " + currentPO.Name + "<br>");
        } // end foreach
    } // end Page_Load method

} // end Default_aspx class

See Also

Reference

GetPurchaseOrdersForUser

GetPurchaseOrder

Other Resources

Working with PurchaseOrders

Orders Runtime Object Model