How to add a link to MOSS My Links using the Object Model

Posted Monday, June 25, 2007 2:05 PM by C-Dog's .NET Tip of the Day

This is relatively straight forward but it took me a little while to figure out since I am just not all that familiar with the MOSS/WSS object model yet. The first thing to know is that MOSS refers to My Links as QuickLinks. Once you know that it makes it easier to find the needed objects. The key to working with QuickLinks is the QuickLinkManager. However before you can create one of these you have to get access to the UserProfile object of the current user.

Start by creating a UserProfileManager and pass in the current server context.

UserProfileManager userProfileManager = new 
UserProfileManager(ServerContext.Current);

Next you can use this object to get access to the current user profile. This method can also be used to get other users as well. In this case, the true parameter tells MOSS to create the User Profile in the MOSS store if the user doesn't have one already.

UserProfile userProfile = userProfileManager.GetUserProfile(true);

Now you can finally acess the QuickLinkManager by passing it the UserProfile object.

QuickLinkManager quickLinkManager = new 
QuickLinkManager(userProfile);

The QuickLinkManager class has a Create method that takes the title of the page, the URL, which group to use (QuickLinkGroupType.General by default) and the accessibility of the links (i.e. public, private, etc.).

quickLinkManager.Create(SPContext.Current.File.Title, 
Page.Request.Url.ToString(), 
QuickLinkGroupType.General, null, Privacy.Private);

Hopefully this will help someone out there that needs to implement this functionality. Make sure that none of the values are null (i.e.: Title or Url), because it will cause an exception that does not at all indicate what the actual error is. As always be sure and use CAS on your class as described in my prior post.

Read the complete post at http://www.dotnettipoftheday.com/blog.aspx?id=368