Products API Examples - C#

Overview

This document reflects over the aforementioned use cases of the Product API. All examples defined within demonstrate calls to the SOAP interface and are provided as is.

Requirements

The following examples demonstrate calls to the Products API in C#. .NET examples demonstrated here assume the use of a generated XML web service client. To generate such a client, a tool such as WSDL.exe can be used. A detailed description on this tool can be found on MSDN, but all you will need to do is point it at your site's WSDL description.

.NET examples utilise the Microsoft .NET Framework component LINQ

Retrieve a single Products' details.

// Instantiate the web service proxy.
ProductsAPI service = new ProductsAPI();

// User to authenticate with.
String username = "Testing";
// Corresponding password to authenticate with.
String password = "Sup94zex";

// Authenticate
ApiResponse authenticationResponse = service.Authenticate(username, password);

// Style code (SKU) to search the Products Store for.
String style = "BC-M005";

// Retrieve the product
ProductApiResponse retrieveResponse = 
	service.Retrieve(authenticationResponse.Message, style);

// Examine the ProductApiResponse for our product.
if (retrieveResponse.Data == null || retrieveResponse.Data.Count() == 0) {
	Console.WriteLine("Failed to retrieve style: {0}", style);
} else {
	// Extract the product from the response.
	Product product = retrieveResponse.Data.First();
}
			

List style codes for Products by term, catalogue and all (unfiltered).

// Instantiate the web service proxy.
ProductsAPI service = new ProductsAPI();

// User to authenticate with.
String username = "Testing";
// Corresponding password to authenticate with.
String password = "Sup94zex";

// Authenticate
ApiResponse authenticationResponse = service.Authenticate(username, password);

// A simple search of all the product Styles for the provided search term.
StringArrayApiResponse searchResponse = 
	service.Search(authenticationResponse.Message, "dress");
searchResponse.Data.AsParallel().ForAll(x => Console.WriteLine(x));

// List all style codes under the provided catalogue
StringArrayApiResponse listing =
	service.ListByCatalogue(authenticationResponse.Message, "Women");
listing.Data.AsParallel().ForAll(x => Console.WriteLine(x));

// Lists all product style codes
StringArrayApiResponse productListing =
	service.List(authenticationResponse.Message);
productListing.Data.AsParallel().ForAll(x => Console.WriteLine(x));
			

Add a new Product.

// Instantiate the web service proxy.
ProductsAPI service = new ProductsAPI();

// User to authenticate with.
String username = "Testing";
// Corresponding password to authenticate with.
String password = "Sup94zex";

// Authenticate
ApiResponse authenticationResponse = service.Authenticate(username, password);

// Create our Product object
Product product = new Product() {
	AvailableFrom = DateTime.Now,
	Catalogues = new String[] { "Womens" },
	ContentLocked = false,
	Cost = 56.95m,
	ForeignIdentity = Guid.NewGuid().ToString(),
	Items = new ProductItem[] { 
		new ProductItem() {
			AvailableFrom = DateTime.Now,
			Barcode = "9300000034267",
			Colour = "Black",
			ColourCode = "BLK",
			Cost = 56.95m,
			ForeignIdentity = Guid.NewGuid().ToString(),
			InStock = true,
			Price = 149.00m,
			Quantity = 100,
			Retail = 45.3m,
			Size = "Extra Large",
			SizeCode = "XL",
			Weight = 44d
		}
	},
	LongDescription = "<p>Relaxed Fit - Allows for movement.</p>",
	Manufacturer = "Pedro Luis",
	Price = 149.00m,
	ShortDescription = "100% Soft Merino Wool",
	Status = 0,
	Style = "LU0456-DarkGreen ",
	Supplier = "Default Supplier",
	Title = "Snap Dome Placket Swing Top ",
	Weight = 44d
};

// Add this product to the system
service.UpdateProduct(authenticationResponse.Message, product);
			

Update an existing Product.

// Instantiate the web service proxy.
ProductsAPI service = new ProductsAPI();

// User to authenticate with.
String username = "Testing";
// Corresponding password to authenticate with.
String password = "Sup94zex";

// Authenticate
ApiResponse authenticationResponse = service.Authenticate(username, password);

// Style code (SKU) to search the Products Store for.
String style = "BC-M005";

ProductApiResponse retrieveResponse =
	service.Retrieve(authenticationResponse.Message, style);

// Examine the ProductApiResponse for our product.
if (retrieveResponse.Data == null || retrieveResponse.Data.Count() == 0) {
	Console.WriteLine("Failed to retrieve style: {0}", style);
} else {
	// Extract the product from the response.
	Product product = retrieveResponse.Data.First();

	// A trivial update of one of the Product fields.
	product.ShortDescription = "Updated Short Description";
	// Update the product 
	// The Product Store will update the ShortDescription.
	service.UpdateProduct(authenticationResponse.Message, product);
}
			

Delete a Product from the database.

// Instantiate the web service proxy.
ProductsAPI service = new ProductsAPI();

// User to authenticate with.
String username = "Testing";
// Corresponding password to authenticate with.
String password = "Sup94zex";

// Authenticate
ApiResponse authenticationResponse = service.Authenticate(username, password);

// Product to delete in the system.
String style = "BC-M005";

// Delete the product from the system.
service.Delete(authenticationResponse.Message, style);
			

Add Product Items.

// Instantiate the web service proxy.
ProductsAPI service = new ProductsAPI();

// User to authenticate with.
String username = "Testing";
// Corresponding password to authenticate with.
String password = "Sup94zex";

// Authenticate
ApiResponse authenticationResponse = service.Authenticate(username, password);

ProductItem item = new ProductItem() {
	AvailableFrom = DateTime.Now,
	Barcode = "9300000034268",
	Colour = "Orange",
	ColourCode = "ORA",
	Cost = 98.67m,
	ForeignIdentity = Guid.NewGuid().ToString(),
	InStock = true,
	Price = 124.00m,
	Quantity = 100,
	Retail = 99.99m,
	Size = "Extra Small",
	SizeCode = "XS",
	Weight = 43d
};

Product product =
	service.Retrieve(authenticationResponse.Message, "BC-M5").Data.First();
var items = product.Items.ToList();
items.Add(item);
product.Items = items.ToArray();

service.UpdateProduct(authenticationResponse.Message, product);
			

Update Product Item details.

// Instantiate the web service proxy.
ProductsAPI service = new ProductsAPI();

// User to authenticate with.
String username = "Testing";
// Corresponding password to authenticate with.
String password = "Sup94zex";

// Authenticate
ApiResponse authenticationResponse = service.Authenticate(username, password);

Product product =
	service.Retrieve(authenticationResponse.Message, "BC-M5").Data.First();
var items = product.Items.ToList();
ProductItem item = items.First(x => x.Barcode == "9300000034267");
item.Price = 87.98m;

product.Items = items.ToArray();

service.UpdateProduct(authenticationResponse.Message, product);
			

Update Site availability for Product Items.

// Instantiate the web service proxy.
ProductsAPI service = new ProductsAPI();

// User to authenticate with.
String username = "Testing";
// Corresponding password to authenticate with.
String password = "Sup94zex";

// Authenticate
ApiResponse authenticationResponse = service.Authenticate(username, password);

// Send through an availability entry for a given barcode
AvailabilityEntry availability = new AvailabilityEntry() {
	Barcode = "9300000034268",
	Cost = 87.2m,
	Price = 87.1m,
	Quantity = 100,
	SalePrice = 76.2m,
};

AvailabilityEntry[] entries = { availability };

// Invoke the UpdateAvailability web method. 
service.UpdateAvailability(authenticationResponse.Message, entries);
			

Update Store availability for Product Items.

// Instantiate the web service proxy.
ProductsAPI service = new ProductsAPI();

// User to authenticate with.
String username = "Testing";
// Corresponding password to authenticate with.
String password = "Sup94zex";

// Authenticate
ApiResponse authenticationResponse = service.Authenticate(username, password);

// Send through an availability entry for a given barcode. 
// This will update the availability for the given barcode, store and website.
AvailabilityEntry availability = new AvailabilityEntry() {
	Barcode = "9300000034268",
	// Branch code 
	Branch = "454",
	Quantity = 900,
	// Website identifier.
	Website = 1
};

AvailabilityEntry[] entries = { availability };

// Invoke the UpdateAvailability web method. 
service.UpdateAvailability(authenticationResponse.Message, entries);
			

Set the main image for a product.

// Instantiate the web service proxy.
ProductsAPI service = new ProductsAPI();
// User to authenticate with.
String username = "Testing";
// Corresponding password to authenticate with.
String password = "Sup94zex";
// Authenticate
ApiResponse authenticationResponse = service.Authenticate(username, password);

// Read the raw byte data.
byte[] imageData = System.IO.File.ReadAllBytes(@"[path to image file]");

// Invoke the SOAP method.
service.SetMainImage(
	authenticationResponse.Message,
	"stylecode",
	"barcode",
	true,
	"Main product image",
	imageData
);