Navigate Up
Sign In
Supporters of Developer
Web

Take your SharePoint social features to the next level with "I like it" and "Tags" counters - Part 2

Item is currently unrated. Press SHIFT+ENTER to rate this item.1 star selected. Press SHIFT+ENTER to submit. Press TAB to increase rating. Press SHIFT+ESCAPE to leave rating submit mode.2 stars selected. Press SHIFT+ENTER to submit. Press TAB to increase rating. Press SHIFT+TAB to decrease rating. Press SHIFT+ESCAPE to leave rating submit mode.3 stars selected. Press SHIFT+ENTER to submit. Press TAB to increase rating. Press SHIFT+TAB to decrease rating. Press SHIFT+ESCAPE to leave rating submit mode.4 stars selected. Press SHIFT+ENTER to submit. Press TAB to increase rating. Press SHIFT+TAB to decrease rating. Press SHIFT+ESCAPE to leave rating submit mode.5 stars selected. Press SHIFT+ENTER to submit. Press SHIFT+TAB to decrease rating. Press SHIFT+ESCAPE to leave rating submit mode.

You may also be interested in: SHARE Conference for Business Users

 

Editor's note: Contributor Elio Struyf is a SharePoint Consultant at Xylos. Follow him @eliostruyf

2012-04-02-SharePointSocialFeatures-Part02-01a.png

In part 1 I talked about retrieving the number of tags by code. The problem was that the GetTags method from the SocialTagManager class, could only retrieve tags from a specific user.

In this part I'll show you a way to retrieve all the tags for a specific location/page.

Decompile

My first step was to check the SocialTagManager class to see the code of the GetTags methods. The SocialTagManager class can be found in the Microsoft.Office.Server.UserProfiles.dll.

2012-04-02-SharePointSocialFeatures-Part02-01.png

The SocialTagManager class contains 13 GetTags methods, and only four of them are public.

2012-04-02-SharePointSocialFeatures-Part02-02.png

The method that could retrieve all the tags (a maximum of 1000) is the following GetTags(Uri url, Int32 maximumItemsToReturn, SocialItemPrivacy socialItemPrivacy).

2012-04-02-SharePointSocialFeatures-Part02-03.png

It seems that this method does not use the current user of a specific user to filter the tags. The only problem is that this is an internal method.

The solution is to use reflection, this will allow you to make use of the GetTags(Uri url, Int32 maximumItemsToReturn, SocialItemPrivacy socialItemPrivacy) method.

Reflection

More information about reflection can be found here.

The first thing you need to do is retrieve all the nonpublic methods of your SocialTagManager class. This can be done as follows:


// Retrieve the type of the SocialTagManager
var type = typeof(SocialTagManager);
// Get the nonpublic methods
MethodInfo[] methods = type.GetMethods(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly);

Now that you have all the nonpublic methods, you can retrieve the GetTags method. I used the following statement to do this:


MethodInfo method = methods.First(m => m.ToString() == "Microsoft.Office.Server.SocialData.SocialTag[] GetTags(System.Uri, Int32, Microsoft.Office.Server.SocialData.SocialItemPrivacy)");
if (method != null)
{
}

The only thing that remains is to invoke the retrieved GetTags method. This can be done like this:


var itemTags = (SocialTag[])method.Invoke(stm, new object[] { uri, 1000, SocialItemPrivacy.PublicOnly });

When you use these code snippets in the console application from part 1, you should now get other results for the items (if you have enough tags, and likes).

2012-04-02-SharePointSocialFeatures-Part02-04.png

When you compare this result with the first part, you can see that "How To Use This Library" has 2 likes more than in the first part.

2012-04-02-SharePointSocialFeatures-Part02-05.png

Complete Source Code

GetTags Code

Part 3

In the next part I will show you how you can put this in a usercontrol, so that these results can be displayed on all your pages.

Categories: C#; Development

Comments

Notify me of comments to this article

E-mail:
   

Add Comment

Title:

 
Comment:
Email:

   


Name:

 
Url: