Add images to a product

To add an image to a product, you must perform the following:

  1. Get instances of the following managers:
    • Catalog manager
    • Libraries manager
    • Content links manager
  2. Get an instance of the product. 
    For more information, see For developers: Query products.
  3. Get an instance of the image. 
    The image of a product is represented by a Sitefinity CMS image.
    For more information about uploading images in Sitefinity CMS, see For developers: CRUD operations with images.
  4. Create an instance of the ProductImage class.
  5. Set the properties of the product image to the respective values of the Sitefinity CMS image.

    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. 
    In the code example below, the first image is assigned as the primary one. For more information, see For developers: Change the primary image of a product.

  6. Add the product image to the product.
    Add the ProductImage instance to the Images collection of the product.
  7. Save the changes to the product manager.
  8. Create a content link.
    To create a content link between the product and the image, call the CreateContentLink method of the content links manager and pass the product and the image as arguments. For example, as componentPropertyName, pass ProductImage.
  9. Set the ordinal of the content link.

    IMPORTANT: Set the Ordinal property of the content link to the same value as the Ordinal property of the product image.

  10. Save the changes to the content links manager.

Code example: Add images to 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?