Wednesday, January 27, 2010

SharePoint 2010 Social Networking Part 1: Setting the user My Site status message programmatically

SharePoint 2010 comes along with a wide range of the social feature like note boards, rating, bookmarking, tagging etc. I’ll be doing a series of blog posts to share some code snippets for managing these cool features programmatically.

One of the interesting social feature is setting the My Site status message which depicts the mood of the user. In the part 1 of this series I’ll demonstrate a small code snippet to set the My Site status message programmatically.

So we’ll start by adding the required assembly references to our console application:

Microsoft.SharePoint.dll
Microsoft.Office.Server.dll
Microsoft.Office.Server.UserProfiles.dll
System.Web

Followed by the following namespace decelerations:

using Microsoft.SharePoint;
using Microsoft.Office.Server;
using Microsoft.Office.Server.UserProfiles;

Now one wearied issue that the users with Visual Studio 2010 beta would notice here is as soon as you compile the solution, you’ll probably notice this error followed by a warning:

image

I wasted a whole day fighting this issue until I found this post by Chris where he points out it as a development issue in VS2010 beta build.

So essentially we would need to add the reference to System.Web.DataVisualization.dll into our project and we are good to go.

Now we’ll add the method as shown below to our application:

  SetStatusMessage

Essentially here we are stating by instantiating the SPSite object for a SharePoint site URL (http://intranet.contoso.com). Then we are creating an object of SPServiceContext class which would enable us make a call to the user profile service application.

Next is to instantiate the UserProfileManager class object for accessing the user profiles and their data stored in the user profile database. The GetUserProfile method of the UserProfileManager class accepts several overloads and returns the user profile for the specified user (in our case the administrator).

Now among the several OOB user profile properties configured on the server, the property SPS-StatusNotes is the one which we are looking for. Setting the value of this property with a string value would set the the status message for the user. The final step is to commit the changes by calling the Commit method.

Now call this method and pass a string message “Status set from code.”. Finally executing our console app would result in setting the status message for administrator as shown in the image below.

 My Site status.

4 comments: