PANVEGA’s Blog

DotNet Development, SharePoint Customizing, Silverlight, MS Infrastructure and other tips and tricks

How to modify the Userprofile´s QuickLinks (MyLinks) with the SP Object Model.

Posted by PANVEGA on November 25, 2008

In this post I wanna explain how to access the UserProfile´s QuickLinks (MyLinks) Sharepoint Personal Sites with the SP Object Model.

  • The best way is to create a custom WebPart with the Visual Studio VSTO template.
  • Add all appropriate references to your project
  • Add your custom code
  • Deploy the Webpart

Here I listed some importand dll references you need, in order to execute the code below.

using Microsoft.SharePoint.Portal;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;
using Microsoft.Office.Server.UserProfiles;
using System.Collections.Specialized;
using System.Web.UI.WebControls;

Here is a code snipped from my custom webpart. You can also replace the Sharepoint Personal Sites with your custom content.

using (SPSite site = new SPSite(“http://moss.litwareinc.com”))
{
using (SPWeb rootWeb = site.OpenWeb())
{
SPUserCollection userCollection = rootWeb.AllUsers;
SPUser currentUser = rootWeb.CurrentUser;
ServerContext context = ServerContext.GetContext(site);
UserProfileManager profileManager = new UserProfileManager(context, true);
UserProfile userProfile = profileManager.GetUserProfile(currentUser.LoginName);
QuickLinkManager qlm = userProfile.QuickLinks;

string title = “GOOGLE”.ToString();
string sLinkUrl = “http://google.de”;
string strGroup = “General”;

// Add my links to Sharepoint Personal Sites
site.AllowUnsafeUpdates = true;
QuickLink ql = qlm.Create(title, sLinkUrl, QuickLinkGroupType.General, strGroup, Privacy.Private);
userProfile.Commit(); }
}

You can also iterate through the QuickLinkManager:

QuickLinkManager qlm = userProfile.QuickLinks;
foreach (QuickLink tmp in qlm.GetItems())
{

//do something (show -, remove QuickLink etc.)
}

Caution!!!

If you receive the following error: “Updates are currently disallowed on GET requests. To allow updates on a GET, set the ‘AllowUnsafeUpdates’ property on SPWeb” even you add site.AllowUnsafeUpdates = true to the code.

Note:

The code must be executed in a postback. Using this knowledge, you can create a simple button and moved the code from the RenderWebpart method to the Button.Click method

The idea behind AllowUnsafeUpdates is that pages retrieved with a GET method are not allowed to make updates.  The reason is that if someone emails you a link or otherwise “tricks” you into clicking a link, any updates the server code makes will happen before the user sees any UI.

By forcing the user to GET a form, then POST a reply back, you can at least tell the user what the results of the action will be.  “WARNING: This will permanently delete you website.  Are you sure?”

If the user POST’s a reply after clicking “Yes, I’m sure”, then you know they had an opportunity to consider your warning.  With a GET method, the action happens without any user interaction.  That’s why it’s considered “Unsafe”.

Advertisements

2 Responses to “How to modify the Userprofile´s QuickLinks (MyLinks) with the SP Object Model.”

  1. Asem Nairat said

    what if i need to display the up-to-date quicklinks, i mean i want each member to recieve messag ethat user x has added new link, such as Facebook feeds!! i dont want to display all the quick links that the user has, just when he added a new quick link, make a notification for the other users such as displaying message in the profile page for each member?? i did everything, but i face a problem in the way to display just the up-to-date quick link?? can u help me in this issue??

  2. PANVEGA said

    I think there is not really a chance to store the up-to-date value foreach QuickLink.

    Give me a response, when if you found any approaches

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: