Scenario:
We need to display / render titles instead of the URL’s in “WhatsPopularWebPart”.
Resolution:
Google the problem and found following blog with an excellent idea how to accomplish above:
http://rolandoldengarm.wordpress.com/2011/04/08/whatspopularwebpart-render-titles-instead-of-urls/
As per “Roland Oldengarm” already stated that we “to created a webpart derived from WhatsPopularWebPart and implemented also ICallbackEventHandler”. Started working on it and found some implementation issues. First, we need to change the Regex and second, we need to change the code.
public class TestWebPart :
Microsoft.Office.Server.WebAnalytics.Reporting.WhatsPopularWebPart, ICallbackEventHandler
{
private const string ItemRegEx = @”<span dir=’ltr’>(?<url>[^<]*)</span>”;
private static string ReplaceUrlsWithTitles(string html)
{
if (Regex.IsMatch(html, ItemRegEx))
{
html = Regex.Replace(html, ItemRegEx, delegate(Match match)
{
var url = match.Groups["url"].Value;
string title = url;
using (SPSite spSite = new SPSite(SPContext.Current.Site.Url))
{
using (SPWeb spWeb = spSite.OpenWeb(title))
{
try
{
var item = spWeb.GetListItem(title);
if (item != null)
{
title = item.Title;
}
}
catch (Exception e) { }
}
}
return
“<span>” + title + “</span>”;
},RegexOptions.IgnoreCase);
}
return html;
}
string ICallbackEventHandler.GetCallbackResult()
{
return ReplaceUrlsWithTitles(base.GetCallbackResult());
}
void ICallbackEventHandler.RaiseCallbackEvent(string eventArgument)
{
base.RaiseCallbackEvent(eventArgument);
}
}