I'm working on my first C# program, and it's a pretty simple program that uses a view of a SharePoint page to go through and delete things that are older than 5 days on that page. Before deleting the items, though, I want to export the files to be deleted to an Excel file. There are a number of fields we want to record from each list item, including Assigned To/Completed By. In the Excel file the value of these are showing up as Microsoft.SharePoint.Client.FieldUserValue instead of the actual name of the person as shown in SharePoint. Is there a simple way of fixing this in the code, or is this a problem with using the view's XML to query SharePoint?

Here is the code I have so far:

    class Program
    static void Main(string[] args)
        ClientContext clientContext = new ClientContext("url");
        Web site = clientContext.Web;
        List workQueue = site.Lists.GetByTitle("Work Queue");

        View view = workQueue.Views.GetByTitle("Cleanup - Prod");       

        CamlQuery query = new CamlQuery { ViewXml = string.Format("<View><Query>{0}</Query></View>", view.ViewQuery) };
        ListItemCollection itemsToDelete = workQueue.GetItems(query);
        Console.Out.WriteLine("Retrieving Items");

        using (StreamWriter file = new StreamWriter(@"C:\Users\...\Desktop\oneQueue.csv"))
            file.WriteLine("Id,Name,Status,Team,Created,Assigned,Completed,Assigned To,Completed By,E-Mail CC,E-Mail From,E-Mail Sender,E-Mail Subject");
            foreach (ListItem listItem in itemsToDelete)
                Dictionary<string, object> values = listItem.FieldValues;
                List<object> fieldValues = new List<object> { values["ID"], values["FileLeafRef"], values["Status"], values["Team"], values["Created"], values["Assigned"], values["Completed"], values["Assigned_x0020_To0"], values["Completed_x0020_By"], values["EmailCc"], values["EmailFrom"], values["EmailSender"], values["EmailSubject"] };
                string fileEntry = fieldValues.Aggregate("", (entry, next) => entry + "\"" + next + "\",", entry => entry.Substring(0, entry.Length-1));

The "values["Assigned_x0020_To0"], values["Completed_x0020_By"]," portion is where the Excel file is showing Microsoft.SharePoint.Client.FieldUserValue.


Microsoft.SharePoint.Client.FieldUserValue.LookupValue should give the username.

Have a look at FieldUserValue members

You can also query the user from User Information List to get full details of the user, as shown below:

 var spUser = user as FieldUserValue;
 var userInfoList = context.Web.SiteUserInfoList;
 var query = new CamlQuery { ViewXml = "<View Scope='RecursiveAll'><Query><Where><Eq><FieldRef Name='ID' /><Value Type='int'>" + spUser.LookupId + "</Value></Eq></Where></Query></View>" };
 var users = userInfoList.GetItems(query);
 context.Load(users, items => items.Include(
      item => item.Id,
      item => item["Name"]));
 if (context.TryExecuteQuery())
      var principal = users.GetById(spUser.LookupId);
      username = principal["Name"] as string;

You can also query for Email and other stuff.

  • This set up worked for what we needed to do. Thanks. – Spen Nov 12 '14 at 20:20
  • what is user in this example? (on the first line) I don't understand this answer.. – Bassie Mar 7 '18 at 3:51
  • user is the value of Person or Group column. values["Created"] this in the above question is user. – Arsalan Adam Khatri Mar 7 '18 at 5:35

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.