How to Import Selected Fields of a Catalog
This topic describes how to import selected fields of a catalog from an XML file. It describes how to set the Commerce Server Core Systems options to control an import operation.
How to import selected fields from an XML file
Create a CatalogImportOptions object and set the properties for your project. This object contains options that control the behavior of the import operation.
Use the PropertiesToImport property of the CatalogImportOptions object to specify the properties you want to import.
Start the import operation by calling one of the ImportXml methods of the CatalogContext object.
Monitor the status of the import operation and look for errors by using the Status property of the ImportProgress object returned by the ImportXml method.
Example
This example imports a catalog "Books" from an XML file "testfile" in the temp directory of this computer. The import is an incremental, non-transactional import. The ListPriceandDisplayName properties are updated. The file is validated as it is imported. If the import fails, the errors are written to the console.
public static void ImportSelectedItems(CatalogContext context)
{
// Three second refresh interval.
const int RefreshInterval = 3000;
// Create CatalogImportOptions and set the options
// for the import operation.
CatalogImportOptions importOptions = new CatalogImportOptions();
importOptions.Mode = ImportMode.Incremental;
// Merge any new data with the existing data.
importOptions.OverwriteRelationships = false;
importOptions.LastModifiedDate = DateTime.MinValue.AddDays(10);
importOptions.TransactionMode = TransactionMode.NonTransactional;
importOptions.CatalogsToImport = "Books";
importOptions.Operation = ImportOperation.ValidateAndImport;
// Only update the ListPrice and DisplayName properties.
importOptions.PropertiesToImport = String.Join(“,”, new string[] { CatalogItemsDataSetSchema.ListPrice, CatalogItemsDataSetSchema.DisplayName } );
// The XML file is in the temp directory on this computer.
string fileName = Path.GetTempPath() + @"testfile.xml";
// Monitor the progress of the import operation.
ImportProgress importProgress = context.ImportXml(importOptions, fileName);
while (importProgress.Status == CatalogOperationsStatus.InProgress)
{
System.Threading.Thread.Sleep(RefreshInterval);
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));
}
}
}
See Also
Other Resources
How to Create a CatalogContext Object