Share via


Sample Basket Search Implementation

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

The following code example illustrates how to search the orders database and retrieve baskets that were last modified on January 1, 2006 or later and contain more than one item.

Example

Code

using System;
using System.Data;
using System.Globalization;
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 statements, replace "StarterSite" 
                // with the name of your site.
                OrderSiteAgent ordersAgent = new OrderSiteAgent("StarterSite");
                OrderManagementContext context = OrderManagementContext.Create(ordersAgent);
                BasketManager manager = context.BasketManager;

                // Create a search clause to find baskets that have
                // not been modified since January 1, 2006 and that
                // contain two or more distinct items.
                DataSet searchableProperties = manager.GetSearchableProperties(CultureInfo.CurrentUICulture.ToString());
                SearchClauseFactory searchClauseFactory = manager.GetSearchClauseFactory(searchableProperties, "Basket");
                SearchClause lastModifiedClause = searchClauseFactory.CreateClause(ExplicitComparisonOperator.OnOrAfter, 
                    "LastModified", new DateTime(2006, 1, 1));
                SearchClause lineItemClause = searchClauseFactory.CreateClause(ExplicitComparisonOperator.GreaterThan,
                    "LineItemCount", 1);
                SearchClause combinedClause = searchClauseFactory.IntersectClauses(lastModifiedClause, lineItemClause);

                // Create search options.
                SearchOptions options = new SearchOptions();
                options.PropertiesToReturn = "LastModified, Created, SubTotal";
                options.SortProperties = "Created";
                options.NumberOfRecordsToReturn = 500;

                // Perform the search.
                DataSet results = manager.SearchBaskets(combinedClause, options);

                // Enumerate the results of the search.
                foreach (DataTable table in results.Tables)
                {
                    Console.WriteLine("Table: " + table.TableName);
                    foreach (DataRow row in table.Rows)
                    {
                        Console.WriteLine("  Row");
                        foreach (DataColumn column in table.Columns)
                        {
                            Console.WriteLine("    " + column.ColumnName + ": " + row[column]);
                        }
                    }
                }
                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();
            }
        }
    }
}

Comments

Your database must contain baskets that were created on or after January 1, 2006 and that have two or more line items for this example to produce any output.

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

How to Search for Baskets

Searching the Orders Database