+1-888-365-2779
Try Now
More in this section
Categories
Bloggers
Blogs RSS feed

How to Integrate Amazon Advertising API - Linq to Amazon

by Georgi Chokov
Earlier, I did a blog post on integrating Amazon Associates API into Sitefinity. Although that widget works fine, what is you want to the product list look a certain way? What if you want to show a specific product based on an ID number, which Amazon calls an ASIN? If you answered yes to any of these questions, or if you want to show lists based on other logic here are the steps:

 

  1. Obtain an Amazon Access ID
  2. Find the ASIN number of the product that you'd like to use. This can be found in the query string. Here is an example:
ASINAmazon

 

     3. Create a user control called AmazonItemLookUp.ascx and use the code below:

 

AmazonItemLookUp.ascx

<h2>Insert ASIN</h2> 
        <asp:TextBox ID="ItemIDTextBox" runat="server" MaxLength="10"></asp:TextBox> 
        <br /> 
       <asp:Button ID="btnSubmit" runat="server" Text="Submit"  
            onclick="btnSubmit_Click" /> 
            <br /> 
            <br /> 
          <asp:Repeater ID="Repeater1" runat="server"
          <ItemTemplate> 
          <asp:Image ID="SmallImage" runat="server" ImageUrl='<%# DataBinder.Eval(Container.DataItem, "MediumProductImageURL") %>' /> 
          <br /> 
          <asp:Label ID="ASIN" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "ASIN") %>' Visible="false"></asp:Label> 
          <br /> 
          <i><asp:Label ID="ProductNameLabel" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "ProductName") %>'></asp:Label></i
<br /> 
          <b><asp:Label ID="ProductPriceLabel" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "ProductPrice") %>'></asp:Label> 
</b> 
<br /> 
<asp:HyperLink ID="DetailPageURLHyperLink" runat="server" Text="Buy this Product!" Target="_blank" NavigateUrl='<%# DataBinder.Eval(Container.DataItem, "DetailPageURL") %>'></asp:HyperLink> 
          </ItemTemplate> 
          </asp:Repeater>  

 

AmazonItemLookUp.ascx.cs

protected void btnSubmit_Click(object sender, EventArgs e) 
    { 
        Amazon a = new Amazon(); 
        //Insert AwsAccessKeyId below 
        a.AwsAccessKeyId = "xxxxxxxxxxxx";  
        a.ItemId = ItemIDTextBox.Text; 
        Repeater1.DataSource = a.PopulateAmazonList(); 
        Repeater1.DataBind(); 
 
    } 
 

      4. Create a class called Amazon in your App_Code folder and insert the code below:

 

 

using System.Linq; 
using System.Collections; 
using System.Xml.Linq; 
 
/// <summary> 
/// Summary description for Amazon 
/// </summary> 
public class Amazon 
    public string GetURL() 
    { 
        return "http://webservices.amazon.com/onca/xml?Service=AWSECommerceService&AWSAccessKeyId=" + AwsAccessKeyId + "&Version=2009-03-31&Operation=ItemLookup&ResponseGroup=Request,SalesRank,Small,Images,OfferSummary&ItemId=" + ItemId; 
 
    } 
 
    private string _AwsAccessKeyId; 
    public string AwsAccessKeyId 
    { 
        get 
        { 
            return _AwsAccessKeyId; 
        } 
        set 
        { 
            _AwsAccessKeyId = value; 
        } 
    } 
    private string _ItemId; 
    public string ItemId 
    { 
        get 
        { 
            return _ItemId; 
        } 
        set 
        { 
            _ItemId = value; 
        } 
    } 
 
    /// <summary> 
    /// Gets namespace needed for Amazon.com recommendations 
    /// </summary> 
    public static XNamespace ns 
    { 
        get 
        { 
            return "http://webservices.amazon.com/AWSECommerceService/2009-03-31"
        } 
    } 
 
    /// <summary> 
    /// Populates a list of product recommendations from Amazon.com 
    /// </summary> 
    /// <param name="list2"></param> 
    public IList PopulateAmazonList() 
    { 
        XElement tags = XElement.Load(GetURL()); 
        var groups = from book in tags.Descendants(ns + "Item"// Return all nodes with the word "Item" in it 
                     let bookAttributes = book.Element(ns + "ItemAttributes"//In each Item node, get the "ItemAttributes" node 
                     let offerAttributes = book.Element(ns + "OfferSummary"//Get the Offer Summary Node 
                     let mediumImageAttributes = book.Element(ns + "MediumImage"
                     let lowestNewPrice = offerAttributes.Element(ns + "LowestNewPrice"//Within the Offer Summary Node, get the Lowest New Price node 
 
                     select new 
                     { 
                         ASIN = ((string)book.Element(ns + "ASIN")), 
                         ProductName = ((string)bookAttributes.Element(ns + "Title")), 
                         MediumProductImageURL = ((string)mediumImageAttributes.Element(ns + "URL")), 
                         DetailPageURL = ((string)book.Element(ns + "DetailPageURL")), 
                         ProductPrice = ((string)lowestNewPrice.Element(ns + "FormattedPrice")) 
 
                     }; 
        return groups.ToList(); 
    } 

 

     5. Upload the User Control to Sitefinity

     6.  Inside the text box, insert the ASIN number of the product

     7.  Click on the link "Buy this Product" ,that appears below, and give it to your customer as shown below:

 AmazonImage

How it Works

Using a long query string, Amazon returns a large XML file with many attributes for the item. Here is a sample:

 

 AmazonXML

To view your URL, place a break point on this code in Amazon.cs:

var Groups

In the URL, you'll notice many properties in the query string and here is an explanation of a few of them:

 

Operation - This defines what you're seeking to do. In this case, I want to look up a single product. You can also look up a list of products based on other identifying information, like a search index or a tag. Amazon also has many other operations and for more info, please read this article.

 

Response Groups - These pull certain item attributes into the XML file. If attributes were removed from your query string in response groups, you'd notice that data would be missing in the XML file. For more info on response groups, please read this article.


1 comment

Leave a comment
  1. Pierre Mar 31, 2011
    Since you are not using the widget provide by amazon how would one get credit for the click generated?

    Leave a comment