At least two options are available then you need to specify explicitly which field values to retrieve.
Using CAML query using ViewFields
attribute
For example, CamlQuery.CreateAllItemsQuery method
public static CamlQuery CreateAllItemsQuery(
int rowLimit,
params string[] viewFields
)
accepts viewFields
parameter to specify which field values to retrieve
Below is provided custom version for CreateAllItemsQuery
method:
public static CamlQuery CreateAllItemsQuery(int rowLimit, params string[] viewFields)
{
if (rowLimit <= 0)
throw new ArgumentOutOfRangeException("rowLimit");
if (viewFields == null)
throw new ArgumentNullException("viewFields");
var camlQuery = new CamlQuery();
var output = new StringBuilder();
var xmlWriter = XmlWriter.Create(output, new XmlWriterSettings()
{
OmitXmlDeclaration = true
});
xmlWriter.WriteStartElement("View");
if (viewFields.Length > 0)
{
xmlWriter.WriteStartElement("ViewFields");
foreach (string str in viewFields)
{
if (!string.IsNullOrEmpty(str))
{
xmlWriter.WriteStartElement("FieldRef");
xmlWriter.WriteAttributeString("Name", str);
xmlWriter.WriteEndElement();
}
}
xmlWriter.WriteEndElement();
}
xmlWriter.WriteElementString("RowLimit", rowLimit.ToString(CultureInfo.InvariantCulture));
xmlWriter.WriteEndElement();
xmlWriter.Close();
camlQuery.ViewXml = output.ToString();
return camlQuery;
}
Usage
//Load and print Managed metadata column named "Document Type"
var list = context.Web.Lists.GetByTitle(listTitle);
var items = list.GetItems(CreateAllItemsQuery(8, new[] { "DocumentType" }));
context.Load(items);
context.ExecuteQuery();
foreach (var item in items)
{
var docTypeVal = item["DocumentType"] as TaxonomyFieldValue;
Console.WriteLine(docTypeVal.Label);
}
Using ClientContext.Load method
ClientRuntimeContext.Load method
public void Load<T>(
T clientObject,
params Expression<Func>[] retrievals
)
where T : ClientObject
accepts retrievals
parameter to specify which field values to retrieve
Usage
var list = context.Web.Lists.GetByTitle(listTitle);
var items = list.GetItems(CamlQuery.CreateAllItemsQuery());
context.Load(items, ic => ic.Include(i => i["DocumentType"]));
context.ExecuteQuery();
foreach (var item in items)
{
var docTypeVal = item["DocumentType"] as TaxonomyFieldValue;
Console.WriteLine(docTypeVal.Label);
}
oList.Fields.
a typo?context.Load(oListItemColl,oList.Fields=>oList.Fields.);