0

I need to get user properties such as email, id, account name, department, and job title using JavaScript. I can successfully pull someone's display name from a people picker field, however, trying to get the properties back has been a chore. Below is the code I'm using to try to bring back the properties. I'm receiving two errors from this code.

function getUserInfo(displayName, successCallback, errorCallback) {
var context = new SP.ClientContext.get_current();
var userInfoList = context.get_web().get_siteUserInfoList();
var query = new SP.CamlQuery();
query.set_viewXml("<View><Query><Where><Eq><FieldRef Name='Name' /><Value Type='Text'>" + displayName + "</Value></Eq></Where></Query></View>");
var userInfoItems = userInfoList.getItems(query);
context.load(userInfoItems);
context.executeQueryAsync(
    function() {
        if (userInfoItems.get_count() > 0) {
            var userInfoItem = userInfoItems.itemAt(0);
            var userProperties = {
                "ID": userInfoItem.get_item("ID"),
                "Name": userInfoItem.get_item("Name"),
                "Department": userInfoItem.get_item("Department"),
                "WorkPhone": userInfoItem.get_item("WorkPhone"),
                "Email": userInfoItem.get_item("EMail"),
                "JobTitle": userInfoItem.get_item("Title")
            };
            successCallback(userProperties);
        } else {
            errorCallback("No user found with the display name '" + displayName + "'"); [Error #2]
        }
    },
    errorCallback
);
}

Here is my calling function:

getUserInfo(
    "Smoe, Joe", 
     function(userProperties) {
          console.log(userProperties);
     },failure);

Error Function:

// ON FAILURE
function failure(sender, args) {
    alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace()); [Error #1]
}

Error #1: Uncaught TypeError: Cannot read properties of undefined (reading 'get_message')

Error #2 at Array. (Functions2.js:165:17) <-- This line points back to the errorcallback function called above.

The hard-coded examples I've been using for display names are 100% users in SharePoint, and I can view their names in the user information list. Is there an easier or better way of doing this using code?

3
  • How you are fetching display name from a people picker field? You might be able to get email or login name or user ID (if data already available in SharePoint) as well from people picker fields. Then you can easily get other details using REST API: Commented Feb 24, 2023 at 17:11
  • I'm using InfoPath. I created a text field that grabs the display name when it gets populated and I'm using that, not directly from the people picker field. Are there any good resources for using REST and JavaScript in 2010? I have little experience with REST, but I get the concepts some from dabbling with it. Commented Feb 24, 2023 at 17:31
  • You only get display name from it? Are you able to fetch email from it? Commented Feb 25, 2023 at 3:18

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.