In my position as Software Architect and Software engineer I frequently
face the requirement to import for example product information from third-party
systems such as SAP into sitecore.
The preferred format is XML in this case as sitecore is based on XML.
As everyone knows, requirements change during
agile development due to the software and structure and the final data structure only becomes clear during the different
development phases. The outcome of this is that an import of product information
could become a bottleneck. However, the product information should be integrated
in the first steps rather than along the line.
The aim should be to get a flexible and dynamic import to avoid putting in excessive effort when changes appear and to avoid increasing project costs to the customer.
Therefor we need an XML structure that gives us this flexibility. It should always be define
prior the project kick-off.
The XML offered here contains some static branches and flexible branches named “attribute”
and “attributegroup”. In this post you will learn how to add items to sitecore and
how to create sitecore-templates dynamically by using XML and c#.
1.) Define your xml
- click here for download
The structure is very simple. You got one or more categories with a unique
key and a name, which contains products with some static values like “name”,
“identifier”(unique), prices and images(the latter two are not considered in this example). The product also contains a tag “attributes” which describes
our dynamic section. In the following steps we will create templates from this section
by using the first level “attributegroup”-Tag as sitecore-Template,
the secondlevel “attributegroup”-Tag as sitecore-Template section and
the “attribute” as sitecore-Field value.
2.) Create base
product/category template and productsBranch
To import the categories and products into sitecore you need to define/create a branch
where the items should be added. You need a template for the product and category.
3.) Create an object
in c# which is equivalent to the XML - click here for download
You need to create an object in c# with which you can work in further steps.
4.) Load and Parse
XML - click here for download
Load your XML and parse it to your object.
5.) Importmanager - create or
First of all create or update categories as described in the following snippet -
click here for download
6.) Create or update
products - click here for download
7.) Create or update
templates - click here for download
Create or update the sitecore-Templates based on the “attributes”-Tag
8.) Add attributes
- click here for download
That’s it - the finished result should be something like this