How to Rank Related Items

You can use ranking to control how items are displayed on your Web site. Ranking applies to both base catalogs and virtual catalogs. Items and relationships in a virtual catalog inherit their ranking from the base catalog. You can override the ranks in the virtual catalog.

It is possible to have the same ranking for two or more items. Items are first sorted by rank, then alphabetically. If you do not specify a rank for a relationship the sorting is alphabetical.

You can set the rank for related items when you create the relationship.

  1. Use the RelatedProducts property on the CatalogItem object to get a dataset that contains the related products for the specified product. Use this property to access the products related to either a product or a category.

  2. Iterate through the dataset to access the individual relationships and products.

  1. Use the RelatedCategories property on the CatalogItem object to get a dataset that contains the related categories for the specified product. Use this property to access the categories related to either a product or a category.

  2. Iterate through the dataset to access the individual relationships and categories.

Example

This example describes how to set and edit the rank for a product relationship. This example creates a new relationship, sets the rank to 1, and saves the changes. It then accesses the relationship and changes the rank for each item in the relationship.

public static void AddRelationshipRank(CatalogContext context, string catalogName, string productId, string relationship, string targetId)
{
    // Create a product relationship with a rank.
    // Get the catalog.
    ProductCatalog catalog = (ProductCatalog)context.GetCatalog(catalogName, "en-US");
    // Get the product for which to create a relationship.
    CatalogItem catalogItem = catalog.GetProduct(productId);

    // Create the relationship with a rank 1 and save the item.
    catalogItem.AddRelationshipToProduct(catalog.Name, targetId, relationship, "Description of relationship", 1);
    catalogItem.Save();

    // Change the rank.
    int rank = 0;
    CatalogRelationshipsDataSet relatedProducts = catalogItem.RelatedProducts;
    foreach (CatalogRelationshipsDataSet.CatalogRelationship relatedProduct in relatedProducts.CatalogRelationships)
    {
        // Set the rank for all products in the relationship.
        relatedProduct.Rank = ++rank;
    }
    // Save the changes.
    catalogItem.Save();
}

See Also

Other Resources

How to Create a Product Relationship