Question

I am trying to return several User-type fields from SharePoint 2013 like this:

using (var clientContext = new ClientContext(myUrl))
{
    var list = clientContext.Web.Lists.GetByTitle(listName);

    var listItems = list.GetItems(GetCamlQuery(fields));

    clientContext.Load(listItems);

    clientContext.ExecuteQuery();

    return listItems.ToList();
}

private CamlQuery GetCamlQuery(IEnumerable<string> fields)
{
    var camlQuery = new CamlQuery();

    var viewXml = new StringBuilder();

    viewXml.AppendLine("<ViewFields>");
    foreach (var field in fields)
    {
        viewXml.AppendFormat("<FieldRef Name='{0}' />", field).AppendLine();
    }
    viewXml.AppendLine("</ViewFields>");

    camlQuery.ViewXml = string.Format("<View>{0}</View>", viewXml);

    return camlQuery;
}

However, all of these User-fields return null, even though I'm sure some of them should contain values. What am I doing wrong?

EDIT:

  • I am using the proper names: the query doesn't fail, and the FieldValues collection contains results for these fields, except they're all null.
  • When I use the Display Name, that field isn't in the result set.
  • I've added other fields, and they also return null, except for "ID" and "Title".

(I also just noticed I am only receiving 10 items as a result, but there are many more entries in the List I am querying. So that's another thing that is wrong...)

Was it helpful?

Solution 3

I've found a workaround:

var loadedListItems = new List<ListItem>();

using (var clientContext = new ClientContext(myUrl))
{
    var list = clientContext.Web.Lists.GetByTitle(configuration.ListName);

    var camlQuery = CamlQuery.CreateAllItemsQuery();
    var listItems = list.GetItems(camlQuery);
    clientContext.Load(listItems);
    clientContext.ExecuteQuery();

    foreach (var loadedListItem in listItems.ToList().Select(x => list.GetItemById(x.Id)))
    {
        clientContext.Load(loadedListItem);
        clientContext.ExecuteQuery();

        loadedListItems.Add(loadedListItem);
    }
}

Note how the code iterates through the result of list.GetItems(camlQuery); and then executes list.GetItemById(x.Id) for each result. This does indeed work and returns items with "Author" and "Editor" filled in.

Important note: I do realize the performance impact (right now this takes 40-50 seconds), but this is the only solution that has solved my problem. I'd much rather have someone solve the issue that is causing the impossibility to retrieve those values via the regular query, but apparently that isn't happening.

OTHER TIPS

You probably know this, but to be safe, each SharePoint field has three names, which can all be different: Internal Name, Static Name, and Display Name.

I have not used this particular interface. In the server side interfaces, sometimes Display Name is needed, and sometimes Internal Name is needed, and sometimes it will try more than one.

Try to give an empty query and specify the column selection using the lambda expression in the load statement. Check out How to: Retrieve List Items

After This Code

camlQuery.ViewXml = string.Format("<View>{0}</View>", viewXml);

Add This line

camlQuery.ViewFieldsOnly = true;
Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top