Gettings Terms asscoiated to a Specific list item

Posted by Gino Abraham on Geeks with Blogs See other posts from Geeks with Blogs or by Gino Abraham
Published on Thu, 22 Mar 2012 07:16:03 GMT Indexed on 2012/03/22 11:30 UTC
Read the original article Hit count: 257

Filed under:

I had a fancy requirement where i had to get all tags associated to a document set in a document library. The normal tag could webpart was not working when i add it to the document set home page, so planned a custom webpart.

Was checking in net to find a straight forward way to achieve this, but was not lucky enough to get something. Since i didnt get any samples in net, i looked into Microsoft.Sharerpoint.Portal.Webcontrols and found a solution.The socialdataframemanager control in 14Hive/Template/layouts/SocialDataFrame.aspx directed me to the solution. You can get the dll from ISAPI folder. Following Code snippet can get all Terms associated to the List Item given that you have list name and id for the list item.

using System;

using

System.Collections.Generic;

using

System.Linq;

using

System.Text;

using

Microsoft.SharePoint;

using

Microsoft.Office.Server.SocialData;

namespace

TagChecker

{

class Program

{

static void Main(string[] args)

{

// Your site url

string siteUrl = http://contoso;

// List Name

string listName = "DocumentLibrary1";

// List Item Id for which you want to get all terms

int listItemId = 35;

using (SPSite site = new SPSite(siteUrl))

{

using(SPWeb web = site.OpenWeb())

{

SPListItem listItem = web.Lists[listName].GetItemById(listItemId);

string url = string.Empty;

// Based on the list type the url would be formed. Code Sniffed from Micosoft dlls :)

if (listItem.ParentList.BaseType == SPBaseType.DocumentLibrary)

{

url = listItem.Web.Url.TrimEnd(

new char[] { '/' }) + "/" + listItem.Url.TrimStart(new char[] { '/' });

}

else if (SPFileSystemObjectType.Folder == listItem.FileSystemObjectType)

{

url = listItem.Web.Url.TrimEnd(

new char[] { '/' }) + "/" + listItem.Folder.Url.TrimStart(new char[] { '/' });

}

else

{

url = listItem.Web.Url.TrimEnd(

new char[] { '/' }) + "/" + listItem.ParentList.Forms[PAGETYPE.PAGE_DISPLAYFORM].Url.TrimStart(new char[] { '/' }) + "?ID=" + listItem.ID.ToString();

}

SPServiceContext serviceContext = SPServiceContext.GetContext(site);

Uri uri = new Uri(url);

SocialTagManager mgr = new SocialTagManager(serviceContext);

SocialTerm[] terms = mgr.GetTerms(uri);

foreach (SocialTerm term in terms)

{

Console.WriteLine(term.Term.Labels[0].Value );

}

}

}

Console.Read();

}

}

}

Reference dlls added are Microsoft.Sharepoint , Microsoft.Sharepoint.Taxonomy, Microsoft.office.server, Microsoft.Office.Server.UserProfiles from ISAPI folder.

This logic can be used to make a custom tag cloud webpart by taking code from OOB tag cloud, so taht you can have you webpart anywhere in the site and still get Tags added to a specifc libdary/List.

Hope this helps some one.

© Geeks with Blogs or respective owner