Skip to content

Current User’s Profile Properties…for lazy people

12/27/2010

There are a number of client-side ways to access user profile information for the current user. For example, you can make a call to OOTB SharePoint web services using jQuery. The fantastic Jan Tielens outlines this nicely here. Some folks, like Derren Johnstone, have even been nice enough to create and share javascript APIs for this purpose. I’ve used both of these extensively, but below I offer a walk-thru which is perhaps the easiest way to do this.  Notice I didn’t say the quickest, or best. There are a bunch of steps to do here, but it requires the least know-how so it might be appealing to those non-developers out there.  The idea is basically to configure a data view web part (DVWP) which will store the information on your page, allowing you to access it using client side script. Here are the steps.

1.  Open your site in SharePoint Designer 2010 and click on Data Sources

2.  Click the SOAP Service Connection button and fill out the resulting form as follows

A few things to note here

3.  Next, insert a Data View Web Part onto a page using your new User Profile Web Service Data Connection as the data source.

The result will look something like this:

4.   Now let’s get some more useful data in there. Simply click Add/Remove Columns from the toolbar under Dataview Tools > Options and select columns you want to see. In my case I want the property name and the property value.

That gives us the much more useful

5.  Now, set up a filter to limit the rows to only those you want. Be sure to use the “OR” operator if you want to display multiple properties
 

6.  Now we’re going to add classes to our TDs in the XSLT of our DVWP so that they are easy to identify later.
View your page in Split View so that you can see your code and the web part. Put your cursor in any row and replace the OOTB class with your own.

7.  Now save your webpart to a file and add it to your page.

 

You should now have a web part on your page that looks similar to the following:

Using the IE Developer Toolbar we can see that my custom classes have been applied

You can hide this web part now

This information is now stored on the page, but not displayed to end users.

8.  Now all that’s left to do is grab the information via jQuery. To do this, add a Content Editor web part (CEWP) and paste this code inside.

<script src=”http://code.jquery.com/jquery-1.4.4.js”></script><script language=”JavaScript”>

$(document).ready(function() {

$(“.myPropertyName”).each(function (i) {

if($(this).text() == “AccountName”)

{

 actName = $(this).next().text();

 actName = actName.substring(6);

 }

if($(this).text() == “FirstName”)

{

 firstName = $(this).next().text();

}

if($(this).text() == “LastName”)

{

 lastName = $(this).next().text();

}

      });

alert(“My account name is ” + actName + “. My full name is ” + firstName + lastName +”.”);

});</script>

//

Then you should see something like the following

Now, all I’m doing is displaying this information but you could obviously use it to do anything you want. This is also not limited to the User Profile web service. This will work for any of the SharePoint web services, or your own! Let your imaginations run wild and have fun!

Advertisements
7 Comments
  1. Thanks for the great article. I got my user profile properties, but instead of an alert window, I simply want it to display in the content editor web part. Any advice? I can’t believe how hard it has been to figure out how to do that simple task. I tried document.write but that displays just the text and the rest of the SharePoint page is gone.

    • Hi,

      Sure – that should be easy.

      In a content editor web part add a div and give it an ID.
      When you want to write your information refer to your div. For example [DIV id=”myPropertyDetails”][/DIV] (replace square brackets with greater than/less than brackets).

      Then to write information to it, your code should look something like this:

      $(“#myPropertyDetails”).html(add the html of whate you want to write here)

      Does that help?

  2. Beth permalink

    Hi,
    When I try to insert the Data Source in my Data view, I get the dreaded “Ther server returned a non-specific error when trying to get data from the data source…” I’ve had this happen with a few other data views I’ve created, but I’ve always been able to get them to work (eventually). I’ve tried this in two different test environments and have the same issue. In one of my environments I’m on SP2010 CUFeb2010 and the other is RTM. What login information are you using? The only way I have been able to get this to work is to enter a test user’s credentials, which isn’t secure. I haven’t tried the SSO option yet.

  3. Beth permalink

    Hi,
    I am adding the credentials when I create the Data Source (Step 2 above). Next to the Source Tab is the Login tab which defaults to “Don’t attempt to authenticate”. However, when I tried that or the Windows Authentication options, each of those resulted in that error.

    Beth

Trackbacks & Pingbacks

  1. SharePoint 2010: Recopilatorio de enlaces interesantes (XV)! « Pasión por la tecnología…
  2. SharePoint 2010: Recopilatorio de enlaces interesantes (XV)! - Blog del CIIN

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: