1

I have a situation where I need to load items from the list and which columns to be loaded cannot be hardcoded, as it will be decided on user input.

I have the following code:

ocamlQuery.ViewXml = "<View><RowLimit>1</RowLimit><Query>" + camlQuery + "</Query>" +
    "<ViewFields>" +
    propertiesNames +
    "</ViewFields></View>";
ListItemCollection oListItemColl = oList.GetItems(ocamlQuery);
context.Load(oListItemColl,oList.Fields=>oList.Fields);
context.ExecuteQuery();

When I am looping through the list item collection and try to access the value, I receive the error message:

The property or field has not been initialized.

How can I load fields dynamically depending on prior user input?

  • Is the extra dot at the end oList.Fields. a typo? context.Load(oListItemColl,oList.Fields=>oList.Fields.); – Nadeem Yousuf Mar 20 '14 at 12:20
4

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);
 }

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.