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.

Get started with Integration Hub | Sitefinity Cloud | Sitefinity SaaS

This free lesson teaches administrators, marketers, and other business professionals how to use the Integration hub service to create automated workflows between Sitefinity and other business systems.

Web Security for Sitefinity Administrators

This free lesson teaches administrators the basics about protecting yor Sitefinity instance and its sites from external threats. 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?