How to Create a Basket

To create a new Basket, use the GetBasket method on the OrderContext object. When you create a Basket, you must indicate the user who owns the Basket. You can optionally provide a name for the Basket. Named Baskets are useful when a single user can have multiple Baskets -- for example, a current basket and a wish list.

Note

If a Basket with the same name and User ID already exists, the GetBasket method returns the existing Basket instead of creating a new Basket.

To create a Basket

  1. Create a new 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 create the Basket.

Example

The following code shows an example of how to create a new Basket from your site code. This example creates a Basket that is not for a specific user by passing a new Guid for the user ID. In a full Commerce Server application, you would get the ID of the current user and pass the ID to the GetBasket method to create a Basket for the specific user.

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)
    {
        // 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();
        String basketName = "default";

        // Create a new Basket.

        Basket newBasket = OrderContext.Current.GetBasket(userID, basketName);

        // Validate that we created a new Basket by writing some
        // information about it to the Web page.

        String details = "user ID: " + newBasket.SoldToId + "  name: " + newBasket.Name;
        Response.Write(details);
    } // end Page_Load
} //end Default_aspx

See Also

Reference

GetBasket

Other Resources

Working with Baskets

How to Add Items to a Basket

Orders Runtime Object Model