The tip given below explains how we can cancel the PurchaseOrder of a particular user by his/her Email Address
First of all we need to create the instance of the ProfileManagementContext using ProfilesWebServiceURL, instance of OrderManagementContext using the OrderSiteAgent & an instance of OrderContext. ProfileManagementContext profileManagementContext = ProfileManagementContext.Create(" http://localhost/MindfireProfilesWebService/ProfilesWebService.asmx"); OrderSiteAgent orderSiteAgent = new OrderSiteAgent ("Mindfire"); OrderManagementContext orderManagementContext = OrderManagementContext.Create(orderSiteAgent); OrderContext orderContext = OrderContext.Create("Mindfire");
To get the UserID first we need to create the search clause using the explicit operator to the User’s EmailAddress and pass it as one of the arguments to the method ExecuteSearch() of ProfileManagementContext. By the result of this search we can get the UserId as a DataSet for the specified Email Address
DataSet searchableEntities = profileManagementContext.GetSearchableEntities(); SearchClauseFactory searchClauseFactory = profileManagementContext. GetSearchClauseFactory(searchableEntities, "UserObject"); SearchClause searchClause = searchClauseFactory. CreateClause(ExplicitComparisonOperator.Equal, "email_address", emailId); //Mentioned the Search Options here Microsoft.CommerceServer.SearchOptions options = new Microsoft.CommerceServer.SearchOptions(); options.PropertiesToReturn = "user_id, first_name, last_name, email_address"; options.SortProperties = "first_name"; DataSet userDataset = profileManagementContext.ExecuteSearch("UserObject", searchClause, options); string userId = userDataset .Tables[0].Rows[0]["user_id"].ToString();
Now we can get the the PurchaseOrder User by his UserId and get the OrderGroupId of the PurchaseOrder. Then get the PurchaseOrder DataSet by the OrderGroupId.
OrderGroupCollection orderGroupCollection= orderContext.GetPurchaseOrdersForUser(new Guid(userId)); DataSet purchaseOrderDataSet = purchaseOrderManager. GetPurchaseOrderAsDataSet(orderGroupCollection[0].OrderGroupId);
Finally,change the status of the PurchaseOrder to “Cancelled” as shown below and update the PurchaseOrder.
purchaseOrderDataSet .Tables["PurchaseOrder"].Rows[0]["Status"] = "Cancelled"; purchaseOrderManager.UpdatePurchaseOrder(purchaseOrderDataSet , "LOB Adapter");
Code Snippet
public void CancelOrder(string emailAddress) {
//Create OrderManagementContext & ProfileManagementContext instances:
ProfileManagementContext profileManagementContext = ProfileManagementContext.Create(" http://localhost/MindfireProfilesWebService/ProfilesWebService.asmx"); OrderSiteAgent orderSiteAgent = new OrderSiteAgent ("Mindfire"); OrderManagementContext orderManagementContext = OrderManagementContext.Create(orderSiteAgent); OrderContext orderContext = OrderContext.Create("Mindfire");
//Get the UserId by UserEmailAddress by searching:
DataSet searchableEntities = profileManagementContext.GetSearchableEntities(); SearchClauseFactory searchClauseFactory = profileManagementContext.GetSearchClauseFactory(searchableEntities, "UserObject"); SearchClause searchClause = searchClauseFactory.CreateClause(ExplicitComparisonOperator.Equal, "email_address", emailId); //Mentioned the Search Options here Microsoft.CommerceServer.SearchOptions options = new Microsoft.CommerceServer.SearchOptions(); options.PropertiesToReturn = "user_id, first_name, last_name, email_address"; options.SortProperties = "first_name"; DataSet userDataset = profileManagementContext.ExecuteSearch("UserObject", searchClause, options); string userId = userDataset .Tables[0].Rows[0]["user_id"].ToString();
//Get the PurchaseOrder as DataSet for the perticular UserId:
OrderGroupCollection orderGroupCollection= orderContext.GetPurchaseOrdersForUser(new Guid(userId)); DataSet purchaseOrderDataSet = purchaseOrderManager. GetPurchaseOrderAsDataSet(orderGroupCollection[0].OrderGroupId);
//Set its Status to Cancelled & Update:
purchaseOrderDataSet .Tables["PurchaseOrder"].Rows[0]["Status"] = "Cancelled"; purchaseOrderManager.UpdatePurchaseOrder(purchaseOrderDataSet , "LOB Adapter");
//Get the Updated PurchaseOrder as DataSet:
DataSet updatedPurchaseOrder = purchaseOrderManager. GetPurchaseOrderAsDataSet(orderGroupCollection[0].OrderGroupId); }