How to Create a Dynamic Google Sitemap ?

Written by omerkamal on Dec 17, 2006
This article about creating a google sitemap programatically

Introducation:

Google has a handy tool called Google Sitemaps. this tool lets you specify a page that google can check on an interval to get a list of pages on your site along with an indicator of how important each page is. This web page needs to display in a special format that google's sitemap tool understands, and this tutorial shows how to create one dynamically using ASP.NET and C#!

Explanation:

We will directly go to the usefull code and create a Class call "GoogleSiteMapItem". It consist of Five (5) Properties and Two (2) Class Constructor. This class is a simple business object that contains row data for the googlesitemap.

 

Create a new Web application or in your Web Application select the "App-Code"  Folder and Create a New Class. Name this Class "GoogleSitemapItem".

 

GoogleSiteMapItem Class;

 

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

    public class GoogleSitemapItem
    {
        public GoogleSitemapItem()
        { }

        public GoogleSitemapItem(string loc, string lastMod, string changeFreq, string priority)
        {
            Loc = loc;
            LastMod = lastMod;
            ChangeFreq = changeFreq;
            Priority = priority;
        }
       
        private string _loc;

        public string Loc
        {
            get { return _loc; }
            set { _loc = value; }
        }

        private string _lastMod;

        public string LastMod
        {
            get { return _lastMod; }
            set { _lastMod = value; }
        }

        private string _changeFreq;

        public string ChangeFreq
        {
            get { return _changeFreq; }
            set { _changeFreq = value; }
        }

        private string _priority;

        public string Priority
        {
            get { return _priority; }
            set { _priority = value; }
        }
    } 

 

 

In your Main application Create a New Page "GoogleSitemap.aspx". Select also the "Code Behind" Check box from the File Creation Dialoge.


 

 

GoogleSiteMap Web Form;

 

Copy the Following Code to your Page.

 

<%@ Page Language="C#" ContentType="text/xml" AutoEventWireup="true" CodeFile="GoogleSitemap.aspx.cs" Inherits="GoogleSitemap" %>

 
   
   
 

 
  
    <%#Eval("Loc")%>
    <%#Eval("LastMod")%>
    <%#Eval("ChangeFreq")%>
    <%#Eval("Priority")%>
  
 
 

 
   
 

 

GoogleSiteMap Code Behind;

 

This is where we get to define our site pages, and where the GoogleSiteMapItem objects you created come into play.

 

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using DevDistrict.Data;
using DevDistrict.BusinessObjects;

public partial class GoogleSitemap : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        LoadData();
    }

    private void LoadData()
    {       
        List urls = new List();
       
        //Dotnet-Friends
        urls.Add(new GoogleSitemapItem("https://dotnet-friends.com/",DateTime.Today.ToString("yyyy-MM-dd"),"daily","1.0"));
        urls.Add(new GoogleSitemapItem("https://dotnet-friends.com/Code.aspx", DateTime.Today.ToString("yyyy-MM-dd"), "daily", "0.9"));
        urls.Add(new GoogleSitemapItem("https://dotnet-friends.com/Download.aspx", DateTime.Today.ToString("yyyy-MM-dd"), "daily", "0.8"));
       
        //Specific to DevDistrict. This grabs all the articles (code snippets) on DevDistrict and
        //creates a GoogleSitemapItem for each of them, since they are all dynamic pages this ensures they get
        //get some Google facetime.
        List

articles = DataController.GetAllArticles();
        foreach(Article a in articles)
        {
            urls.Add(new GoogleSitemapItem("https://dotnet-friends.com/CodeDetails.aspx?A=" + a.ArticleId.ToString(), DateTime.Today.ToString("yyyy-MM-dd"), "monthly", "0.8"));   
        }

        urls.Add(new GoogleSitemapItem("https://dotnet-friends.com/Links.aspx", DateTime.Today.ToString("yyyy-MM-dd"), "daily", "0.6"));

        urls.Add(new GoogleSitemapItem("https://dotnet-friends.com/About.aspx", DateTime.Today.ToString("yyyy-MM-dd"), "monthly", "0.5"));
        urls.Add(new GoogleSitemapItem("https://dotnet-friends.com/Contact.aspx", DateTime.Today.ToString("yyyy-MM-dd"), "monthly", "0.5"));
        urls.Add(new GoogleSitemapItem("https://dotnet-friends.com/Disclaimer.aspx", DateTime.Today.ToString("yyyy-MM-dd"), "monthly", "0.5"));
        urls.Add(new GoogleSitemapItem("https://dotnet-friends.com/Privacy.aspx", DateTime.Today.ToString("yyyy-MM-dd"), "monthly", "0.5"));
        urls.Add(new GoogleSitemapItem("https://dotnet-friends.com/Search.aspx", DateTime.Today.ToString("yyyy-MM-dd"), "monthly", "0.5"));

        SiteMapRpt.DataSource = urls;
        SiteMapRpt.DataBind();
    }
}

 

This code creates a List of GoogleSiteMapItem objects. Each page of my site is then added to the list as a GoogleSiteMapItem. At the end the List is bound to the DataRepeater control on our page and presto we are done!

Note that this code always tells google that the page was updated at the current date/time. This is just for your reference, feel free to pull these values out of a database or some other data store.

 

Visitors/Readers Comments
(for questions please use The Forum)



joel

Thats neat. Google has a couple of api's available at code.google.com now and haven't really seen many articles on how to use'em in conjunction with asp.net. (of course js is independent).

19/06/2007 00:45:49 UTC

balasubramaniam
Hi
 
        The above information is useful after creating the google sitemap what to do..i need steps
thankyou in advance



regardsa
k.Balasubramaniam

04/03/2008 05:05:27 UTC

balasubramaniam
Hi
 
        The above information is useful after creating the google sitemap what to do..i need steps
thankyou in advance



regards
k.Balasubramaniam

04/03/2008 05:06:01 UTC

InterviewsWorld
Hi

Good Article.
http://www.interviewsworld.com
 

19/08/2008 04:09:49 UTC

Sam Miller
Good article

http://www.walkersresearch.com/BusinessEmailLists.asp

28/08/2008 09:46:40 UTC




Add your Comments

Name:  
Message:


Advertise Here
For more details
Contact Us

Advertisments