In some cases though you might need to leverage the API outside a web context for automated integration between systems, for custom web services, or integration of uCommerce functionality directly in other custom applications such as point of sale- or kiosk apps.
This article outlines the steps required to get the API going for uCommerce 3.
Running uCommerce 2? Please refer to Enable the uCommerce E-commerce API Outside the Web Context instead.
Open Visual Studio and create a new console application project in the usual way. We’ll use that as a basis for getting the APIs up and running.
My usual project structure has a folder for the project source and any libraries required to build it like below.
You’ll find the assemblies in your website under /bin.
Copy the following assemblies to /lib in your project.
You’ll want the core configuration and the pipelines as part of your project to be able to use the API fully.
Copy these folders to the project folder of your solution.
For uCommerce to able to pick up the configuration they need to be copied to the output folder of the compiled application.
Select all the config files and press F4 to bring up the properties for the files.
Set “Copy to Output Directory” to “Copy if newer”.
Next reference the assemblies you copied to the /lib folder in Visual Studio.
Next up we need to modify the application config with settings to enable the final bits of uCommerce to run.
Open up web.config of your Umbraco site and locate the configSections element. Copy the sectionGroups called “commerce” and “systemcache” to the coonfigSections element of App.config.
NHibernate.Caches.SysCache2" requirePermission="false" />
Finally we need the actual sections from web.config copied to app.config.
With all that in place your App.config file should look like this:
<?xml version="1.0" encoding="utf-8" ?>
<sectionGroup name="commerce" type="System.Configuration.ConfigurationSectionGroup">
<section name="catalogConfiguration" type="UCommerce.Infrastructure.Configuration.CatalogConfigurationSection, UCommerce.Infrastructure" />
<section name="runtimeConfiguration" type="UCommerce.Infrastructure.Configuration.RuntimeConfigurationSection, UCommerce.Infrastructure" />
<section name="securityConfiguration" type="UCommerce.Infrastructure.Configuration.SecurityConfigurationSection, UCommerce.Infrastructure" />
<section name="amazonPaymentMethodService" type="UCommerce.Transactions.Payments.Amazon.Configuration.AmazonPaymentMethodServiceConfigurationSection, UCommerce.Transactions.Payments" requirePermission="false" />
<section name="syscache" type="NHibernate.Caches.SysCache.SysCacheSectionHandler, NHibernate.Caches.SysCache2" requirePermission="false" />
<runtimeConfiguration enableCache="false" cacheProvider="NHibernate.Caches.SysCache2.SysCacheProvider, NHibernate.Caches.SysCache2" connectionString="(auto)" />
<catalogConfiguration defaultCultureCode="en-US" enforceCategoryNameUniquenessWithinCatalogs="true" />
<securityConfiguration enable="true" />
<amazonPaymentMethodService configSource="umbraco\ucommerce\configuration\payments\Amazon.config" />
<!-- Cache catalog objects for 60 mins before refreshing -->
<cache region="CatalogFoundation" expiration="3600" priority="5" />
<cache region="MarketingFoundation" expiration="3600" priority="5" />
<cache region="SecurityFoundation" expiration="3600" priority="5" />
Finally you need to point the connection string at your database. Simply replace the (auto) values with an actual connection string and you’re good to go.
<runtimeConfiguration ... connectionString="server=(local);database=Umbraco;user id=umbraco;password=1234567890" />
Leveraging uCommerce outside a web site makes for some interesting use cases. Foremost is automated integration with other systems, but you could use this method to establish an offline Point of Sale or Kiosk-type system with a completely different frontend done in Windows Presentation Foundation for a rich in store experience.
Download code sample
The opinions expressed herein are my own personal opinions and do not represent
my employer's view in any way.