Change the primary image of a product

This article explains how to change the primary image of a product with one of the other images that are associated with the product.
To do this, you must perform the following:

  1. Get instance of the following managers:
    • Catalog manager
    • ContentLinks manager
  2. Get an instance of the product. 
    For more information, see For developers: Query products.
  3. Form the Images collection, get the new primary image of the product.
  4. Get the images that are ordered before the new primary image.
    To keep the ordinals consistent, you must update the ordinals of the images that are positioned before the selected image.
  5. Update the ordinal of the new primary image.
    To assign the product as a primary image, set the Ordinal property of the product image to 0

    NOTE: The primary image of a product must have its Ordinal property set to 0. The other images must have their Ordinal properties set to 1, 2, 3, etc. You can only have one primary image per product.

  6. Get the content link for the new primary image and update its Ordinal property.
  7. Update the ordinal property of the images that were positioned before the new primary image.
    You must shift each one of these images one position behind, by incrementing their Ordinal property by 1.
  8. Update the content link of the images that were positioned before the new primary image.
    Get the content link of each of these images and increment its Ordinal property by 1.
  9. Save the changes to both the managers.

Code example: Change the primary image of a product

Want to learn more?

Sign up for our free beginner training. Boost your credentials through advanced courses and certification.
Register for Sitefinity training and certification.

Was this article helpful?