Wednesday, June 11, 2008

Google Base Integration

Google Base is a place where you can easily submit all types of online and offline content, which we'll make searchable on Google (if your content isn't online yet, we'll put it there). You can describe any item you post with attributes, which will help people find it when they do related searches. In fact, based on your items' relevance, users may find them in their results for searches on Google Product Search and even our main Google Web Search.

IF we want to add products to google base there are certain steps we need to follow

STEP 1: Down load the Api for google base from google Devolper center http://code.google.com/apis/base/
STEP 2: Now add the following dll's "Google.GData.Client.dll", "Google.GData.Extensions.dll", "Google.GData.GoogleBase.dll" as reference to the bin directory of your porject.
STEP 3: Now import the namespaces
using Google.GData.Extensions;
using Google.GData.Client;
using Google.GData.GoogleBase;
STEP 4: Create an account and get a Devolper key.
http://code.google.com/apis/base/signup.html
STEP 5: Now we can start coding
It is possible to add,edit and delete products in google base


How to add products to Google Base
//Create the objec to GBaseService which take two parameters the website name and devolper key
Google.GData.GoogleBase.GBaseService service = new Google.GData.GoogleBase.GBaseService(websiteName, devolperKey);

// set the user credentials
service.setUserCredentials(userName,password);

//now create the object of GbaseEntry to which we can add the details of the Product
Google.GData.GoogleBase.GBaseEntry entry = new Google.GData.GoogleBase.GBaseEntry();
entry.Title.Text = this.ProductName;
entry.Content.Content = this.Description;

// it will depend upon what type of item we are adding to google base
// for now we are adding products so the item type will be Products
entry.GBaseAttributes.ItemType = "Products";
entry.GBaseAttributes.AddTextAttribute("product type", this.Category);
entry.GBaseAttributes.Price = new Google.GData.GoogleBase.FloatUnit((float)this.Price, "USD");

//if we want to add an url to the product that is added to google base
AtomLink link = new AtomLink();
link.HRef = new AtomUri("url");
//specify the url
link.Rel = "alternate";
link.Dirty = false;
link.Type = "text/html";
entry.Links.Add(link);

// specify the location of the product ie Which location to dispaly the product
entry.GBaseAttributes.Location = "us";

//Finally we add the product to Google Base
Google.GData.GoogleBase.GBaseEntry myEntry = service.Insert(
Google.GData.GoogleBase.GBaseUriFactory.Default.ItemsFeedUri, entry);
string gbaseEntryId = myEntry.Id.Uri.ToString();

//myEntry.Id.Uri is the unique id that we used to acces product in Google Base.

How to Delete products From Google Base
Google.GData.GoogleBase.GBaseService service = new Google.GData.GoogleBase.GBaseService(websiteName, devolperKey);
// set the user credentialsservice.setUserCredentials(userName,password);
Uri deleteEntry = new Uri(SelectedProduct.GbaseKey.Trim
());//we need to specify the unique id
service.Delete(deleteEntry);//Deletes the correspondign product from google base

How to Edit products in Google Base
Google.GData.GoogleBase.GBaseService service = new Google.GData.GoogleBase.GBaseService(websiteName, devolperKey);// set the user credentials
service.setUserCredentials(userName,password);

Uri gBaseEnty = new Uri("UniqueId")
Google.GData.GoogleBase.GBaseEntry myEntry = service.GetEntry(gBaseEnty);//get the product details using the uinque id and then update the details
myEntry.Title.Text = this.ProductName;
myEntry.Content.Content = this.Description;
myEntry.GBaseAttributes.ItemType = "Products";
myEntry.GBaseAttributes.AddTextAttribute("product type", this.Category);
myEntry.GBaseAttributes.Price = new Google.GData.GoogleBase.FloatUnit((float)this.Price, "USD");
AtomLink link=new AtomLink();
link.HRef=new AtomUri("url");
link.Rel = "alternate" ;
link.Dirty = false ;
link.Type = "text/html";
myEntry.Links.Add(link) ;
service.Update(myEntry);// Upadate the product with new entries

Happy Programming

No comments: