Search Purchase Orders in MS Commerce Server 2007

MS Commerce Server provides an OrderManagementContext object which is a starting point for performing data management operations on all objects in the orders system. We must create OrderManagementContext object before we access any others object in the Microsoft.CommerceServer.Orders namespace.

Following are steps which guide how to create ans use OrderManagementContext object.

Step1: Add following references in your visual studio project

* Microsoft.CommerceServer.CrossTierTypes
* Microsoft.CommerceServer.Orders.CrossTierTypes

Step2: Copy paste the below sample code to get the Purchase Orders (Note: Modify the fields as per your requirements)

using System;
using System.Data;
using System.Globalization;
using Microsoft.CommerceServer;
using Microsoft.CommerceServer.Orders;

namespace MS.CS.OrdersManagement
{
public class Orders
{
//return dataset of PurchaseOrders
public DataSet GetPurchaseOrders()
{
//Create object of OrderServiceAgent which will be used to create OrderManagementContext object
string orderWebServiceURL = “http://localhost/OrdersWebService/OrdersWebService.asmx”;
OrderServiceAgent ordersAgent = new OrderServiceAgent(orderWebServiceURL);
// or create OrderSiteAgent object. In the following, replace “StarterSite” with the name of your site.
// Faster but code must run in local server.
// OrderSiteAgent ordersAgent = new OrderSiteAgent(“StarterSite”);

//Create OrderManagementContext object using OrderAgent
OrderManagementContext orderManagementContext = OrderManagementContext.Create(ordersAgent);

//Create the instance of the PurchaseOrderManager used to retrieve and store the PurchaseOrder in Commerce Server.
PurchaseOrderManager purchaseOrderManager = orderManagementContext.PurchaseOrderManager;

//Create a search clause to find purchase orders whose Product name is Boot
DataSet searchableProperties = purchaseOrderManager.GetSearchableProperties (CultureInfo.CurrentUICulture.ToString());
SearchClauseFactory searchClauseFactory = purchaseOrderManager.GetSearchClauseFactory(searchableProperties, “PurchaseOrder”);
SearchClause productClause = searchClauseFactory.CreateClause(ExplicitComparisonOperator.Equal, “ProductName”, “Boot”);

//Specify the optional parameters to control the search results
SearchOptions options = new SearchOptions();

//specify the properties you want to return by the DataSet
options.PropertiesToReturn = “ProductName, Created, OrderGroupId, Total, LineItemCount, OrderFormId, SoldToName, SoldToId”;

//Specify the property by which the result will be sorted
options.SortProperties =”Created”;

//Specify the maximum number of records to return
options.NumberOfRecordsToReturn = 100;
//Get the DataSet as result by passing the parameters to the SearchPurchaseOrders() method
DataSet purchaseOrders = purchaseOrderManager.SearchPurchaseOrders(productClause, options);

return purchaseOrders;

}
}
}

%d bloggers like this: