Digital Colony!

Blogger Label List for FTP Accounts (ASP.NET)

The new version of Blogger includes tags. They call them Labels. Hosted accounts (BlogSpot) get templates which display a list of tags for quick navigation. This feature is not available to FTP accounts. This post is a hack to create a user control in ASP.NET that displays a Label List. Before I do, lets breakdown how labels look on the file system.
  1. Labels reside in a folder. By default that folder is called labels.
  2. Every label gets one file.
  3. The label file name is the same as the label name plus extension.
  4. Inside each file is a copy of each blog with that label.
  5. Blogger places the class name of blogger-labels before the Labels text.
For example, on this site I have a label called Javascript.
http://digitalcolony.com/labels/Javascript.aspx
It resides in the labels folder just outside the blog root. The name of the label is Javascript. The name of the file is Javascript.aspx. As of this writing there is 1 post and therefore 1 instance of blogger-labels in the View Source. With the above facts laid out, this is an overview of how to create your own Label List control.
  1. Using server-side code to handle Files and Directories, open the labels folder.
  2. Collect each file name (minus the file extension).
  3. Open each file and count how many times blogger -labels appears
  4. Write label and count to screen.
Here is my .NET code for the Blogger Label List control. On the .ASPX page that the control will be on add this line to the top to register the control.
<%@ Register Src="~/LabelList.ascx" TagName="LabelList" TagPrefix="dc" %>
And the control on the page gets placed with this line.
<dc:LabelList ID="LabelList1" runat="server" Folder="labels" DisplayCount="true" />
And here is the code for the LabelList.ascx user control.
using System;
using System.Web;
using System.IO;
using System.Text;

public partial class LabelList : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {       
        string rootDir = Server.MapPath("~");
        string labelDirectory = rootDir + folder;
        StringBuilder sb = new StringBuilder();
        if(Directory.Exists(labelDirectory)){
            
            string[] fileList = Directory.GetFiles(labelDirectory,"*.aspx");
            string labelName;
            string labelCount;

            foreach (string file in fileList)
            {
                FileInfo fi = new FileInfo(file);
                labelName = fi.Name.Replace(fi.Extension, "");

                if (displayCount)
                {
                    StreamReader sr = new StreamReader(fi.FullName);
                    string webPage = sr.ReadToEnd();
                    sr.Close();
                    int webPageLen = webPage.Length;
                    webPage = webPage.Replace("bl0gger-labels", "bl0gger-labelsX");
                    int iLabelCount = webPage.Length - webPageLen;
                    labelCount = "(" + iLabelCount.ToString() + ")";
                }
                else
                {
                    labelCount = "";
                }
                folder = folder.Replace("\\","/");
                if (folder.Substring(folder.Length - 1, 1) == "/")
                    folder = folder.Substring(0, folder.Length - 1);
                sb.Append("<a href=\"" + folder + "/" + fi.Name + "\">" + 
labelName + labelCount + "</a><br/>");               
            }            
        } 
        else {
            sb.Append("No labels in folder [" + labelDirectory + "] defined.");
        }
        Response.Write(sb.ToString());
    }

    private string folder;
    public string Folder
    {
        get { return folder; }
        set {
            if (value.Substring(0, 1) == "/")
                folder = value;
            else
                folder = "/" + value;
            folder = folder.Replace("/","\\");
        }
    }
    private bool displayCount;
    public bool DisplayCount
    {
        get { return displayCount; }
        set { displayCount = value; }
    }
}
Note that the code above misspells blogger-labels as to not confuse the count of the Label control that I'm using for this site. If you cut and paste the code, fix that spelling.

PHP and ASP coders should be able to hack up something similar using the above code as a framework.

UPDATE (Feb 2007): I created a version of Blogger Label Lists using Classic ASP.

Labels: ,

AddThis Social Bookmark Button

7 Comments:

Anonymous Anonymous said...

the extension for label files on my ftp blog is "html". Any ideas how I can make them "aspx"?

6/28/2007 8:49 AM

Blogger MAS said...

Settings --> Template --> Blog File Name. Change to .ASPX.

AND

Settings --> Archive
Change archive file to name to .ASPX extension.

It will not work the first time. Blogger has a bug (surprise). Post a dummy blog. Delete it and then post again. It should work then.

6/28/2007 8:55 AM

Blogger HF Blacdian said...

When i copy and paste that code I get some errors. Im using VS2005. Do the code go in the method?

10/29/2007 10:26 AM

Blogger MAS said...

The code is for VS 2005 - ASP.NET 2.0. Without knowing what error you have, I can't advise a solution.

10/29/2007 10:31 AM

Blogger HF Blacdian said...

I found out one problem, Im a visual Basic coder and the method is in C#.

Everytime I upload the file it breaks. Does the template code have to specify the language programmed in order for it to pick up without error the User Control?

10/29/2007 10:52 AM

Blogger MAS said...

Not sure. I never tried hosting a C# control in a VB page. If your site is using VB, it would probably be best to translate the code into VB.

Look at the VB Script code I wrote for Classic ASP as well.

10/29/2007 10:58 AM

Blogger Barbara said...

This is an excellent tool.  We had to made some changes to get the .ascx to work.  Hopefully these changes will help the next person.

**** Start of LabelList.ascx ****
<%@ Control Language="C#" ClassName="LabelList" %>
<%@ Import Namespace="System" %>
<%@ Import Namespace="System.Web" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Text" %>
<script runat=server >
    protected void Page_Load(object sender, EventArgs e)
...
**** Replace Response.Write in middle ****
        //Response.Write(sb.ToString());
    Label1.Text  = sb.ToString();
**** End of LabelList.ascx ****
</script>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>

2/09/2008 12:30 PM

 

Post a Comment

 

Digital Colony Copyright © 1999-2008 XHTML   508
This site uses Blogger, which is not 100% XHTML compliant.
Try...Catch Disclaimer: For brevity many examples do not include error handling. That is your responsibility.