Jaa


How to Place an Order by Using XML

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

Commerce Server can process orders that did not place through your Web site. This is especially useful in business-to-business scenarios. The customer can create an XML file that represents the order to place, and call a service that you provide to process the order.

To place an order from an XML document

  1. Create a new OrderServiceAgent object, and provide the URL to the orders Web service.

    For more information about how to perform this step, see How to Create an OrderManagementContext Object.

  2. Create a new OrderManagementContext object, and pass the OrderServiceAgent object to the Create method of the OrderManagementContext object.

  3. Get the BasketManager object from the OrderManagementContext object.

  4. Create an XmlDocument object, and load the XML file that represents the order into this object.

  5. Call the AcceptBasket method of the BasketManager object to process the order, and pass the DocumentElement member of the XmlDocument object as a parameter.

Example

The following code example creates a purchase order from an XML document.

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

namespace CS2007PipelineConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                // In the following code, replace the URL with the URL 
                // to your orders Web service.
                OrderServiceAgent ordersAgent = new OrderServiceAgent("https://localhost/ordersWebService/ordersWebService.asmx");
                OrderManagementContext context = OrderManagementContext.Create(ordersAgent);
                BasketManager basketManager = context.BasketManager;

                // Load the XML file that represents the order.
                XmlDocument doc = new XmlDocument();
                doc.Load(@"Basket.xml");

                // Call the AcceptBasket method with a third parameter
                // whose value is true to save the basket as a purchase
                // order.
                XmlElement poXml = basketManager.AcceptBasket(doc.DocumentElement, "acceptbasket", true);

                // Write the XML representation of the purchase order
                // to the AcceptBasketResult.xml file.
                XmlTextWriter writer = new XmlTextWriter("AcceptBasketResult.xml", System.Text.Encoding.UTF8);
                writer.Formatting = Formatting.Indented;
                writer.WriteStartDocument();
                poXml.WriteTo(writer);
                writer.WriteEndDocument();
                writer.Flush();
            }
            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();
            }
        }
    }
}

The following XML code shows the contents of the Basket.xml file that the previous example references.

Copy the XML code into a file, and save the file as Basket.xml in the \bin\debug directory of your application. Replace the following values in the XML file with valid values for your application:

  • PaymentMethodId

  • ShippingMethodId

  • ShippingAddressId

  • ProductId

  • VariantId

  • CatalogName

  • SoldToId

Replace the value of the OrderGroupId attribute of the Basket element with a new GUID every time that you run the example.

<?xml version="1.0" encoding="utf-8"?>
<Basket LastModified="2004-11-07T11:25:08.7333579-08:00" SubTotal="0" Created="2004-11-07T11:25:08.311491-08:00" StatusCode="Basket" Name="myBasket - 3cfab1d1-ad22-44cf-8196-ffda22078294" BillingCurrency="" OrderGroupId="e88434dc-f15b-43dd-9010-33cb675f6701" ModifiedBy="" BasketID="3cfab1d1-ad22-44cf-8196-ffda22078294" HandlingTotal="0" ShippingTotal="0" LineItemCount="3" Total="0" Status="Submitted" TaxTotal="0" SoldToId="3cfab1d1-ad22-44cf-8196-ffda22078294" SoldToName="">
  <OrderForms>
    <OrderForm OrderGroupId="e88434dc-f15b-43dd-9010-33cb675f6701" TaxTotal="0" LastModified="2004-11-07T11:25:08.7333579-08:00" Created="2004-11-07T11:25:08.311491-08:00" SubTotal="0" HandlingTotal="0" ShippingTotal="0" ModifiedBy="" BillingAddressId="" Total="0" OrderFormId="A318641E-21A3-4ad8-B317-062F65EFAEF1" Name="default">
      <PromoCodeRecords>
          <PromoCodeRecord OrderGroupId="e88434dc-f15b-43dd-9010-33cb675f6701" PromoCode="Promocode1" PromoCodeLookupDate="2004-11-07T11:25:10.811443-08:00" PromoCodeStatus="OK" PromoCodeDefinitionID="1" PromoCodeReserved="true" PromoApplied="true" OrderFormID="8a932732-0393-4bac-b234-abfd6a48dddb">
                <WeaklyTypedProperties>
                    <WeaklyTypedProperty Name="serializableProperty" Type="String" Value="Serialize this!" />
                    <WeaklyTypedProperty Name="Line3" Type="String" Value="address line 2 - home" />
                </WeaklyTypedProperties>
          </PromoCodeRecord>
          <PromoCodeRecord OrderGroupId="e88434dc-f15b-43dd-9010-33cb675f6701" PromoCode="Promocode2" PromoCodeLookupDate="2004-11-07T11:25:10.811443-08:00" PromoCodeStatus="OK" PromoCodeDefinitionID="2" PromoCodeReserved="true" PromoApplied="true" OrderFormID="8a932732-0393-4bac-b234-abfd6a48dddb">
              <WeaklyTypedProperties>
                    <WeaklyTypedProperty Name="serializableProperty" Type="String" Value="Serialize this!" />
                    <WeaklyTypedProperty Name="Line3" Type="String" Value="address line 2 - home" />
                </WeaklyTypedProperties>
          </PromoCodeRecord>
      </PromoCodeRecords>
        <Payments>
            <CreditCardPayment Status="" ValidationCode="505" PaymentMethodId="00000000-0000-0000-0000-004580001161" PaymentType="CreditCard" AuthorizationCode="C78TX09" ExpirationMonth="11" ExpirationYear="2006" CardType="VISA" PaymentMethodName="VISA CreditCard Payment" PaymentID="5B84682E-56A1-4112-B6A9-8332E0069D0A" BillingAddressID="24b4b83a-7198-433c-a4a3-4ac20d1eeb1f" Amount="0" CreditCardNumber="4111111111111111" OrderGroupId="e88434dc-f15b-43dd-9010-33cb675f6701" OrderFormID="8a932732-0393-4bac-b234-abfd6a48dddb">
                <WeaklyTypedProperties/>
            </CreditCardPayment>
        </Payments>
      <Shipments>
          <Shipment ShippingMethodID="00000000-0000-0000-0000-004580001161" OrderGroupId="e88434dc-f15b-43dd-9010-33cb675f6701" ShipmentID="FF9B4D5F-57C5-405d-B32C-7238AB30EB2A" ShipmentTotal="10" Status="InProgress" ShipmentTrackingNumber="1000" ShippingAddressID="24b4b83a-7198-433c-a4a3-4ac20d1eeb1f" OrderFormID="8a932732-0393-4bac-b234-abfd6a48dddb">
              <LineItemIndexes>
                  <AnyType Type="Int32" Value="0"/>
                  <AnyType Type="Int32" Value="1"/>
                  <AnyType Type="Int32" Value="2"/>
              </LineItemIndexes>
              <ShippingDiscounts>
                <ShippingDiscountRecord DiscountId="789" LastModified="2004-11-07T11:25:10.8270677-08:00" Priority="0" DiscountAmount="0" DiscountValue="0" TypeOfDiscount="CurrencyValue" ShipmentID="FF9B4D5F-57C5-405d-B32C-7238AB30EB2A" OrderGroupId="058a1983-6aa0-41b7-9d26-5ac554fcaccf" DiscountName="ShippingDiscount1" PromoCodeDefinitionId="0">
                    <WeaklyTypedProperties>
                        <WeaklyTypedProperty Name="serializableProperty" Type="String" Value="Serialize this!" />
                    </WeaklyTypedProperties>
               </ShippingDiscountRecord>
               <ShippingDiscountRecord DiscountId="1011" LastModified="2004-11-07T11:25:10.8270677-08:00" Priority="0" DiscountAmount="0" DiscountValue="0" TypeOfDiscount="CurrencyValue" ShipmentID="FF9B4D5F-57C5-405d-B32C-7238AB30EB2A" OrderGroupId="058a1983-6aa0-41b7-9d26-5ac554fcaccf" DiscountName="ShippingDiscount2" PromoCodeDefinitionId="0">
                    <WeaklyTypedProperties>
                        <WeaklyTypedProperty Name="serializableProperty" Type="String" Value="Serialize this!" />
                    </WeaklyTypedProperties>
               </ShippingDiscountRecord>
              </ShippingDiscounts>
              <WeaklyTypedProperties>
                <WeaklyTypedProperty Name="serializableProperty" Type="String" Value="Serialize this!" />
                <WeaklyTypedProperty Name="Line3" Type="String" Value="address line 2 - home" />
            </WeaklyTypedProperties>
          </Shipment>
      </Shipments>
      <PromoCodes />
      <LineItems>
        <LineItem ProductCategory="crampons" ShippingMethodName="Acme Shipping" Created="2004-11-07T11:25:08.3896145-08:00" ProductId="AW109-15" PlacedPrice="0" InventoryCondition="InStock" OrderFormId="A318641E-21A3-4ad8-B317-062F65EFAEF1" ShippingAddressId="" AllowBackordersAndPreorders="true" LastModified="2004-11-07T11:25:08.7177332-08:00" OrderGroupId="e88434dc-f15b-43dd-9010-33cb675f6701" ListPrice="0" Quantity="3" DisplayName="Wafflestomper" ModifiedBy="" ProductCatalog="Adventure Works Catalog" ShippingMethodId="00000000-0000-0000-0000-004580001161" OrderLevelDiscountAmount="0" PreorderQuantity="0" ExtendedPrice="0" BackorderQuantity="0" LineItemDiscountAmount="0" InStockQuantity="0" LineItemId="EE31B820-74B9-47f9-911F-F5BC18CE2EF6">
          <ItemLevelDiscountsApplied />
          <OrderLevelDiscountsApplied />
          <WeaklyTypedProperties>
            <WeaklyTypedProperty Name="serializableProperty" Type="String" Value="Serialize this!" />
          </WeaklyTypedProperties>
        </LineItem>
        <LineItem ProductCategory="crampons" ShippingMethodName="Acme Shipping" Created="2004-11-07T11:25:08.3896145-08:00" ProductId="AW145-15" PlacedPrice="0" InventoryCondition="InStock" OrderFormId="A318641E-21A3-4ad8-B317-062F65EFAEF1" ShippingAddressId="" AllowBackordersAndPreorders="true" LastModified="2004-11-07T11:25:08.7177332-08:00" OrderGroupId="e88434dc-f15b-43dd-9010-33cb675f6701" ListPrice="0" Quantity="1" DisplayName="Edgehugger" ModifiedBy="" ProductCatalog="Adventure Works Catalog" ShippingMethodId="00000000-0000-0000-0000-004580001161" OrderLevelDiscountAmount="0" PreorderQuantity="0" ExtendedPrice="0" BackorderQuantity="0" LineItemDiscountAmount="0" InStockQuantity="0" LineItemId="20B0CEB3-D442-4d08-9E3E-AF6DD892E116">
          <ItemLevelDiscountsApplied />
          <OrderLevelDiscountsApplied />
          <WeaklyTypedProperties>
            <WeaklyTypedProperty Name="serializableProperty" Type="String" Value="Serialize this!" />
          </WeaklyTypedProperties>
        </LineItem>
      </LineItems>
      <WeaklyTypedProperties>
        <WeaklyTypedProperty Name="serializableProperty" Type="String" Value="Serialize this!" />
<WeaklyTypedProperty Name="MyDateTimeProperty" Type="DateTime" Value="2005-08-08T00:15:45.7372856Z" />
      </WeaklyTypedProperties>
    </OrderForm>
  </OrderForms>
  <Addresses>
    <OrderAddress Email="customero@example.com" State="state" Organization="org" EveningPhoneNumber="(425) 222-2222" CountryCode="US" FaxNumber="(425) 333-3333" DaytimePhoneNumber="(425) 111-1111" OrderAddressId="24b4b83a-7198-433c-a4a3-4ac20d1eeb1f" City="city" RegionName="Puget" FirstName="first" CountryName="country" RegionCode="Pu" Line1="line 1" Line2="line 2" LastName="last" OrderGroupId="e88434dc-f15b-43dd-9010-33cb675f6701" PostalCode="90210" Name="&amp;hard~!@and&amp;&amp;and#$%^&amp;*()&amp;">
      <WeaklyTypedProperties>
        <WeaklyTypedProperty Name="serializableProperty" Type="String" Value="Serialize this!" />
        <WeaklyTypedProperty Name="Line3" Type="String" Value="address line 2 - home" />
      </WeaklyTypedProperties>
    </OrderAddress>
  </Addresses>
  <WeaklyTypedProperties>
    <WeaklyTypedProperty Name="serializableProperty" Type="String" Value="Serialize this!" />
    <WeaklyTypedProperty Name="MyCharProperty" Type="Char" Value="34" />
<WeaklyTypedProperty Name="MyDateTimeProperty" Type="DateTime" Value="2005-08-08T00:15:45.7372856Z" />
</WeaklyTypedProperties>
</Basket>

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 Process Basket Punch-Outs

Working with Orders Data Management Objects