Power User Toolbox: JavaScript for SharePoint – Pt5
When I first installed SharePoint, I was amazed with the many ways I could access data (your mileage may vary depending on licensing options): user interface, web services, SharePoint Services API, RSS, email alerts, workflows, Business Data Catalog, Excel Services, Stssync protocol (Outlook Integration), Stsadm command line, T-SQL (unsupported), and RPC/URL protocols (owssvr.dll). Well, add to that list (and your Power User Toolbox) the SharePoint and Office Live JavaScript API.
Darren Johnstone created a JavaScript library that talks to SharePoint using the native web services. Here’s an example that pulls the current user’s name and displays it:

<script type="text/javascript" src="/PaulGrenier/js/SPAPI/SPAPI_Core.js"></script> <script type="text/javascript" src="/PaulGrenier/js/SPAPI/SPAPI_Lists.js"></script> <div id="username_div"></div> <script type="text/javascript"> _spBodyOnLoadFunctionNames.push('showUserName'); function showUserName(){ var userName = getCurrentUserName(); if (userName != null){ document.getElementById('username_div').innerHTML = 'Welcome: ' + userName; }else{ document.getElementById('username_div').innerHTML = 'User name not found'; } } function getCurrentUserName(){ var lists = new SPAPI_Lists('https://endusersharepoint.securespsites.com/PaulGrenier'); var items = lists.getListItems( '2D3F492B-C6E9-4608-82FA-DCAE42B0C57F', //other lists can be called by name '', '<Query><Where><Eq><FieldRef Name="ID"/><Value Type="Counter">' + _spUserId + '</Value></Eq></Where></Query>', // query '', 1, // rowLimit '' // queryOptions ); if (items.status == 200){ var rows = items.responseXML.getElementsByTagName('z:row'); if (rows.length == 1){ return rows[0].getAttribute('ows_Title'); //field name of data to return }else{ return null; } }else{ return null; } } </script>
Just like with jQuery, we have to reference some scripts that we uploaded to the server. After that, the code has a little div tag to display the returned information–but that’s the last step, no reading ahead! You’ll miss the cool parts.
The first line of JavaScript uses a built-in SharePoint function you probably noticed in Solution 1 of part 4, _spBodyOnLoadFunctionNames.push. That just tells SharePoint that we want to run this script when the page loads. That calls the first function we wrote, showUserName.
showUserName calls the last function, getCurrentUserName, and stores the returned value in a variable called userName. Within the last function, we have to call Darren’s library functions: SPAPI_Lists and getListItems to establish the list we’re talking to and to grab the returned XML.
With Darren’s libraries, we can normally use the list name (very cool) instead of the Globally Unique Identifier (GUID) as a parameter in the SPAPI_Lists function but I was not able to make that work in this situation. I have an easy way to find the GUID for the User Information List that I’ll show you later.
In the middle of getListItems, we use another built-in SharePoint function, _spUserId, to filter the returned rows by the current user’s ID. To understand the next few steps, it helps to see what the XML the web service returns. To see that, we can use the URL protocol.
This trick works on any site and with most permission levels. Click the People and Groups link and then click All People. Now view the page’s source and search for owssvr.dll. Since the User Information List isn’t a normal SharePoint list, the page uses the URL protocol to retrieve the data.

Notice the value of the List parameter, that’s the User Information List’s GUID. Copy that entire URL after o:webquerysourcehref and paste it into your browser’s address bar. If you don’t see many records, remove the &View=… portion of the URL and try again. This displays an XML file with helpful information. Here’s a sample returned row:

See the z:row element? The script above uses getElementsByTagName(’z:row’) to get that data. In the next step, we extract the attribute ows_Title but we could easily change that to pull ows_EMail or ows_Name instead.
In the last step, after getting a value for the userName variable, we simply find the empty div we made and insert the value as innerHTML.
- Power User Toolbox: JavaScript for SharePoint - Pt1
- Power User Toolbox: JavaScript for SharePoint - Pt2
- Power User Toolbox: JavaScript for SharePoint - Pt3
- Power User Toolbox: JavaScript for SharePoint - Pt4
- Power User Toolbox: JavaScript for SharePoint - Pt5
- Power User Toolbox: JavaScript for SharePoint - Pt6
- Power User Toolbox: JavaScript for SharePoint - Pt7
- Power User Toolbox: JavaScript for SharePoint - Pt8
- Power User Toolbox: JavaScript for SharePoint - Pt9
Hi,
Sorry for asking this question. What is the main aim of these parts on Power User ToolBox: Java Script on SharePoint.
Thanks,
Rao.
Rao,
While a simple “Welcome <>” web part may seem useless, it’s merely a demonstration of a powerful option available without .NET code or access to SPD that brings data to the page from an “outside” source.
For instance, combine the ability to bring the user’s manager’s name from an SP list (using the JavaScript API) and the ‘pre-populate a form field’ script from pt4. That’s something that a lot of people ask for–”automatically put my manager as the approver for a manually started workflow.”
Without hard specs to develop toward (or rather before you get the specs in hand), I feel it’s important to have a “toolbox” full of options. I didn’t want this series to start out with “solutions” but rather provide some building blocks and see how our readers put them together.
Of course, if you have specific requests, I’m all ears.
Paul
This looks really useful, but how does it work if all you get back from the User Information List are Windows Groups rather than Windows Users?
I’ve figured you can use the MembershipGroupId to limit the list to just check specific SharePoint Groups for a user (as opposed to the generic User Information List), but if the user is part of an AD Group (as per MS Good Practice for SharePoint) I don’t think the user will be found by Darren’s code, will it?
Krys Kujawa,
Accessing AD through a web service requires a web service for AD. That’s usually a C# or ASP.NET page that turns a web request into an LDAP query (from what I understand). There should be examples “out there” but it’s beyond the scope of my abilities or this article series.
Paul
Regarding “…we can normally use the list name (very cool) instead of the Globally Unique Identifier (GUID) as a parameter in the SPAPI_Lists function but I was not able to make that work in this situation…”
After some looking around I found this in Microsoft’s online documentation for the SharePoint Web Services under the Lists web service’s GetList Method:
Parameters: listName: A string that contains either the title or the GUID for the list. When querying the UserInfo table, the string contains “UserInfo”.
So use “UserInfo” as the name. This worked for me. The GUID for this list seems to be unique to each SharePoint install since mine (which I used before I found this tidbit) is different than the one used here.
http://msdn.microsoft.com/en-us/library/lists.lists.getlist.aspx
Not having access to the administator directories of the SharePoint Server, I was only able to find the core.js. What is the location or URL I can use to download the list.js file also?
You can use the list title “User Information List” instead of the GUID, but only from the root URL.