Cannot return an item's Author and Editor properties in CSOM
-
03-10-2020 - |
Question
I have a utility which makes updates without affecting the ModifiedBy/Editor field. Before any updates are made, I store the user value in one of two ways:
CamlQuery camlQuery = new CamlQuery();
camlQuery.ViewXml = _query;
ListItemCollection collListItem = list.GetItems(camlQuery);
context.Load(collListItem);
context.ExecuteQuery();
if (collListItem.Count > 0)
{
foreach (ListItem item in collListItem)
{ //both of these error out.. but only on this specific list
object modifiedBy = item["Editor"];
original_editor = item["Editor"] as FieldUserValue;
}
}
This has been working in SP 2010 and SP 2013 for as long as the application has existed (~2 years), but I now have a list for which I am not able to use this. Other lists in the same site collection are updated just fine. The only difference with this list is that it has hundreds of fields (250-300). Is there an actual reason for why item["Editor"]
is causing this error
"Microsoft.SharePoint.Client.PropertyOrFieldNotInitializedException" ?
Solution 2
I mentioned earlier that this list had hundreds of fields. That's actually the only thing that set this list apart from the rest. My CAML query simply added a scope of "RecursiveAll" along with my query. Because of the large number of fields, I needed to specify ViewFields. Nk SP was on the right track, but this is where I need to include the property instead.
Previous (not working on list with many fields) CAML:
<View Scope='RecursiveAll'>
<Query>
<Where><Eq><FieldRef Name='ID'/><Value Type='Number'>1</Value></Eq></Where>
</Query>
</View>
Working CAML:
<View Scope='RecursiveAll'>
<ViewFields>
<FieldRef Name='Editor' />
<FieldRef Name='Modified' />
</ViewFields>
<Query>
<Where><Eq><FieldRef Name='ID'/><Value Type='Number'>1</Value></Eq></Where>
</Query>
</View>
OTHER TIPS
You have to include the property:
using (var clientContext = new ClientContext("http://yourSite/"))
{
var oSite = clientContext.Site;
clientContext.Load(oSite);
var oWeb = oSite.OpenWeb("");
clientContext.Load(oWeb, web => web.Lists);
clientContext.ExecuteQuery();
//get your list by index
var list = oWeb.Lists[0];
ListItemCollection itemsColl= list.GetItems(new CamlQuery());
clientContext.Load(itemsColl,
items => items.Include(
item => item.Id,
item => item["Editor"]));
clientContext.ExecuteQuery();
foreach (ListItem item in itemsColl)
{
Console.WriteLine((item["Editor"] as Microsoft.SharePoint.Client.FieldUserValue).LookupValue);
}
}