Understanding Products And Fetching The Products in Console Application in Sitecore OrderCloud
Hello everyone, In this blog we will see how we can create a product in Sitecore OrderCloud. Additionally, we will see how to fetch that product in console application. This usecase will be using Buyer User.
To start, lets quickly navigate OrderCloud Portal. To create a product and assign that product to the user, we need to follow some steps listed below:
1) Creating a product: Navigate to Product section in OrderCloud portal. See the below screenshot for your reference.You can directly copy the JSON payload as shown and can submit it rather than filling individual fields.
2) Assigning Product to Catalog:Once product is created assign that product to Catalog by navigating to Catalog section.
Note : Now Check, whether product is assigned properly to Catalog. Your Catalog should be in active state or else your product will not be shown to buyer user. Also your Catalog should be assigned to your Buyer Organization where that user is present.
3) Verify Product visible for that user : Navigate to Me and My stuff section in left panel. Click on My Product section and give a get call as shown below. You should see the product now in the list for that user. Note: Select the context user to the user which you have assigned the product.
4) Fetching Products in Console application:
Create a console application and add reference of OrderCloud.SDK using Nuget manager as show below.
Now copy the below code and run the application. You should see the ouput in console. We are using Password Grant Type Workflow. We will be requiring Client Credentials(Get from API Client section under Seller in portal), UserName(For which user you are trying to fetch the product), Password(For which user you are trying to fetch the product), ApiURL(Your Sandbox URL) and AuthURl(Your Sandbox URL). Just check the referece section for more detail.
OrderCloudConfig.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace OrderCloudConsoleApp
{
internal class OrderCloudConfig
{
internal const string CLIENT_ID = "##############################";
internal const string USER_NAME = "#############################";
internal const string PASSWORD = "############################";
internal const string API_URL = "###########################";
internal const string AUTH_URL = "##########################";
}
}
Program.cs
using OrderCloud.SDK;
using OrderCloudConsoleApp;
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.Shopper,ApiRole.MeAdmin,ApiRole.ProductFacetReader,ApiRole.ProductReader},
ApiUrl = OrderCloudConfig.API_URL,
AuthUrl = OrderCloudConfig.AUTH_URL,
});
try
{
// Get all product list
// var productlist = await client.Products.ListAsync();
//Get ALL Product with Filter catalog
//var productlist = await client.Products.ListAsync(
// filters: new
// {
// catalogID = "Catalog2",
// }
//);
//Get Specific User ProductList
var productlist = await client.Me.ListProductsAsync();
Console.WriteLine($"{productlist.Meta.TotalCount} product found.");
Console.WriteLine($"Product Page {productlist.Meta.Page} of {productlist.Meta.TotalPages}.");
foreach (var product in productlist.Items)
{
Console.WriteLine($"Product ID: {product.ID}, Product Name: {product.Name}");
}
Console.Read();
}
catch(Exception ex)
{
Console.WriteLine(ex.ToString());
Console.Read();
}
Final Output:
Note: If your are getting in sufficient role error, check if the user is having appropriate access in portal.Check below screenshot
Thanks for reading!!. Happy learning.
You can check my other blogs too if interested. Blog Website
References:
Comments
Post a Comment