Syncing divisions from an external source with Sitecore Commerce Connector

Today we’re going to set up a simple import process to start syncing divisions using Sitecore Commerce Connect.  While this post will focus on syncing divisions, it could be easily adapted for use on other commerce entities.  I’m also going to assume that you already have a working Sitecore 7.5 installation with the Commerce Connect installed and set up.

The first thing to know when setting up a division sync process is that the default Commerce Connect defines a sync pipeline.  We’re going to insert some custom code into this pipeline to mock up retrieving division listings from an external source and saving these to our Sitecore instance. Right now we’re going to focus on syncing in a single direction, if you’ll be pushing changes made in Sitecore back out there are a few additional considerations to keep in mind, these will be covered at a later date.

Let’s get started.  First we’ll create a folder in the App_Config\Include called “CommerceConnectTest” or something clever like that. Creating this folder will help ensure that your configuration files are run in the correct order.  In here we’ll add a .config file to patch into the Commerce Connect sync pipelines and remove what isn’t needed for a single direction sync.  While we’re at it, let’s also create a new class for the division sync pipeline process we’re about to create.

The thing to keep in mind when you patch in a new processor is that you’ll want to make sure it runs before the save divisions processor.  In the end it should look something like this:

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
  <sitecore>
    <pipelines>
      <commerce.synchronizeProducts.synchronizeDivisions>
        <processor type="Sitecore.Commerce.Pipelines.Products.SynchronizeDivisions.ReadSitecoreDivisions, Sitecore.Commerce">
          <patch:delete />
        </processor>
        <processor type="Sitecore.Commerce.Pipelines.Products.SynchronizeDivisions.ResolveDivisionsChanges, Sitecore.Commerce">
          <patch:delete />
        </processor>
        <processor type="Sitecore.Commerce.Pipelines.Products.SynchronizeDivisions.SaveDivisionsToExternalCommerceSystem, Sitecore.Commerce" >
          <patch:delete />
        </processor>
        <processor type="CommerceConnectTest.DivisionSync, CommerceConnectTest" patch:after="processor[@type='Sitecore.Commerce.Pipelines.Products.SynchronizeDivisions.ReadExternalCommerceSystemDivisions, Sitecore.Commerce']" />
      </commerce.synchronizeProducts.synchronizeDivisions>
    </pipelines>
  </sitecore>
</configuration>

After patching your …/sitecore/admin/showconfig.aspx should contain something like this.

<commerce.synchronizeProducts.synchronizeDivisions patch:source="Sitecore.Commerce.Products.config">
<!--
 The following processor can be removed if synchronization will be performed only from external commerce system to Sitecore
-->
<processor type="Sitecore.Commerce.Pipelines.Products.SynchronizeDivisions.ReadExternalCommerceSystemDivisions, Sitecore.Commerce"/>
<processor type="CommerceConnectTest.DivisionSync, CommerceConnectTest" patch:source="DivisionSync.config"/>
<!--
 The following processor can be removed if synchronization will be performed only from external commerce system to Sitecore
-->
<!--
 The following processor can be removed if synchronization will be performed only from external commerce system to Sitecore
-->
<processor type="Sitecore.Commerce.Pipelines.Products.SynchronizeDivisions.SaveDivisionsToSitecore, Sitecore.Commerce">
<param desc="divisionRepository" ref="divisionRepository"/>
</processor>
</commerce.synchronizeProducts.synchronizeDivisions>

At this point we’ve inserted some custom code into the synchronizeDivisions pipeline, however our DivisionSync class isn’t quite ready to go yet.  In order for Sitecore to know how to interact with this class we’ll need to have it extend

PipelineProcessor<ServicePipelineArgs>

and implement the Process method.

According to the documentation we’ll need to use the “Divisions” property of the request to add our external divisions. This is an enumeration of Division entities. How that division entity is configured and created is pretty slick, if you’re this far looking to do some research of your own into Commerce Connect I recommend looking into the repositories further.  Depending on what other processes you have running in this pipeline it may not be created for you already.  If it doesn’t exist (it shouldn’t in this example) create it and add a test division.  With the magic of copy and paste your process method should look like this:


public override void Process(ServicePipelineArgs args)
        {
            try
            {
                if (!args.Request.Properties.Contains("Divisions"))
                {
                    args.Request.Properties["Divisions"] = new List<Division>();
                }

                List<Division> divisions = args.Request.Properties["Divisions"] as List<Division>;
                if (divisions == null)
                {
                    Log.Error("Oops!", this);
                    args.Result.Success = false;
                    return;
                }

                divisions.Add(new Division
                {
                    Name = "My First division " + DateTime.Now.Second.ToString(),
                    Created = DateTime.MinValue,
                    Updated = DateTime.Now,
                    ExternalId = "42",
                    Description = "Hello World!",
                });

                args.Result.Properties["Divisions"] = divisions;

                args.Result.Success &= true;
            }
            catch (Exception ex)
            {
                Log.Error("Error syncing divisions", ex, this);
                args.Result.Success = false;
            }
        }

Let’s save, build and run Synchronize artifacts again.  Once it completes navigate to or refresh the divisions in Sitecore.  You should now see a newly created division.

SitecoreDivision

The creation of the Sitecore items from the contents of the Divisions property is handled in the SaveDivisionsToSitecore process, which is setup in the default installation.  Not having to re-create the Sitecore CRUD operations is a pretty nice time-saver.  Just remember that in order to take advantage of it you’ll need to snuggle your processor in before the SaveDivisionsToSitecore processor.

I hope you found this helpful and a good starting place.  Check back often for new updates on Sitecore Commerce Connect

2 thoughts on “Syncing divisions from an external source with Sitecore Commerce Connector

Add yours

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

Up ↑

%d bloggers like this: