Thursday, July 7, 2011

Scanning through tags and notes, comments and User profile data in Sharepoint 2010

I've come across a task where by I need to write a timer job to scan through User profile data and all the social networking tags and notes to policing whether any user has put in any vulgarities or inappropriate comments. After going through the API and some of the testing on my own this is a small prototype of my findings. The program can tell which user has made what kind of changes for all these data. If anyone out there looking for something similar to what I needed to be done I guess this would help someone's live a little easier. :)

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Office.Server;
using Microsoft.Office.Server.Administration;
using Microsoft.Office.Server.UserProfiles;
using Microsoft.SharePoint;
using System.Web;
using Microsoft.Office.Server.SocialData;

namespace UserProfileChanges
{
class Program
{
static void Main(string[] args)
{
using (SPSite site = new SPSite("http://yourserver"))
{
SPServiceContext context = SPServiceContext.GetContext(site);
UserProfileManager profileManager =
new UserProfileManager(context);

// this gets some subset of changes to a user profile

DateTime startDate =
DateTime.UtcNow.Subtract(TimeSpan.FromDays(5));
UserProfileChangeQuery changeQuery =
new UserProfileChangeQuery(false, true);
UserProfileChangeToken changeToken =
new UserProfileChangeToken(startDate);
changeQuery.ChangeTokenStart = changeToken;
changeQuery.Anniversary = false;
changeQuery.SingleValueProperty = true;
changeQuery.MultiValueProperty = true;
changeQuery.DistributionListMembership = false;
changeQuery.SiteMembership = false;
changeQuery.UpdateMetadata = true;

UserProfileChangeCollection changes =
profileManager.GetChanges(changeQuery);
foreach (UserProfileChange change in changes)
{
Console.WriteLine("Changes occurs at "+change.EventTime.ToString());
if (change is UserProfilePropertyValueChange)
{
UserProfilePropertyValueChange propertyChange =
(UserProfilePropertyValueChange)change;
if (propertyChange.ChangeType.ToString() == "Metadata")
{
Console.WriteLine("Change Type " + propertyChange.ChangeType.ToString());
Console.WriteLine("Name " + propertyChange.ProfileProperty.Name);
Console.WriteLine("Account Name" + propertyChange.AccountName);
Microsoft.Office.Server.UserProfiles.UserProfile u = profileManager.GetUserProfile(propertyChange.AccountName);
Console.WriteLine("Property Value " + u[propertyChange.ProfileProperty.Name].ToString());
Console.WriteLine();
Console.WriteLine();
}
}
}

UserProfileManager myUserProfileManager = new UserProfileManager(context);
foreach (UserProfile myUserProfile in myUserProfileManager)
{
SocialTagManager mySocialTagManager = new SocialTagManager(context);
SocialTag[] tags = mySocialTagManager.GetTags(myUserProfile);
Console.WriteLine("Tags for user:");
foreach (SocialTag tag in tags)
{
Console.WriteLine(tag.Term.Name + ": " + tag.Url);
Console.WriteLine(tag.Owner.DisplayName);
}

SocialCommentManager mySocialCommentManager = new SocialCommentManager(context);
SocialComment[] comments = mySocialCommentManager.GetComments(myUserProfile);
Console.WriteLine("Comments for user:");
foreach (SocialComment comment in comments)
{
Console.WriteLine(comment.Url + ": " + comment.Comment);
Console.WriteLine(comment.Owner.DisplayName);
}
}


Console.Read();
}

}
}
}

5 comments:

Jinesh said...

Hello,
The same thing i need to do in my task.
and i getting current login user's tag details..but i cant access other userprofile tag details.

any idea is that something i m missing ??

thanx.
Jinesh

Leo Lu said...

If you are doing this in the web part instead of console application you might face this issue. It should has to do with the security trimming feature. When I try to write custom noteboard I face the same kind of issue. Quite irritating. I think 1 way around for you is to find out whether there is any sharepoint web services to get the tags.

If you are using the console application then it might has something to do with the way you get tags. I think there is a few different parameters you can pass in to get the tags. Hope it helps. =)

Devendra Singh said...

using the same code as you posted but in my case, returned rows are exceeded the 1000 limit of UserProfileChangeQuery.Could you please help me, to get record change no. 1001 and onward using c# code.

Leo Lu said...

I think sharepoint only fetches recent number of changes. The changetoken start date you can try offseting more than 5 days if you need to fetch more. For my case I set 5. It might not be a gd idea to get all the changes in 1 go as it might return lots of data anyway. I haven't play around too much on it by the way. :)

Unknown said...

you've an excellent blog here! would you wish to make some invite posts on my blog? online casino slots