Retrieve Person or Group field using SharePoint 2013 Client Object Model

Home | SharePoint Development | Retrieve Person or Group field using SharePoint 2013 Client Object Model

I’m involved in troubleshooting a crazy people picker field issue in SharePoint 2013, which necessitates me to programmatically query the Person or Group field of SharePoint list using Client Object model. I was trying to figure out the exact syntax that can be used to grab the Person or Group field using FieldUserValue objects. Here is the complete code below :-

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Client;
using System.Net;

namespace ReadApproverNameField
{
    public class Program
    {
        static void Main(string[] args)
        {
            GetApproverDetails();
            Console.ReadLine();

        }

        public static void GetApproverDetails()
        {
            ClientContext clientContext = new ClientContext(http://servername/sites/sitecollectionname);
            
            NetworkCredential oNetworkCredential = new NetworkCredential();
            oNetworkCredential.Domain = @"Domain Name";
            oNetworkCredential.UserName = @"User Id";
            oNetworkCredential.Password = @"Password";
            clientContext.Credentials = oNetworkCredential;

            List list = clientContext.Web.Lists.GetByTitle("Name of the list");

            CamlQuery camlQuery = new CamlQuery();
            camlQuery.ViewXml = "<View/>";
            ListItemCollection listItems = list.GetItems(camlQuery);
            clientContext.Load(list); 
            clientContext.Load(listItems);
            clientContext.ExecuteQuery();


            foreach (ListItem oListItem in listItems)
            {
                
                FieldUserValue name = oListItem.FieldValues["ApproverName"] as FieldUserValue;
                string person = name.LookupValue;                
                Console.WriteLine(oListItem.Id + "     " + oListItem["Title"].ToString() + "     " + person);


            }
        }
    }
}
Basically we cannot create a new instance of FieldUserValue object, it needs to be casted from the current item’s field using ‘as FieldValue’ syntax
and then the LookupValue need to be invoked to fetch the value. If we run the above code, we’ll get the output below.
pic1

 Subscribe to my blog