+1-888-365-2779
Try Now
More in this section

Forums / Ecommerce / Get Image Url of Product by Product id

Get Image Url of Product by Product id

3 posts, 0 answered
  1. Ritesh
    Ritesh avatar
    2 posts
    Registered:
    26 Aug 2012
    11 May 2012
    Link to this post
    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;
       
    }
  2. Tim
    Tim avatar
    258 posts
    Registered:
    22 Jun 2011
    18 May 2012
    Link to this post
    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!
  3. Tim
    Tim avatar
    258 posts
    Registered:
    22 Jun 2011
    18 May 2012
    Link to this post
    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;
            }

3 posts, 0 answered