Adding category titles to Blog sites (category.aspx)

I recently found an application of the Blog template that was a great fit, but needed the Category title listed on the top of the page when you click a category from the default page.  Normally, it is not visible anywhere except in the URL as a parameter, and in the categories of the posts listed. 

The primary content of the page is displayed by a ListViewWebPart embedded in a table in the aspx file.  It starts like this:

<table cellpadding=0 cellspacing=0 style="padding: 5px 10px 10px 10px;">
           <tr>
            <td valign=top>
                <WebPartPages:WebPartZone runat="server" FrameType="TitleBarOnly" ID="Left" Title="loc:Left"><ZoneTemplate>
<WebPartPages:ListViewWebPart runat="server" __MarkupType="xmlmarkup" WebPart="true" __WebPartId="{973762DE-3C5C-47B0-B80C-895B16BEDD6E}" >
<WebPart xmlns:xsi="
http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.microsoft.com/WebPart/v2">
  <Title>Posts</Title>
  <FrameType>None</FrameType>
  <Description>Use the Posts list for posts in this blog.</Description>

I added the following table row to the table above (before the webpart's row) to display it.

           <tr>
            <td><script language="javascript">
function getURLParam()
    {
        var strReturn = "null";
          var strHref = window.location.href;
        if ( strHref.indexOf("?") > -1 )
        {
            var strQueryString = strHref.substr(strHref.indexOf("?")+1);
            var aQueryString = strQueryString.split("&");
            for ( var iParam = 0; iParam < aQueryString.length; iParam++ )
            {
              if (aQueryString[iParam].indexOf("Name=") > -1 )
              {
                var aParam = aQueryString[iParam].split("=");
                strReturn = aParam[1];
                break;
              }
            }
            var fields,i;
              fields = document.getElementsByTagName('DIV');
              for( i = 0; i < fields.length; i ++ )
              {
                if(fieldsIdea.innerHTML.indexOf("::") != -1)
                {
                     fieldsIdea.innerHTML = unescape(strReturn);
                }         
            }
         }
    }
    _spBodyOnLoadFunctionNames.push("getURLParam");
            </script>
             <div class="ms-sitetitle">::</div></td>
           </tr>

Note the use of the _spBodyOnLoadFunctionNames.push method to fire the script... This is documented on the SPD team's blog here.  This is so you can embed your javascript in master or aspx and have it execute correctly once the entire page is loaded.  I probably could have gotten away without it here, but used it to be safe. 

The script basically just looks for the <DIV> tag containing "::" and replaces it with the unescaped text from the "Name" parameter in the URL (Looks like http://MySharePointServer/sites/ICS/blog/category.aspx?Name=General%20Questions).  That is how the category is passed from the hyperlink to the category.aspx page.


Posted on SharePoint Blogs Del.icio.us | Digg It | Technorati | Blinklist | Furl | reddit | DotNetKicks

Read the complete post at http://www.sharepointblogs.com/echef/archive/2007/12/27/adding-category-titles-to-blog-sites-category-aspx.aspx

Published Thursday, December 27, 2007 2:05 PM by SharePoint Blogs
Filed under: , ,