SharePoint 2010: Display titles instead of URL’s in WhatsPopularWebPart


We need to display / render titles instead of the URL’s in “WhatsPopularWebPart”.


Google the problem and found following blog with an excellent idea how to accomplish above:

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))




var item = spWeb.GetListItem(title);

if (item != null)


title = item.Title;



catch (Exception e) { }



“<span>” + title + “</span>”;



return html;


string ICallbackEventHandler.GetCallbackResult()


return ReplaceUrlsWithTitles(base.GetCallbackResult());


void ICallbackEventHandler.RaiseCallbackEvent(string eventArgument)







  1. rolandoldengarm said

    Thanks for your pingback and link 🙂 After testing I saw my code was not correct, because I retrieve the listitem from the rootweb. That will go wrong if the item is in a subweb…
    I will change that part 🙂

    • Anonymous said

      Hello, I am just waiting for your updated code to retrieve the item in subweb.

      • Farhan Faiz said

        I implemented with subweb approach. It should retrieve items from subwebs as well. Let me know if you are having any error.

      • Anonymous said

        Hello Farhan,
        Is it the same code working for sub-web?
        Or do you have another copy of code?
        Thanks for the quick reply 🙂

    • Swamy said

      Is this supports only SharePoint 2010? or can we implement same functionality in MOSS 2007?

      • Farhan Faiz said

        Never tried myself but on papers, it should.

  2. Anonymous said

    Hello Farhan,
    It is the same code which works for sub-web?
    Plz share

    • Farhan Faiz said

      Yup. Above code should work for sub-webs. If it is not working, please let me know.

      • Anonymous said

        We are facing strange issue in 2 enviornments.

        One shows the full url in the Web Analytics Web Part.
        e.g.: http://server/pages/waiting-order.aspx

        But the other shows just /pages/waiting-order.aspx

        Any idea, why so?

      • Farhan Faiz said

        Seems strange. Can you please confirm HTML link text and Navigation URL are same or different. You can use Developer Toll in IE. Also, what I can recommend is to manually append the web URL if not already there.

  3. Anne said

    Hi, I have recently used your code to create my first custom webpart in SP2010 (yay!). However, when I add the webpart to my page, it is still showing the url of the items, not the title. I have placed it next to the OOT WhatsPopularWebpart and they look identical. Do you have any ideas why the url is not being replaced with the title? Thanks.

    • Farhan Faiz said

      Can you please add some text with the string “title” and see if it works or not? Let me know.

      also, congrats for 1st webpart in SharePoint 2010. Hopefully you will enjoy working in SharePoint 2010 🙂

      • Anne said

        Thanks for replying. I found I had an error in the syntax of the ItemRegEx – I had copied your code above and the single quotes marks around ltr were wrong – once I retyped them from my keyboard it fixed the problem…well sort of!
        The code is now working for some content and not others e.g. if the content is a list view the html does not match (/lists/mylist/allitems.aspx). Is this expected? In reality we probably don’t want to include this sort of content in the list, but the only way to block content in the web part editor is by “Exclude item title or search query containing” which would be impossible to manage. Is there anyway this could be achieved in the code (I am new to c#)?

      • Farhan Faiz said

        Good to hear that It started working. Well, we faced the same scenario. We opted to go with URL as the probability of displaying such pages will decrease as users start accessing the site. If you are not this much lucky :(, then, you can write code to remove such pages all together or replace them with predefined text. Let me know which option is appropriate and will try to help.

      • Anonymous said

        Hi Anna, could you please share the code to hide unnecisary links like calendar, pictures, etc. Appreciate it!

  4. Anne said

    Ideally we would want to exclude list pages from appearing altogether – I agree that in practice they would be less likely to appear as time goes on, but this may not be true for our discussion board which is used heavily and which we have been expressly asked to exclude from this webpart.
    Any help would be much appreciated!!

    • Farhan Faiz said

      In this case, as per my understanding, the only way left is to write code to remove links from the webpart. Send me the links and hopefully, i will come up with something on this weekend.

      • Anne said

        Some examples would be…
        /lists/corporate discussion board/allitems.aspx
        /media library/forms.allitems.aspx
        where these are all under the site collection (e.g. http://localhost).

      • Lexx said

        Hi Farhan,
        This still actual.. Could you please provide code for this, me need to remove same links like calendar.aspx, allitems.aspx, etc. Appreciated for help!

      • Farhan Faiz said

        I am not able to find the code 😦 but just before links are loaded, wrote checks (if statements). Please let me know if further assistance is desired

  5. tinker said

    could you please share the webpart ?

    • Farhan Faiz said

      You can just copy/paste the code and it will work. If not, I will try to find out the webpart in SVN on weekend.

  6. Anonymous said

    Great piece of code!

    A small change made it work for me:
    The Regex.Replace already has the IgnoreCase.
    I added the IgnoreCase also when testing for a match:
    if (Regex.IsMatch(html, ItemRegEx, RegexOptions.IgnoreCase))

  7. how to get the corresponding hit counts for the item

  8. Anup said

    I followed the same steps but still i am getting the URLs. Could you please help me with this?

    • Farhan Faiz said

      Check the regex using some regex editor. Some times we need to change regex.

  9. Dhileep said

    Thanks for Very helpful post. I have one more requirement, to hide the site url coming at the top of the webpart for eg: (under http://servername:1234/sites/Test).
    Do you have any idea on this ?

    • Farhan Faiz said

      Please modify the regix and add capability to replace or remove such urls.

    • Anonymous said

      Hi Dhileep, I’m still googling for solution, did you able to resolve it?
      Please let me know.

  10. Anonymous said

    you are sexy bro 🙂

  11. Anonymous said

    this is working only for site scope : this site collection , but if you choose any other scope it will not return any data?

    • Farhan Faiz said

      This is designed for site collection.

RSS feed for comments on this post · TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: