Question

I have created the SharePoint hosted app.

I'm using JSOM for App.

I'm retrieving items from the list.

I have default column in the list called Author (Created by).

I want to retrieve email address of that Author.

Était-ce utile?

La solution

You can use the below sample code. It gets the item by its ID, then get the created by field and then ensures the user exits in site collection using web.EnsureUser. After that, we can use the email property.

var clientContext = new SP.ClientContext();
var item = clientContext.get_web().get_lists().getByTitle("Custom List").getItemById(1);
clientContext.load(item);
clientContext.executeQueryAsync(
  function(){ 
       var createdBy = item.get_item("Author");
       var user = clientContext.get_web().ensureUser(createdBy.get_lookupValue());
       clientContext.load(user);
       clientContext.executeQueryAsync(
            function(){ 
                 var email = user.get_email();   
                 console.log("User Email: " + email);
            },function(sender,args){ 
                 console.log(args.get_message());
            }
       );
  },
  function(sender,args){ 
       console.log(args.get_message());
  }
);
Licencié sous: CC-BY-SA avec attribution
Non affilié à sharepoint.stackexchange
scroll top