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

Increase your Sitefinity skills by signing up for our free trainings. Get Sitefinity-certified at Progress Education Community to boost your credentials.

Web Security for Sitefinity Administrators

The free standalone Web Security lesson teaches administrators how to protect your websites and Sitefinity instance from external threats. Learn to configure HTTPS, SSL, allow lists for trusted sites, and cookie security, among others.

Foundations of Sitefinity ASP.NET Core Development

The free on-demand video course teaches developers how to use Sitefinity .NET Core and leverage its decoupled architecture and new way of coding against the platform.

Was this article helpful?