Partilhar via


How to Create a Base Catalog by Using the Catalog API

This topic describes how to create a base catalog in the catalog database. It describes how to create product and category definitions, and then add those definitions to a base catalog. It shows how to create properties for the catalog products and set values for those properties.

To create a base catalog

  1. Create the properties needed in your catalog by using the CreateProperty method of the CatalogContext object.

  2. Create a product definition by using the CreateDefinition method of the CatalogContext object that has the definitionType parameter set to ProductDefinition.

  3. Create one or more category definitions by using the CreateDefinition method of the CatalogContext object that has the definitionType parameter set to CategoryDefinition.

  4. Create a base catalog by using one of the CreateBaseCatalog methods of the CatalogContext object.

    Dd328285.alert_caution(en-US,CS.90).gifImportant Note:

    Do not use the following characters in the catalog name: .,\"[]'()#

  5. Add the categories to the catalog by using one of the CreateCategory methods of the BaseCatalog object.

  6. Add the products to the catalog by using one of the CreateProduct methods of the BaseCatalog object.

  7. Save the properties of the product in the catalog.

Example

This example creates a base catalog named "Books". The example creates a category definition named "BookType" and a product definition named "Book". It creates a category named "Fiction". A single book is added to the Fiction category with the properties "Title", "Author", "ISBN", and "PageCount".

public static void CreateNewCatalog(CatalogContext context)
{
    try
    {
        // Create the properties for the catalog products.
        CatalogProperty title = context.CreateProperty("Title", CatalogDataType.String, 250);
        CatalogProperty author = context.CreateProperty("Author", CatalogDataType.String, 100);
        CatalogProperty isbn = context.CreateProperty("ISBN", CatalogDataType.String, 20); 
        CatalogProperty pageCount = context.CreateProperty("PageCount", CatalogDataType.Integer, 0);
        CatalogProperty code = context.CreateProperty("Code", CatalogDataType.String, 20);

        // Create a product definition for the books in the catalog. 
        CatalogDefinition productDefinition = context.CreateDefinition("Book", CatalogDefinitionType.ProductDefinition);

        // Add the properties to the product definition and save the definition.
        productDefinition.AddProperty(title.Name, DefinitionPropertyType.NormalProperty);
        productDefinition.AddProperty(author.Name, DefinitionPropertyType.NormalProperty);
        productDefinition.AddProperty(isbn.Name, DefinitionPropertyType.NormalProperty);
        productDefinition.AddProperty(pageCount.Name, DefinitionPropertyType.NormalProperty);
        productDefinition.AddProperty(code.Name, DefinitionPropertyType.VariantProperty);
        productDefinition.Save();

        // Create a category definition and save it.
        CatalogDefinition categoryDefinition = context.CreateDefinition("BookType", CatalogDefinitionType.CategoryDefinition);
        categoryDefinition.Description = "Categories for books";
        categoryDefinition.Save();

        // Create a base catalog with property "ISBN" as the identifying property for the catalog.
        BaseCatalog baseCatalog = context.CreateBaseCatalog("Books", isbn.Name, code.Name, "en-us", "en-us");

        // Add the categories to the catalog and save.
        Category categoryFiction = baseCatalog.CreateCategory(categoryDefinition.Name, "Fiction");
        categoryFiction.DisplayName = "Fiction";
        categoryFiction.Save();

        // Add a book with the specified ISBN to the Fiction category in the catalog.
        Product book = baseCatalog.CreateProduct(productDefinition.Name, "01401040445", 10.95m, categoryFiction.Name);

        // Define the properties of the product and save.
        book["Title"] = "War and Peace";
        book["Author"] = "Leo Tolstoy";
        book.Save();
    }
    catch (EntityAlreadyExistsException e)
    {
        Console.WriteLine(string.Format("Exception: {0}", e.Message));
    }
}      

See Also

Other Resources

How to Create a CatalogContext Object

Managing Products and Categories by Using the Catalog API