18.09.2011 I have posted a new solution which makes this one obsolete. You find the new one here
09.09.2010 Updated the function “getUserInfo”. It no longer requires the list GUID for the user list to be specified in the script.
31.10.2009: Small update for default picture in the example CEWP.
This article describes how to access the “user profile” under WSS 3.0 via javascript. The method used is a query trough the webservice lists.asmx.
I use some code (“interaction.js” and stringBuffer.js”) created by Erucy and published on codeplex.
The jQuery-library is found here. The pictures and the sourcecode refers to jquery-1.3.2.min. If you download another version, be sure to update the script reference in the sourcecode.
As always we begin like this:
Create a document library to hold your scripts (or a folder on the root created in SharePoint Designer). In this example i have made a document library with a relative URL of “/test/English/Javascript” (a subsite named “test” with a subsite named “English” with a document library named “Javascript”):
You can call this script from any page. In this example i will place it on Default.aspx.
The sourcecode for the “AccessUserProfileInWSS.js” looks like this:
/* getUserInfo - Returns "user info" data from user list in WSS 3.0 (not MOSS user profile) * --------------------------------------------- * Created by Alexander Bautz * alexander.bautz@gmail.com * https://spjsblog.com * LastMod: 20.09.2009 * --------------------------------------------- Refer these scripts: interaction.js // Erucy - http://spjslib.codeplex.com stringBuffer.js // Erucy - http://spjslib.codeplex.com jQuery // http://jQuery.com Use: var ui = getUserInfo(); // If UserId is not specified it assumes it's logged in user (_spUserId) alert(ui.Title); */ function getUserInfo(UserId){ wsBaseUrl = '/_vti_bin/'; var uiObj = {}; if(typeof(UserId)=="undefined" || UserId=='')UserId = _spUserId; var arrOfFields = ['ID', 'Name', 'Title', 'EMail', 'Department', 'JobTitle', 'Notes', 'Picture', 'IsSiteAdmin', 'Created', 'Author', 'Modified', 'Editor', 'SipAddress', 'Deleted']; var item = getItemById('UserInfo',UserId,arrOfFields); if(item != null){ for(i=0;i<arrOfFields.length;i++){ if(item[arrOfFields[i]]!=null){ uiObj[arrOfFields[i]] = item[arrOfFields[i]]; }else{ uiObj[arrOfFields[i]] = ''; } } return uiObj; }else{ for(i=0;i<arrOfFields.length;i++){ uiObj[arrOfFields[i]] = "User with id " + UserId + " not found."; } return uiObj; } }
Save this as a text file and rename to “AccessUserProfileInWSS.js”, then upload to the library as shown above.
You call the script from a standard CEWP like this:
Sourcecode for CEWP:
<script type="text/javascript" src="/test/English/Javascript/jquery-1.3.2.min.js"></script> <script type="text/javascript" src="/test/English/Javascript/interaction.js"></script> <script type="text/javascript" src="/test/English/Javascript/stringBuffer.js"></script> <script type="text/javascript" src="/test/English/Javascript/AccessUserProfileInWSS.js"></script> <script type="text/javascript"> var ui = getUserInfo(); // If UserId is not specified it assumes it's logged in user (_spUserId) picSrc = '/_layouts/images/person.gif'; if(ui.Picture!=''){ picSrc = ui.Picture.split(', ')[0] } var str = ''; str += "Accountname: " + ui.Name + "<br>"; str += "Full name: " + ui.Title + "<br>"; str += "E-mail: " + ui.EMail + "<br>"; str += "Department: " + ui.Department + "<br>"; str += "<img alt='' src='" + picSrc + "' />"; document.write(str); </script>
And you get a result like this:
I will follow up this article with an article describing how to hide or show form fields based on membership – or not membership – in a SharePoint group.
Alexander