Jaa


How to Retrieve All Shipping Methods

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

A shipping method is a way that items can be shipped to a customer. "Ground" and "Overnight" are examples of shipping methods. You can retrieve all the shipping methods that are configured for the site from the ShippingMethodCollection member of the ShippingMethodManager object.

To retrieve all shipping methods

  1. Create an OrderManagementContext object.

    For more information about how to create an OrderManagementContext object, see How to Create an OrderManagementContext Object.

  2. Get the ShippingMethodManager object from the OrderManagementContext object.

  3. Get the ShippingMethodCollection object from the ShippingMethodManager.

Example

The following code example displays the name and ID of each shipping method.

using System;
using System.Data;
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);

                // Get the ShippingMethodManager object.
                ShippingMethodManager manager = context.ShippingMethodManager;

                // Get the collection of all shipping methods.
                ShippingMethodCollection shippingMethods = manager.ShippingMethods;

                // Retrieve and display information about each
                // shipping method.
                foreach (ShippingMethod method in shippingMethods)
                {
                    string methodName = method.GetShippingMethodName();
                    Guid methodId = method.ShippingGroupId;
                    Console.WriteLine("Name: {0}     ID: {1}", methodName, methodId);
                }
                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();
            }
        }
    }
}

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

Orders Data Management Object Model

Working with Orders Data Management Objects