Retrieve Tags and Notes for Current User programmatically Sharepoint 2010

Here is a code for a webpart that will display current user's Tags and Notes.

using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Portal.WebControls;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.ApplicationPages;
using Microsoft.SharePoint.Administration;
using Microsoft.Office.Server;
using UP = Microsoft.Office.Server.UserProfiles;
using Microsoft.Office.Server.Administration;
using Microsoft.Office.Server.SocialData;
using Microsoft.SharePoint.Taxonomy;
using System.Web.UI;
using System.Reflection;
using System.Security.Principal;
using System.Text;
using System.IO;


namespace ILikeItPart
{
public partial class ILikeItUserControl : UserControl
{
#region Members
SPSite m_site;
SPWeb m_web;
UP.UserProfile m_up;
#endregion

protected void Page_Load(object sender, EventArgs e)
{
m_site = SPContext.Current.Site;
m_web = m_site.OpenWeb();

//Get profile manager
SPServiceContext m_context = SPServiceContext.GetContext(m_site);
UP.UserProfileManager m_upm = new UP.UserProfileManager(m_context);
//Get user profile for currently logged in user
m_up = null;

try
{
m_up = m_upm.GetUserProfile(HttpContext.Current.User.Identity.Name);
}
catch (Exception ex)
{
debug.InnerHtml += ex.ToString();
return;
}

SocialTagManager stm = new SocialTagManager(m_context);
List<SocialTag> mytags = new List<SocialTag>();
mytags.AddRange(stm.GetTags(m_up));

rpt_ILikeIt.DataSource = mytags;
rpt_ILikeIt.DataBind();
}

protected void rpt_ILikeIt_ItemDataBound(object sender, RepeaterItemEventArgs e)
{

if (e.Item.DataItem != null)
{
SocialTag tag = (SocialTag)e.Item.DataItem;
if (tag.Term.Name == "I like it")
{
SPSite tagSite = new SPSite(tag.Url.ToString());
SPWeb tagWeb = tagSite.OpenWeb();

((HtmlGenericControl)e.Item.FindControl("p_ILikeIt")).InnerHtml = tagWeb.Url;
}}}}}

0 comments:

Post a Comment

Disclaimer

This is a personal weblog. The opinions expressed here represent my own and not those of my employer or anyone else. Should you have any questions or concerns please e-mail me at sharepointprogrammingblogger@gmail.com .

Copyright (c) 2010 @ myshaepointwork.blogspot.com. All rights are reserved.Do Not Copy.

@ Learning SharePoint.com