Reputation: 1
When attempting to access the Modified By field in SharePoint in C# I'm receiving a weird error stating that the property or field has not been initialized. When I use the debugger to drill down, I can see that the Editor field is not in the collection. What's weird is that when I try to get the same item from the same list (based on ID) I am able to access the field.
Setting the stage:
string siteUrl = @"https://sharepointsite";
ClientContext context = new ClientContext(siteUrl);
List qsList = context.Web.Lists.GetByTitle("SharePointList");
context.ExecuteQuery();
User theUser = context.Web.EnsureUser("SharePointUser");
context.ExecuteQuery();
Get item by ID (this works). When I view the collection in my Locals Window I can see the Editor Key/Value pair.
ListItem item = qsList.GetItemById(1);
context.Load(item);
context.ExecuteQuery();
FieldUserValue editor = (FieldUserValue)item?["Editor"];
Console.WriteLine(editor.LookupValue);
Get ListItemCollection with a CamlQuery passed to GetItems method of the List object. This does not work and when I view in Locals Window I can see that there is no Key/Value pair in the ListItem for "Editor" for the ListItem with the same ID value.
CamlQuery query = CamlQuery.CreateAllItemsQuery(10);
ListItemCollection qsItems = qsList.GetItems(query);
context.Load(qsItems);
context.ExecuteQuery();
foreach (ListItem listItem in qsItems)
{
Console.WriteLine(listItem);
FieldUserValue editor = (FieldUserValue)listItem?["Editor"];
Console.WriteLine(editor.LookupValue);
}
Upvotes: 0
Views: 284
Reputation: 1862
I think you forgive one step: In your code that you want to request the User Field, you need another context.Load() for FieldUserValue object, like this:
FieldUserValue editor = (FieldUserValue)listItem?["Editor"];
context.Load(editor);
context.ExecuteQuery();
Please, try this an give a feedback.
Upvotes: 0