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

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
HI,
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.
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
Hello,
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.
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
Hi
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#)?
Thanks
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!
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!!
Anne
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/events/calendar.aspx
/lists/corporate discussion board/allitems.aspx
/media library/forms.allitems.aspx
where these are all under the site collection (e.g. http://localhost).
Thanks
Anne
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
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.
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))
sudhesh (@gsudhesh) said
how to get the corresponding hit counts for the item
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.
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 ?
Thanks,
Dhileep
Farhan Faiz said
Please modify the regix and add capability to replace or remove such urls.
Dhileep said
Hi Farhan,
Thanks for your reply. FYI. I am not telling about the items which are coming as url’s. That is working fine for me. Please see the image posted in this below forum. The webpart showing the site detail at the top.
http://social.msdn.microsoft.com/Forums/en-US/sharepoint2010customization/thread/95d7f753-b1ee-4d20-a20f-7370173f6a74
If that is the one you are mentioning, can you please provide any code sample.
Thanks in advance.
Anonymous said
Hi Dhileep, I’m still googling for solution, did you able to resolve it?
Please let me know.
Thanks!
Anonymous said
you are sexy bro