Friday, July 29, 2011

SharePoint - Counting Social Tags

I found some code on the web for counting social tags, but unfortunately it failed when the page was a "welcome" page. The code below handles that special case. I used a Web Service Reference to the SocialDataService web service, although I probably could have created a (WCF) Service Reference instead.

The method IsWelcomePage() is detailed in my previous post. The method TrimPageFromUrl() is fairly simple, it just changes a URL such as http://spsite/pages/default.aspx to http://spsite/.

private void CountTagsByTermNameAndUrl()
{
using (SocDataSvc.SocialDataService socialDataService = new SocDataSvc.SocialDataService())
{
socialDataService.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials;
socialDataService.Url = GetUrlWithUpdatedDomain(socialDataService.Url, m_siteUrl);
SocDataSvc.SocialTermDetail termDetail;
try
{
termDetail = socialDataService.GetTagTermsOnUrl(m_siteUrl, null)
.Where(detail => detail.Term.Name == m_termName).SingleOrDefault();
if (termDetail == null && IsWelcomePage(m_siteUrl))
{ // this could be the default page, remove the last part of the Url and try again
m_siteUrl = TrimPageFromUrl(m_siteUrl);
termDetail = socialDataService.GetTagTermsOnUrl(m_siteUrl, null)
.Where(detail => detail.Term.Name == m_termName).SingleOrDefault();
}
}
catch (Exception ex)
{
// handle exception
}

// make sure we got at least one result
m_tagCount = (termDetail != null && termDetail.Term.Name == m_termName) ? termDetail.Count : 0;
}
}

No comments: