Srutu-tutu
Srutu-tutu

Reputation: 35

Get multiple list items with Person field "Email' property loaded

I would like to fetch all SharePoint list items, which have a Person field and see the person's email. When using a default query from the docs, the Email property of the Person field is not loaded:

var myList = await context.Web.Lists.GetByTitleAsync("My List", p => p.Title,  
                                                     p => p.Fields.QueryProperties(p => p.InternalName, 
                                                                                   p => p.FieldTypeKind, 
                                                                                   p => p.TypeAsString, 
                                                                                   p => p.Title));
await foreach (var listItem in spList.Items)
{
   try
   {
       var owner = (listItem.Values["MyOwner"] as FieldUserValue).Email;
   }
   catch (Exception ex) 
   {
       Console.WriteLine(ex.Message) //Property Email was not yet loaded
   }
}

How to expand complex fields like Person and Lookup fields in PnP Core?

This is fairly easy in PnP JS, for example:

$pnp.sp.web()
.lists.getByTitle("Sites")
.items.select('Person/Title')
.expand('Person')
.get();

Upvotes: 1

Views: 255

Answers (0)

Related Questions