On my journey in the new features of SharePoint 2010, my attention was caught by the so-called ‘Social’ features of SharePoint 2010. In particular, the use of the tagging and rating functionality had my interest.
A client wanted to give end-users a quick way to register favourites on their Intranet. I immediately thought of the ‘I Like It’ button in the Ribbon, where end-users can quickly tag a page that they like. In their profile, they could see all the tags and the url’s where they placed the tags.
Unfortunately, I could not find a web part that could display the tags for a given user for a specific url, or vice versa. The latter is what I needed. And so I decided to write some small code to do that for me.
In fact, the basic principles are simple. We connect to the Users profile, retrieve their tags, get the tag we want to filter on, retrieve the urls for that tag and display those in a list. Easy right?
SharePoint 2010 stores tags entered by users in their user profile. You can view the tags you entered by going to your profile page and review the Tags and Notes section. That sections shows all the notes and tags, as well as a history (activities) trail for that particular tag or note. Simply put, the information is there.
I tried to find the controls SharePoint is actually using to generate these views, but was not able to make those available through a web part. Secondly, I want to be able to control the output and settings, so I will go with my own web part anyway.
Setting up the solution
I would like to keep this post simple, so I will not go into the specific details of formatting and making things configurable. I will just show the principle.
So, start Visual Studio 2010 and create a new Empty SharePoint project. Add a new item of the type Web Part. Give it all some reasonable names and your solution explorer should resemble the following picture:
Open the Favourites.cs file and we will start adding the code. Add the following references to your project:
- Microsoft.Office.Server (needed for the UserProfile servercontext)
- Microsoft.Office.Server.UserProfiles (needed for the UserProfiles)
- Microsoft.SharePoint.Taxonomy (needed for the term store)
- Microsoft.SharePoint (needed for the normal SharePoint objects)
Below code shows the GetUserUrlsByTag method that will retrieve all urls from the current user for a specific tag.
private Uri GetUserUrlsByTag(string tag)
// first get the service context
SPContext ctx = SPContext.Current;
SPServiceContext serviceContext = SPServiceContext.GetContext(ctx.Site);
// Then load the UserProfileManager
UserProfileManager mngr = new
// Get the user’s profile
UserProfile currentProfile = mngr.GetUserProfile(false);
// If the user has a profile, get the SocialTagManager
if (currentProfile != null)
SocialTagManager smngr = new
// Get the terms for the user
SocialTerm terms = smngr.GetTerms(currentProfile);
SocialTerm favTerm = null;
//Iterate through the terms and search for the passed tag
foreach (SocialTerm t in terms)
if (t.Term.Name.ToLower() == tag.ToLower())
favTerm = t;
// If found, get the Urls and return
if (favTerm != null)
return smngr.GetUrls(favTerm.Term, currentProfile);
The method needs to return an array of urls that have a certain tag applied to it. We first set up the context and service context for this site. We then create a UserProfileManager based on the service context and retrieve the UserProfile of the current user.
If the current user has a user profile, we create a SocialTagManager that will perform the bulk of the functionalities we need. We then get all the SocialTerms for this user. It would be better if we could request a specific Term to be retrieved, but unfortunately I have not found a method to do so. So instead, we will need to iterate through the returned terms until we find the one we want to filter on. I would be much obliged if someone know a better method of doing this. For now, it works.
Once we found the term we are interested in, we can request all the url’s for that tag by calling the SocialTagManager.GetUrls method, passing the term and the profile.
And we are done! The web part I coded contains the method call and will output all the url’s in a list. However, you can style it in the any way you want obviously. Now when users click the ‘I like it’ button in the ribbon, it will show up in their favorites web part.
To be complete, the code in the CreateChildControls method of the web part.
protected override void CreateChildControls()
Uri urls = GetUserUrlsByTag(“I Like It”);
HtmlGenericControl table = new
if (urls != null)
foreach (Uri url in urls)
HtmlGenericControl row = new
HtmlGenericControl cell = new
HyperLink link = new HyperLink();
link.NavigateUrl = url.AbsoluteUri;
link.Text = url.AbsoluteUri;
Label lbl = new Label();
lbl.Text = “No favourites found.”;
catch (SPException ex)
Label lbl = new Label();
lbl.Text = ex.ToString();
And finally the output:
Improvements on this code
So, a couple of things can obviously be improved in this little piece of code.
- Make the input and output configurable. So selecting the term to filter on and exporting the output through XML, which allows us to use XSL for the rendering.
- Get details about the url’s in the profile. For example, we could retrieve the page title rather than the url by obtaining that from the page instance in the pages library.
- Add a button or control to the ribbon or expose that as a separate web part that does the favourite tagging. That way we can control the term we call favourite (for example, MyFavourite instead of I like it)
- Improve the filtering on the terms. As soon as a user gets a lot of terms, the filtering might slow down.
The new Social framework of SharePoint 2010 can be used for very neat things. To bad some of the features are not exposed more through web parts, but I am sure the community will provide their own sets of controls that expose the power behind Social tagging and rating. You can find the VS project for this example here.
Till next time!