Tuesday, January 18, 2011

Adding a Link to My SharePoint Links Programmatically

Today, I am going to talk about how to work with the My Links functionality in SharePoint 2010 using the API. The first thing to know is that the API calls it a QuickLink. Knowing this makes it that much easier on finding information you need in the SDK to work with them. I figured this list worked like a regular SharePoint list, but in fact it is quite a bit different (which is why I am posting today).

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

The first step is to get a reference to the UserProfileManager. You’ll need to pass in a reference to the current SSP by using ServerContext.Current.

UserProfileManager userProfileManager = new UserProfileManager(ServerContext.Current);

After that you get the current user’s profile. Passing true, will create the user’s profile if it doesn’t exist.

UserProfile currentUser = userProfileManager.GetUserProfile(true);

The QuickLinkManager class provides what we need to add, remove, or iterate through the links the user currently has.

QuickLinkManager quickLinkManager = currentUser.QuickLinks;
quickLinkManager.Create("My Quick Link", "http://www.dotnetmafia.com", QuickLinkGroupType.General, null, Privacy.Private);


You can also iterate through the user’s QuickLinks pretty easily using the GetItems() method. For example:

foreach (QuickLink quickLink in quickLinkManager.GetItems())
{
Console.WriteLine(quickLink.Url);
}

No comments: