Enterprise Keywords TaxKeyword Tag Cloud

Posted on Updated on

In this post, i will help you to generate a Tag Cloud based on the TaxKeyword, the default enterprise TAG for SharePoint. This sample is for OOTB or not. I’m using SharePoint Search.

Let’s roll to the search method:

Now that we have the DataTable let´s get the Tags from the TAGS Field

/// <summary>
/// Executes the key word search.
/// </summary>
/// <param name="queryText">The query text.</param>
/// <param name="siteUrl"></param>
/// <param name="queryLimit"></param>
/// <returns></returns>
public DataTable ExecuteKeyWordSearch(string queryText, string siteUrl, int queryLimit)
{
    var retResults = new DataTable(); 
    using (var site = new SPSite(siteUrl))
    {
        using (var query = new KeywordQuery(site))
        {
            query.QueryText = queryText;
            query.KeywordInclusion = KeywordInclusion.AllKeywords;
            query.RowLimit = queryLimit;

            query.SelectProperties.Add("Tags");

            var se = new SearchExecutor();
            var rtc = se.ExecuteQuery(query);
            if (rtc.Count <= 0) return null;
            var results = rtc.Filter("TableType", KnownTableTypes.RelevantResults);
            var resultTables = results as IList<ResultTable> ?? results.ToList();
            if (results != null && resultTables.Count() == 1)
                retResults.Load(resultTables.First(), LoadOption.OverwriteChanges);
            return retResults;
        }
    }
}

Let’s replace all “L0|#” and remove all Guid’s

public List<string> GetSafeTagName(string tag)
{
    tag = tag
        .Replace("L0|#", "");
    tag = Regex.Replace(tag, @"([a-z0-9]{8}[-][a-z0-9]{4}[-][a-z0-9]{4}[-][a-z0-9]{4}[-][a-z0-9]{12})", ""); 
    char[] delimiter = { ';' };
    var parts = tag.Split(delimiter, StringSplitOptions.RemoveEmptyEntries);
    return parts.ToList();
} 

 
var list = new List<string>(); 
foreach (var refiners in from dr in _dt.AsEnumerable()
                         where dr["Tags"].ToString() != string.Empty
                         select GetSafeTagName(dr["Tags"].ToString()))
{
    list.AddRange(refiners);
}
 

After having our tags, let’s take a count for each TAG, I’m taking 11 for my render

var q = (from x in list
 group x by x
     into g
     let count = g.Count()
     orderby count descending
     select new { Value = g.Key, Count = count }).Take(11); 
 

So? Let´s render. I’m having a bug :), but let´s remove the first 2 characters

foreach (var x in q)
{
    x.Value.Substring(2)
    x.Count
} 
 

THE TAG CLOUD

This is an example, but you can refine it. I’m taking the total, that is 11, and passing the xCount

private static string TagLogic(double xCount, double xTotal)
    {
        string ret;
        var count = xCount;
        var percent = (count / xTotal) * 100; 
        if (percent < 20)
        {
            ret = "10";
        }
        else if (percent < 50)
        {
            ret = "20";
        }
        else if (percent < 70)
        {
            ret = "50";
        }

        else
        {
            ret = "70";
        }
        return ret;
    }
} 
Advertisements

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s