C# SDK Sample: Retrieve Avalara Item Settings
The following sample program will load all the items on an account and collect the Avalara specific settings from the item properties.
Â
using System;
using System.Collections.Generic;
using com.ultracart.admin.v2.Api;
using com.ultracart.admin.v2.Client;
namespace evolve
{
class Program
{
static void Main(string[] args)
{
// This is required. See https://www.ultracart.com/api/versioning.html
Configuration.Default.DefaultHeader.Add("X-UltraCart-Api-Version", "2017-03-01");
// ------------------------------------------------------------
// SIMPLE KEY AUTHENTICATION
// TODO: Make sure to specify your API key here
Configuration.Default.AddApiKey("x-ultracart-simple-key", "YOUR API KEY HERE");
// ------------------------------------------------------------
var apiInstance = new ItemApi();
var _limit = 200; // We're going to load the maximum amount of items per API call
var _sort = "merchant_item_id"; // Sorted by item id
var _expand = "properties"; // Expand the properties on the item which is where Avalara settings are stored
// Collect values in dictionaries and lists
Dictionary<string, string> itemIdToAvalaraTaxCodeMap = new Dictionary<string, string>();
Dictionary<string, string> itemIdToAvalaraTaxDescriptionMap = new Dictionary<string, string>();
List<string> itemIds = new List<string>();
for (var _offset = 0; ; _offset += _limit)
{
var result = apiInstance.GetItems(null, null, _limit, _offset, null, _sort, _expand, false);
// Was the API call successful?
if (result.Success == true)
{
for (var i = 0; i < result.Items.Count; i++) {
var item = result.Items[i];
// Collect the item id
itemIds.Add(item.MerchantItemId);
// Loop through properties and collect the ones we're interested in
if (item.Properties != null)
{
for (var j = 0; j < item.Properties.Count; j++)
{
var property = item.Properties[j];
if (String.Compare(property.Name, "avalaraTaxCode", true) == 0)
{
itemIdToAvalaraTaxCodeMap.Add(item.MerchantItemId, property.Value);
}
if (String.Compare(property.Name, "avalaraTaxCodeDescription", true) == 0)
{
itemIdToAvalaraTaxDescriptionMap.Add(item.MerchantItemId, property.Value);
}
}
}
}
// If we received less than the limit we're done.
if (result.Items.Count < _limit) break;
}
}
// Output for sample purposes.
for (var i = 0; i < itemIds.Count; i++)
{
var itemId = itemIds[i];
var taxCode = itemIdToAvalaraTaxCodeMap.ContainsKey(itemId) ? itemIdToAvalaraTaxCodeMap[itemId] : null;
var taxDescription = itemIdToAvalaraTaxDescriptionMap.ContainsKey(itemId) ? itemIdToAvalaraTaxDescriptionMap[itemId] : null;
Console.Out.WriteLine("ItemId: " + itemId + " - Tax Code: " + taxCode + " - Tax Description: " + taxDescription);
}
// Indicate completition
Console.Out.WriteLine("DONE");
// Prompt for input to make viewing in development environment easier.
Console.Out.WriteLine("Press ENTER to exit.");
Console.In.ReadLine();
}
}
}