Retrieving user details from Sharepoint 2007 using Javascript

As part of our Tin Can test implementation we needed a way to identify the current Sharepoint user via Javascript. This is so we could pass that user’s credentials as part of the Tin Can message.

We could have hand-coded some solution (probably) but why bother when you can stand on the shoulders of giants. In this case the giant we leant on was Marc Anderson and his fantastic SPServices library.

Getting Started

SPServices provides a way to interact with Sharepoint via JQuery. This means before you can use you need to add both the JQuery and SPServices libraries to your page.

If you don’t have high enough access to add these libraries at some default/server level then you need to manually add directly to each page that needs them. This is easy to do via script reference in a Content Editor webpart. You can either reference the libraries online or download copies and store locally in a sharepoint library. We always download and save within our site (usually in a dedicated /scripts document library) so the code will be fully operable even for staff without internet access.

To reference your libraries just add a Content Editor Webpart, edit the source and add the Javascript references:

<script src="path/to/jquery-1.9.1.min.js"></script>
<script src="path/to/SPServices-0.7.2.min.js"></script>

Refer to the SPServices documentation for some constraints over which versions of each library play well together. We had no issues with downloading the current of each.

Retrieving user details

Once the libraries are installed, the command needed to retrieve user details is SPGetCurrentUser. For a detailed description refer to the documentation, as this lists all the values that are obtainable and the code to use.

Essentially the method allows you to retrieve any details that appear on your user profile page. For example: the profile field labelled “Account” can be accessed as the Name user property. Just add as a Content Editor webpart and insert the following via the Edit Source button:

var thisUserAccount = $().SPServices.SPGetCurrentUser({
		 fieldName: "Name",
		 debug: false
});
document.write ("<p>You are logged in as " + thisUserAccount + "</p>");

Refer to the documentation to see how to retrieve all other information, including email address, name, etc.

Retrieving multiple details for the same user

Often you’ll want a few details for your user. You can either add a series of individual calls, or make one call passing the list of values you need as an array. For example, here’s how to obtain the account, name and email address in one go:

var currentUserDetails = $().SPServices.SPGetCurrentUser({
	fieldNames: ["Name", "Title", "EMail"],
	debug: false
});
document.write ("<ul>");
document.write ("<li>Account = " + currentUserDetails['Name'] + "</li>");
document.write ("<li>Name = " + currentUserDetails['Title'] + "</li>");
document.write ("<li>Email = " + currentUserDetails['EMail'] + "</li>");
document.write ("</ul>");

Here’s one I prepared earlier

As a quick start, download the following ZIP file, extract the webpart (.DWP) and import on your page.

SPServices

As long as you’ve an internet connection you should then see your user name, login and email address presented automagically.

Internet access is required because this webpart relies on accessing online instances of the two libraries. Full code is as follows.

<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/jquery.SPServices/
  0.7.2/jquery.SPServices-0.7.2.min.js"></script>

<div style="font-size:larger; margin-top:10px">
Retrieving details of the current user (i.e. you):
<ul>

<script>
var currentUserDetails = $().SPServices.SPGetCurrentUser({
	fieldNames: ["Name", "Title", "EMail"],
	debug: false
});
document.write ("<li>Account = " + currentUserDetails['Name'] + "</li>");
document.write ("<li>Name = " + currentUserDetails['Title'] + "</li>");
document.write ("<li>Email = " + currentUserDetails['EMail'] + "</li>");
</script>

</ul>
</div>

Conclusion

And that’s all there is to it. With just a couple of lines of code (and import of two major libraries!) you can easily obtain all the pertinent user information ready for whatever further processing is required.

One thought on “Retrieving user details from Sharepoint 2007 using Javascript

  1. Kelly Brown

    Excellent. Following your steps managed to get it running in only a few minutes. Thank you. Thank you.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>