Sample Purchase Order Search Implementation

The following code example illustrates how to search the orders database and retrieve purchase orders whose total price is over $100 US and which were last modified on or after July 1, 2006.

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

                // Create a search clause to find purchase orders whose
                // total cost is greater than $100 US and which were
                // last modified on or after July 1, 2006.

                DataSet searchableProperties = manager.GetSearchableProperties(CultureInfo.CurrentUICulture.ToString());
                SearchClauseFactory searchClauseFactory = manager.GetSearchClauseFactory(searchableProperties, "PurchaseOrder");
                SearchClause costClause = searchClauseFactory.CreateClause(ExplicitComparisonOperator.GreaterThan, "Total", 100);
                SearchClause lastModifiedClause = searchClauseFactory.CreateClause(ExplicitComparisonOperator.OnOrAfter, 
                    "LastModified", new DateTime(2006, 7, 1));
                SearchClause combinedClause = searchClauseFactory.IntersectClauses(costClause, lastModifiedClause);

                // Create search options.

                SearchOptions options = new SearchOptions();
                options.PropertiesToReturn = "Total, LineItemCount, SoldToName, SoldToId";
                options.SortProperties = "Total";
                options.NumberOfRecordsToReturn = 500;

                // Perform the search.

                DataSet results = manager.SearchPurchaseOrders(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 purchase orders whose total price is greater than $100 US and which were modified on or after July 1, 2006 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 Purchase Orders

Searching the Orders Database