Partager via


How to Export Selected Fields of a Catalog

This topic describes how to export selected fields of a catalog to an XML file. It describes how set the options to control an export operation.

To export selected fields of a catalog

  1. Create a CatalogExportOptions object and set the properties for your project. This object contains options that control the behavior of the import operation.

  2. Specify the properties to export. You can do this by using the PropertiesToExport property of the CatalogExportOptions object. You can also specify the properties as the result of a search clause. To do this set the Mode property of the ExportOptions object to ExpressionBased. You can specify the search by using the ProductsAndCategoriesClause property and the FilterDescendantsClause property of the ExportOptions object.

  3. Start the export operation by calling one of the ExportXml methods of the CatalogContext object.

  4. Monitor the status of the export operation and look for errors by using the Status property of the ExportProgress object returned by ExportXml method.

Example

This example exports a catalog "Books" to an XML file "testfile" in the temp directory of this computer. The export is expression-based. Only the items in "Category1" with a list price greater than $10.00 are exported. If the export fails, the errors are written to the console.

public static void ExportSelectedItems(CatalogContext context)
{
    // 3 second refresh interval.
    const int RefreshInterval = 3000;
    ProductCatalog catalog = context.GetCatalog("Books");
    CatalogExportOptions exportOptions = new CatalogExportOptions();
    exportOptions.CatalogsToExport = catalog.Name;

    // Export the schema with the data.
    exportOptions.SchemaExportType = SchemaExportType.All;
    // The catalog will be written to an XML file "testfile" in the 
    // temp directory on this computer.
    string path1 = Path.GetTempPath();
    string path2 = "test1.xml";
    string fileName = Path.Combine(path1, path2);    

    // Do an expression-based export.
    exportOptions.Mode = ExportMode.ExpressionBased;
    // Look only at items in 'Category1'.
    exportOptions.ProductsAndCategoriesClause = String.Format("{0}='Category1'", CatalogItemsDataSetSchema.CategoryName);
    // Look at all items in this category.
    exportOptions.Descendants = Descendants.All;
    // Of the items in this category, export those that have a list price greater than $10.00.
    exportOptions.FilterDescendantsClause = String.Format("{0} > 10", CatalogItemsDataSetSchema.ListPrice);

    ExportProgress progress = context.ExportXml(exportOptions, fileName);
    while (progress.Status == CatalogOperationsStatus.InProgress)
    {
        System.Threading.Thread.Sleep(RefreshInterval);
        progress.Refresh();
    }

    // If the export operation failed, write the errors to the console.
    if (progress.Status == CatalogOperationsStatus.Failed)
    {
        foreach (CatalogError error in progress.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

How to Export a Catalog to an XML File

What Are the Export Options?

Importing Catalog Data by Using the Catalog API