다음을 통해 공유


How to Retrieve a User's Basket or Baskets

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

There are two ways to retrieve the Basket instances for a user.

  • If you know which Basket you want to retrieve, use the GetBasket method.

  • If you do not know which Basket you want to retrieve, or, if you want to retrieve all of the Baskets for a user, use the GetBasketsForUser method.

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

To retrieve a user's Basket or Baskets

  1. In Visual Studio, create a new Commerce Server 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 Basket you want to retrieve.)

  5. Call the GetBasket method to retrieve a single Basket, or call the GetBasketsForUser method to retrieve all of the user's Baskets.

Example

The following code shows an example of how to retrieve a user's Basket of wish list items.

The example begins by creating a user ID and then creating a Basket for the user, so that there is a Basket to retrieve. The example next retrieves the Basket, and displays the user ID of the Basket's owner along with the Basket's name.

using System;
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)
    {
        // Create a new Guid for the user's ID, so that we have a
        // user ID to pass in when we retrieve a Basket.
        // If this were part of a full CommerceServer site that
        // implemented logons, we could get the ID of the current
        // user from the CommerceContext.

        Guid userID = Guid.NewGuid();

        // Create a new Basket named "wish list".
        // Note that the GetBasket method returns a new Basket if
        // the Basket we requested doesn't exist.

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

        // Save the Basket.

        myBasket.Save();

        // Now get the same Basket back.

        Basket retrievedBasket = OrderContext.Current.GetBasket(userID, "wish list");

        // Confirm that we got the Basket back by displaying the
        // user ID and name of both the Basket we created and the 
        // Basket we retrieved.

        Response.Write("created basket name: " + myBasket.Name + "<br>");
        Response.Write("retrieved basket name: " + retrievedBasket.Name + "<br>");
        Response.Write("created basket owner: " + myBasket.SoldToId.ToString() + "<br>");
        Response.Write("retrieved basket owner: " + retrievedBasket.SoldToId.ToString() + "<br>");
    } // end Page_Load method
} // end Default_aspx class

The following code shows an example of how to retrieve all of a user's Baskets.

The example begins by creating a user ID and then creating three Baskets for the user, so that there are Baskets to retrieve. The example next retrieves the Baskets, and displays the name of each Basket.

using System;
using System.Web;
using System.Collections;
using Microsoft.CommerceServer.Runtime.Orders;

public partial class Default_aspx : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        // Create a new Guid for the user's ID, so that we have a
        // user ID to pass in to the GetBasket method.
        // If this were part of a full CommerceServer site that
        // implemented logons, we could get the ID of the current
        // user from the CommerceContext.

        Guid userID = Guid.NewGuid();

        // Create three new Baskets, each with a different name.
        // Note that the GetBasket method returns a new Basket if
        // the Basket we requested doesn't exist.

        Basket basket1 = OrderContext.Current.GetBasket(userID, "wish list");
        Basket basket2 = OrderContext.Current.GetBasket(userID, "primary");
        Basket basket3 = OrderContext.Current.GetBasket(userID, "holiday");

        // Save the Baskets.

        basket1.Save();
        basket2.Save();
        basket3.Save();

        // Now retrieve all of the user's Baskets.

        OrderGroupCollection allBaskets = OrderContext.Current.GetBasketsForUser(userID);

        // For each Basket we retrieved, print its name.

        foreach (Basket currentBasket in allBaskets)
        {
            Response.Write("basket name: " + currentBasket.Name + "<br>");
        } // end foreach
    } // end Page_Load method
} // end Default_aspx class

See Also

Reference

GetBasket

GetBasketsForUser

Other Resources

Working with Baskets

How to Add Items to a Basket

Orders Runtime Object Model