2

I am creating a Hosted app using NAPA on Sharepoint 2013 (Office365). I need to retrive a user from a list column and get the user's email.

SP.List.getItems with a query returns a listEnumerator correctly. However, the user field is of type SPFieldUserValue. This does not appear to have a property called User from which I can retrieve the email (the C# version of SPFieldUserValue has a User property). Any idea on how I can get the user's email in javascript?

1

This query will give you Author column (user) with email from a list 'Test':

/_api/Web/Lists/GetByTitle('Test')/Items?$select=Author/EMail&$expand=Author/EMail

You can replace 'Author' with any other Person/Group column.

0

I think that you need to use SP.Web.ensureUser method to retrieve user by it's logon name, after this you can use SP.User.email property.

SP.SOD.executeFunc('sp.js', 'SP.ClientContext', sharePointReady);

function sharePointReady() {
    //retrieve logonName

    var clientContext = SP.ClientContext.get_current();
    var web = context.get_web();
    var user = web.ensureUser(logonName);

    clientContext.load(user);
    clientContext.executeQueryAsync(onQuerySucceeded, onQueryFailed);
}
function onQuerySucceeded() {
    alert('The email address of the current user is ' + user.get_email());
    alert('The account name is ' + user.get_loginName());
}
function onQueryFailed(sender, args) {
    alert('Error: ' + args.get_message());
}
  • Thanks. I'm trying to get the email of a user from a list column, not the current user. Unfortunately, SPFieldUserValue only returns the Display Name and not the logon name. So this won't work. – Sanat Gersappa May 17 '13 at 5:18
  • you also can try to use User Information List. See this example. It should store user's email. – Alexander May 17 '13 at 6:46
0

It can be done for any person field not just current user. Get lookupvalue from person field(contact in this case) and the get the user field.

var userName = currentRd.get_item("Contact").get_lookupValue();

user = web.ensureUser(userName);

Everything else should work the same way as Alexander said.

0

From the list API you can use select statement as below

siteurl + "/_api/web/lists/getbytitle('listname')/items&$select=Author/EMail

and from the result iterate like below

var itemcount = apiResultValue.length;
var itemsresult = JSON.stringify(apiResultValue);
itemsresult = JSON.parse(itemsresult);
if (itemcount > 0) {
alert(itemsresult[0].Author.Email);
//above line will get the email of user from list createdby user column
}

Hope it will help someone :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.