Sitecore OrderCloud SDK Client Library for .NET

The Sitecore OrderCloud is an API-first and headless eCommerce platform, so developing OrderCloud based application is very different compared to the traditional Sitecore development. 

The Sitecore OrderCloud SDK for .NET is a client library for building solutions targeting the eCommerce platform using C# or other .NET language. 

You will probably always need this .NET SDK library in order to build any OrderCloud based application.

In this blog, I'll walk you through the use of this SDK for development using .NET Console application where we'll try to fetch the orders from OrderCloud portal.

Let's get started.

Assumptions

You have already setup your OrderCloud sandbox environment and configured Admin and Buyer users security profiles. Like this - 

You have also created one API Client and have the access to the Sellers and Buyers. Please refer this screenshot - 

Now, let's create some dummy orders in OrderCloud using API Console or Postman. We'll fetch these orders in our console application. 

In this example, I have 5 orders (Open + Completed), will fetch all these orders using console application.


  • Let's create one console application.
  • Download the OrderCloud .NET SDK Library from here.
  • Copy this SDK to your console application project folder.
  • Add the SDK project reference to your console application.

  • Create one OrderCloudConfig.cs file in your console application. We will keep some configuration settings in this file. However in real project these configurations may be stored at different location or config file and load dynamically.

 OrderCloudConfig.cs code snippet:

internal class OrderCloudConfig
{
    internal const string CLIENT_ID = "XXXXX-XXXX-XXXX-XXXX-XXXXXX";
    internal const string USER_NAME = "ADMIN_USER";
    internal const string PASSWORD = "ADMIN_PASSWORD";
    internal const string API_URL = "SANDBOX_API_URL";
    internal const string AUTH_URL = "AUTH_URL";

}
  • Now open your Program.cs file and copy paste the following code -
using OrderCloud.Console;
using OrderCloud.SDK;


var client = new OrderCloudClient(new OrderCloudClientConfig
{
    ClientId = OrderCloudConfig.CLIENT_ID,

    //This is required for password grant flow:
    Username = OrderCloudConfig.USER_NAME,
    Password = OrderCloudConfig.PASSWORD,

    Roles = new[] { ApiRole.OrderAdmin },
    ApiUrl = OrderCloudConfig.API_URL,
    AuthUrl = OrderCloudConfig.AUTH_URL,
});

var orders = await client.Orders.ListAsync(OrderDirection.Incoming);

Console.WriteLine($"{orders.Meta.TotalCount} orders found.");
Console.WriteLine($"Fetched page {orders.Meta.Page} of {orders.Meta.TotalPages}.");

foreach (var order in orders.Items)
{
    Console.WriteLine($"Order ID: {order.ID}, Order Total: {order.Total:C}, Order Status: {order.Status}");
}
That's it!! Now run you console application. If you have followed all the above steps you should see the orders in the console output window. Something like this - 


Thank you!

Comments

Popular posts from this blog

Setup Sitecore XM Cloud Dev Environment using Docker

Sitecore Content Hub - Triggers, Actions and Scripts

All Blog Posts - 2023