Accessing user profile information in SharePoint with javascript – updated version

I have previously posted a solution for pulling user information from the built-in user information list in SharePoint (found in WSS 3.0, MOSS 2007 and SharePoint 2010 both foundation and server). This is NOT the user profile found in MOSS 2007 and in SharePoint 2010 server. You find the solution here

This solution is an updated version of the script – and should hopefully work better with sites on a managed path.

Insert this code in a CEWP
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>
<script type="text/javascript" src="/test/EMSKJS/spjs-utility.js"></script>
<script type="text/javascript">

var userInfoObj = getUserInfo_v2(_spUserId);
var name = userInfoObj.Title;
var email = userInfoObj.EMail;
</script>

I have incorporated the new “getUserInfo_v2” in the file “spjs-utility.js”.

The parameter “_spUserId” is provided by SharePoint and represents the current user’s userID.

The function takes one argument which can be a userID or a login name (domainlogin or appname:user). If you use domainuser as a string like this:

var userInfoObj = getUserInfo_v2("contoso\alexander");

Please note the extra “” as it must be escaped in javascript.

The variables “name” and “email” are examples, the full range of “out of the box fields” are:
ID, Name, Title, EMail, Department, JobTitle, Notes, Picture, IsSiteAdmin, Created, Author, Modified, Editor, SipAddress

Download

You can download the file “spjs-utility.js” here. Ensure you pick the version dated 18.09.2011, or newer.

37 Comments on “Accessing user profile information in SharePoint with javascript – updated version

  1. I tried using this and nothing shows up on the screen. I downloaded the above file and placed in a document library and then copied the code above and linked it to a CEWP. I made sure and changed the line 2 to reflect where my file is saved. I save everything but nothing shows in the CEWP box.

  2. Hi Rebecca and Steve,
    This code does not display anything on the screen. Look at the code and see the variables “name” and “email”.

    If you want a “feedback” – alert the variables.

    Alexander

    1. Hi,
      This script does not use the “user profile” from MOSS, it deals with the built in user list. The fields you can access are listed in the function “getUserInfo_v2” and involves: ‘ID’, ‘Name’, ‘Title’, ‘EMail’, ‘Department’, ‘JobTitle’, ‘Notes’, ‘Picture’, ‘IsSiteAdmin’, ‘Created’, ‘Author’, ‘Modified’, ‘Editor’, ‘SipAddress’.

      You can however add extra fields add add those to the array.

      Alexander

    1. Hi,
      It looks OK from here – if you have issues with https, try using http (edit the URL). Let me know if it still does not work.

      Alexander

    1. Hi,
      I do not have access to a MOSS / SharePoint 2010 Server so i have not tested this function:

      var myProfile = getSSPUserProfile();
      alert(myProfile['the name of the profile property']);
      
      function getSSPUserProfile(){
      	var result, name, value;
      	result = {};
      	spjs_wrapSoapRequest('/_vti_bin/userprofileservice.asmx',
      	'http://microsoft.com/webservices/SharePointPortalServer/UserProfileService/GetUserProfileByName',
      	'<GetUserProfileByName xmlns="http://microsoft.com/webservices/SharePointPortalServer/UserProfileService"><AccountName></AccountName></GetUserProfileByName>',
      	function(data){
      		$(data).find('PropertyData').each(function(){
      			name = $(this).find('Name').text();
      			value = $(this).find('Value').text();
      			result[name] = value;
      		});
      	});
      	return result;
      }
      

      You must use the “spjs-utility.js” as it uses the function “spjs_wrapSoapRequest” from that library.

      Please let me know if it works.
      Alexander

    2. Hi,
      Try adding the accountname (domain\user) between these tags

      <AccountName>mydomain\\alexander</AccountName>

      Note the double backslash if the account name is a string and not a variable.

      This is not tested as I do not have access to SharePoint server.

      Alexander

      1. Hi Alexander,
        I have the same needs of Sneha (retrieve domain user’s email), but the function gives me an “undefined” message.
        Since the original function works very well, but retrieves only email of persons inside “All People” internal group, could you give me a proper solution to implement the original script?

  3. Hi,
    Is there any chance that I could still download the js file? That website is unavailable . Or is there any other place where I could get the file from?

    1. Hi,
      You must edit the function “getUserInfo_v2” found in the file “spjs-utility.js” and add the FieldInternalName of your “Work/Cell number field” to the array “arrOfFields”.

      Alexander

  4. Hi, Alex very nice Post.I have tried and it works f9 for me.But I want to get the List of all the Colleague of current users. i have read your getUserInfo_v2 function but unable to edit as per my need.
    Thanks an advance.

  5. Hi,
    Update spjs-utility.js to v1.177 and then use this code:

    var userInfoObj = getUserInfo_v2(_spUserId,"YourCustomPropterty");
    
    var x = userInfoObj["YourCustomPropterty"];
    

    “YourCustomPropterty” must be the FieldInternalName of the property from the use list.

    Alexander

  6. Hi Alexander,
    I added field “l” to get City of the user. But I got null as return value. I have admin rights to the site. Not sure what’s the issue. Rest of the field values are retrieved very well.

    -Venkatesh R

  7. The parameter “_spUserId” does not seem to be provided on the page. I think it has something to do with my current custom master page. Do you have any suggestions on how I can possibly “import” this field to the page?

    Thank you very much.

    -Shar Y

  8. Hello Alexander,

    We try to capture the current users SipAddress on a sharepoint page. We have several solutions that work in IE but no solution that also works in Safari.

    Do you have a solution for us?

    (We also tried the solution on this page)

    Kind regards,
    Mario

      1. Hello Alexander,

        It is Safari on Windows (haven’t tried Mac)
        We have turned on the console and got the following errors:
        ReferenceError: Can’t find variable: jQuery
        ReferenceError: Can’t find variable: getUserInfo_v2

        It works fine in IE.

        Kind regards,
        Mario

  9. Hello Alexander,

    It seems the script on the server cannot be read.
    So I included the script in the site collection and it works.

    The question for me now is why does Safari not read from the server.

    Kind regards,
    Mario

    1. Hi,
      Add one line to the code example above:

      <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>
      <script type="text/javascript" src="/test/EMSKJS/spjs-utility.js"></script>
      <script type="text/javascript">
      
      var userInfoObj = getUserInfo_v2(_spUserId);
      var name = userInfoObj.Title;
      var email = userInfoObj.EMail;
      // This line is for writing the email to a field called "TheEmailField"
      setFieldValue("TheEmailField",email);
      </script>

      PS: If you are using SP2013 you must change _spUserId for _spPageContextInfo.userId

      Alexander

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.