Dmitry Tysh

Hello,

I am trying to do a programmatic import/create products from xml into commerce server catalog ¨C into existing product category. I have searched online but was not able to find any relative information. If anyone did the product import/create please provide the sample code.

Thank you for your help.

Dmitry




Re: Commerce Server 2007 Commerce Server 2007 Create New Product

Gael Lille

Hello Dimitri;

I done that with an application console. My problematic was :

Getting an xml data from my customer, I do :

1) Validation with a schema (optionnal) -> i'm getting a new XML with only good value

2) Generate the new schema (optionnal) -> i use it when i'm importing into the production environnement

3) Transformation into CS import format -> the simply way is to export your current catalog with the rich catalog editor (you can see the format) (i generate the id with the xslt function generate-id())

4) Importation into CS

// Variable XmlDocument permettant de stocke le flux XML contenant le catalogue du client
//XmlFluxCatalogueClient = new XmlDocument();

#region Lecture et validation du flux XML
if (Configuration.ConfigImport.ValidationFluxXmlParSchema)
{
System.
Console.WriteLine("Lecture et validation du flux XML...");

// Le flux catalogue du client est nettoye par le schema
// La variable XmlFluxCatalogueClient contiendra donc l'ensemble des produits valides par le schema
// Un fichier XML contenant l'ensemble des erreurs sera generes

XmlFluxCatalogueClient = XML.Validation.GetXMLDocument(Configuration.ConfigImport.PathXmlFluxCatalogueProduitClient, Configuration.ConfigImport.PathXsdValidationFluxCatalogueProduitClient, Configuration.ConfigImport.PathXmlFluxErreurCatalogueProduitClient);

System.Console.WriteLine("Document valide...");

}

else

{

// On charge le flux XML

System.Console.WriteLine("Lecture du flux XML");

XmlFluxCatalogueClient = new XmlDocument();

XmlFluxCatalogueClient.Load(Configuration.ConfigImport.PathXmlFluxCatalogueProduitClient);

}

System.Console.WriteLine("Nombre de produits a importer : " + XmlFluxCatalogueClient.SelectSingleNode("/import/Catalogue").ChildNodes.Count.ToString());

#endregion

#region Generation des flux pour Commerce Server

// Pour generer les informations du catalogue on a besoin du context

System.Console.WriteLine("Connexion au webservice catalogue en cours...");

CatalogContext context = Altima.CommerceServer.Catalogue.CreateCatalogContextFromServiceAgent(Configuration.ConfigImport.URLWebServiceCatalogue);

System.Console.WriteLine("Connexion ok.");

// On cree le document XML a importer

XmlDocument xmlValidePourCS = new XmlDocument();

// Stockage des attributs du noeud Root

listAttributs = new List<Altima.XML.Attribut>();

listAttributs.Add(new Altima.XML.Attribut("version", "3.0"));

XmlElement root = Altima.XML.Tools.CreationNoeudElementAvecAttributs(xmlValidePourCS, "MSCommerceCatalogCollection2", listAttributs);

// On genere le schema si configure

if (Configuration.ConfigImport.GenerationSchema)

{

System.Console.WriteLine("Generation du schema en cours...");

root.AppendChild(xmlValidePourCS.ImportNode(Altima.CommerceServer.Catalogue.GetCatalogsSchema(context), true));

System.Console.WriteLine("Generation du schema termine.");

xmlValidePourCS.AppendChild(root);

}

#endregion

// On ajoute les produits du catalogue

System.Console.WriteLine("Generation des produits du catalogue a importer...");

root.AppendChild(xmlValidePourCS.ImportNode(Altima.XML.Transformation.TransformXML(XmlFluxCatalogueClient, Configuration.ConfigImport.PathXslTransformToCS).SelectSingleNode("/child::Catalog"), true));

// On met a jour les informations obligatoire du catalogue

XmlNode xmlnodeCatalog = root.SelectSingleNode("Catalog");

// Name

XmlAttribute xmlattribute = xmlValidePourCS.CreateAttribute("name");

xmlattribute.Value = Configuration.ConfigImport.NomDuCatalogueAImporter;

xmlnodeCatalog.Attributes.Append(xmlattribute);

//variantUID

xmlattribute = xmlValidePourCS.CreateAttribute("variantUID");

xmlattribute.Value = Configuration.ConfigImport.VariantUID;

xmlnodeCatalog.Attributes.Append(xmlattribute);

//productUID

xmlattribute = xmlValidePourCS.CreateAttribute("productUID");

xmlattribute.Value = Configuration.ConfigImport.ProductUID;

xmlnodeCatalog.Attributes.Append(xmlattribute);

//currency

xmlattribute = xmlValidePourCS.CreateAttribute("currency");

xmlattribute.Value = Configuration.ConfigImport.Currency;

xmlnodeCatalog.Attributes.Append(xmlattribute);

//weight_measuring_unit

xmlattribute = xmlValidePourCS.CreateAttribute("weight_measuring_unit");

xmlattribute.Value = Configuration.ConfigImport.WeightMeasuringUnit;

xmlnodeCatalog.Attributes.Append(xmlattribute);

//DefaultLanguage

xmlattribute = xmlValidePourCS.CreateAttribute("DefaultLanguage");

xmlattribute.Value = Configuration.ConfigImport.DefaultLanguage;

xmlnodeCatalog.Attributes.Append(xmlattribute);

//ReportingLanguage

xmlattribute = xmlValidePourCS.CreateAttribute("ReportingLanguage");

xmlattribute.Value = Configuration.ConfigImport.ReportingLanguage;

xmlnodeCatalog.Attributes.Append(xmlattribute);

//languages

xmlattribute = xmlValidePourCS.CreateAttribute("languages");

xmlattribute.Value = Configuration.ConfigImport.Languages;

xmlnodeCatalog.Attributes.Append(xmlattribute);

// On ajoute le noeud root au document xml

xmlValidePourCS.AppendChild(root);

System.Console.WriteLine("Generation termine.");

// On sauvegarde le flux

xmlValidePourCS.Save(Configuration.ConfigImport.PathXmlFluxAImporterDansCS);

// Importation

CatalogImportOptions importoptions = new CatalogImportOptions();

importoptions.Mode = ImportMode.Incremental;

importoptions.TransactionMode = TransactionMode.NonTransactional;

importoptions.CatalogsToImport = Configuration.ConfigImport.NomDuCatalogueAImporter;

importoptions.Operation = ImportOperation.ValidateAndImport;

importoptions.ImportSchemaChanges = false;

importoptions.GenerateFullTextIndexes = false;

importoptions.OverwriteRelationships = false;

Console.WriteLine("Debut de l'importation");

ImportProgress importprogress = context.ImportXml(importoptions, Configuration.ConfigImport.PathXmlFluxAImporterDansCS);

while (importprogress.Status == CatalogOperationsStatus.InProgress)

{

System.Threading.Thread.Sleep(3000);

importprogress.Refresh();

}

// If the import operation failed, write the errors to the console.

if (importprogress.Status == CatalogOperationsStatus.Failed)

{

foreach (CatalogError error in importprogress.Errors)

{

Console.WriteLine(string.Format("Line number: {0} Error message: {1}",

error.LineNumber, error.Message));

}

}

Console.WriteLine("Fin de l'importation");






Re: Commerce Server 2007 Commerce Server 2007 Create New Product

Jeff Lynch - MVP

You could do this very simply by using BizTalk Server 2006 and the new "Catalog" Adapter provided with CS2007. I'd highly recommend this over a custom .NET application especially if your catalog imports are large.

Jeff Lynch
MVP Windows Server System - Commerce Server
http://codebetter.com/blogs/jeff.lynch






Re: Commerce Server 2007 Commerce Server 2007 Create New Product

DTysh

Jeff thanks for the reply, do you have a sample by any chance






Re: Commerce Server 2007 Commerce Server 2007 Create New Product

Gael Lille

Of course, but your customer can not have Biztalk...