Get Image Url of Product by Product id

Posted by Community Admin on 05-Aug-2018 14:36

Get Image Url of Product by Product id

All Replies

Posted by Community Admin on 11-May-2012 00:00

I need image url of product by product Id. i m not getting image url by this code

  CatalogManager manager = new CatalogManager();
 var products = manager.GetProducts().ToList();

 foreach (var product in products)
     
      string imgUrl=product.PrimaryImageUrl;  // or  product.Images;
   

Posted by Community Admin on 18-May-2012 00:00

Ritesh,

Here is some code to get you on the right path.

protected void Page_Load(object sender, EventArgs e)
        
            // Get a Catalog Manager
            CatalogManager manager = CatalogManager.GetManager();
 
            // Pull back all of your products
            IQueryable<Product> products = manager.GetProducts();
 
            // Populate the Images
            IEnumerable<Product> productsWithImages = manager.PopulateImages("", products);
 
            // Loop through each product and do some stuff :)
            foreach (Product product in productsWithImages)
            
                // Grab the URL to the primary image for this product
                string primaryImageURL = product.PrimaryImageUrl;
 
                // This will be a list of all the images associated with this product
                List<ProductImage> productImages = product.Images;
 
                // Loop through this products images and do some more stuff :D
                foreach (ProductImage productImage in productImages)
                
                    // Grab the URL to the current image
                    string imageURL = productImage.Url;
                
            
        

Big thanks to Venkata Koppaka for showing me how to do it in the first place!

Posted by Community Admin on 18-May-2012 00:00

Ritesh,

Eh, it seems that I left out the whole "get by id" part. Here's another example.

public string getPrimaryImageURLByProduct(Guid productID)
        
            string primaryImageURL = String.Empty;
 
            // Get a Catalog Manager
            CatalogManager manager = CatalogManager.GetManager();
 
            // Pull back your product
            Product product = manager.GetProducts().Where(p => p.Id == productID).FirstOrDefault();
 
            if (product != null)
            
                // Populate the Images
                IEnumerable<Product> productWithImages = manager.PopulateImages("", Enumerable.Repeat<Product>(product, 1));
 
                // Loop through each product and do some stuff :)
                foreach (Product p in productWithImages)
                
                    // Grab the URL to the primary image for this product
                    primaryImageURL = p.PrimaryImageUrl;
                
            
 
            return primaryImageURL;
        

This thread is closed