Work with the Orders API (C#)

Version: 10.3

The Orders service provides a RESTful, OData-compliant API, that you can use to integrate with other business systems to exchange order-related data. This topic provides examples of how you can use the Orders API to build Commerce integrations using the C# programming language.

Note

The C# code examples contained in this topic are based on the Sitecore.Commerce.Sample.Console samples that are packaged in the Sitecore Commerce Engine Software Development Kit (Sitecore.Commerce.Engine.SDK).

You must include Commerce Engine operations and commands within a container call (using C#) as shown in this example.

To execute any operation in the Sitecore Experience Commerce (XC) system, the calling system must first obtain a valid bearer authorization token from the Sitecore Identity server.

Note

You can perform other operations programmatically, for example to work with orders fulfillment or order payments, by using the Orders Views and Actions API .

Create an order (C#)

You can use the Orders API to create an order when the shopping cart has all necessary items, fulfillment, and payment information.

To create an order, use the CreateOrder query. The query must provide the shopping cart ID (cartId) and the shopper's email address, for example:

RequestResponse
Container container = AuthoringContainer();
DataServiceActionQuerySingle<CommerceCommand> actionQuery = container.CreateOrder("cartId", "[email protected]");
CommerceCommand command = Proxy.DoCommand(actionQuery);
string orderId = command.Models.OfType<CreatedOrder>().FirstOrDefault().OrderId;

Cancel an order (C#)

You can use the Orders API to cancel an order when the order status is either pending or on hold.

To cancel an order:

RequestResponse
Container container = AuthoringContainer();
DataServiceActionQuerySingle<CommerceCommand> actionQuery = container.CancelOrder(orderId);
CommerceCommand command = Proxy.DoCommand(actionQuery);

Set the order to a custom status (C#)

You can set an order to a custom status.

Important

By default, the Commerce Orders flow is based on the orders status. The status of the order determines the next action to take on the order in the end-to-end process flow.  Keep in mind that the introduction of a custom order status impacts the way commerce orders normally flow through the system.

To set the order to a customer status using C#:

RequestResponse
DataServiceActionQuerySingle<CommerceCommand> actionQuery = container.SetOrderStatus(orderId, "CustomStatus");
CommerceCommand command = Proxy.DoCommand(actionQuery);

Get sales activities from an order (C#)

Sitecore XC generates sales activities every time an order's payment information is updated.

To retrieve the sales activities for an order using C#:

RequestResponse
DataServiceQuery<SalesActivity> actionQuery = container.GetOrderSalesActivities(orderId);
IEnumerable<SaleActivity> salesActivities = Proxy.Execute(actionQuery);

Do you have some feedback for us?

If you have suggestions for improving this article,