Adrian Esquivel
November 27, 2009
Articles

TECKpert's Tips: Using Google Base with .NET for Real Estate

Adding listings to Google Base Real Estate is simple and completely free. Today I’ll show how to, using the Google Base .NET library, add listings to Google Base and then search for those listings. The library can be downloaded at http://code.google.com/apis/gdata/client-cs.html.First import the following classes from the Google Base Client library.using Google.GData.Client;using Google.GData.GoogleBase;To insert items, you need to be authenticated. Specify your Google Base account name and password to GBaseService as follows. You can use your Gmail account but you have to first sign into Google Base to activate the account.GBaseService service = new GBaseService("Application Name", developerKey);service.setUserCredentials("username", "password"); The next step is to create the GBaseEntry you would like to insert.GBaseEntry entry = new GBaseEntry();entry.Title.Text = "My Title";entry.Content.Content = description;It is important to set the ItemType to "Housing".entry.GBaseAttributes.ItemType = "Housing"; Now set the other attributes. To see a list of all the Google Base attributes go to http://base.google.com/support/bin/answer.py?hl=en&answer=78170.FloatUnit unit = new FloatUnit(price, "usd");entry.GBaseAttributes.Price = unit;entry.GBaseAttributes.AddTextAttribute("feature", feature);entry.GBaseAttributes.AddImageLink(imgLink);entry.GBaseAttributes.AddTextAttribute("property_type", propertyType);entry.GBaseAttributes.AddTextAttribute("listing_type", listType);entry.GBaseAttributes.AddTextAttribute("listing_status", listStatus);FloatUnit sunit = new FloatUnit(sqft, "square ft.");entry.GBaseAttributes.AddFloatUnitAttribute("square_feet", sunit);entry.GBaseAttributes.AddFloatAttribute("bathrooms", baths);entry.GBaseAttributes.AddIntAttribute("bedrooms", beds);entry.GBaseAttributes.Location = "Address, City, State xxxxx USA ";entry.GBaseAttributes.AddTextAttribute("mls_listing_id", listingID);entry.GBaseAttributes.AddTextAttribute("broker", listBroker);entry.GBaseAttributes.AddTextAttribute("agent", listAgent); Finally, insert this GBaseEntry object into the Items feed. GBaseEntry ent = service.Insert(GBaseUriFactory.Default.ItemsFeedUri, entry); The method service.Insert returns the item you just inserted. The Google Base server assigns pre-computed attributes for your entry such as the creation date and time, the author, and most importantly, the identifier (or URL) of the new entry. The entry may take a few hours to publish so it may not be searchable immediately.Now you can search for your listings on Google Base by connecting to a snippets feed URL and then interpreting the resulting atom feed. This can be done programmatically by creating a GBaseService object and executing a query on it.GBaseService service = new GBaseService("Application Name", developerKey);GBaseQuery query = new GBaseQuery(GBaseUriFactory.Default.SnippetsFeedUri); The following query will get single family homes for sale within a 5 mile radius of San Francisco CA.query.GoogleBaseQuery = @"[item type:housing] [listing type:for sale] [property_type:single] [location:@""San Francisco, CA, USA"" + 5mi]";You can also set the sort by and order, number of results, and search start index.query.NumberToRetrieve = 20;query.StartIndex = 0;query.AscendingOrder = true;query.OrderBy = "price(float USD)";Lastly, execute the query and save the atom feed into a readable xml format. This can be parsed and displayed on a web page.XmlTextReader reader = new XmlTextReader(query.Uri.AbsoluteUri);XmlDocument doc = new XmlDocument();doc.Load(reader); Pretty simple right? Now you can have your listings searchable on Google along with your website.