0

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...)

  • Can you try setting camlQuery.ViewFieldsOnly = true;, just a random guess :) – Arsalan Adam Khatri Sep 14 '15 at 15:25
  • Everything seems to be fine with your code. My bet is on the part not visible in your code - fields collection. Make sure that you use valid internal names for all fields. – dstarkowski Sep 14 '15 at 20:13
  • @ArsalanAdamKhatri There is no ViewFieldsOnly property. – BCdotWEB Sep 15 '15 at 8:15
0

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.

| improve this answer | |
0

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

| improve this answer | |
-1

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.

| improve this answer | |
  • 2
    Oh my. I understand that it might have helped with your problem and maybe performance isn't an issue in your on premise environment. But this is simply terrible. Your code will issue a CSOM request for each item on the list. If there are 1000 items on the list, it will issue 1000 web service calls and the 1000 SQL queries (best case scenario). If you execute this code in context of remote hosted app, SharePoint (both on prem and online) will deny service with 429 Too many request result. Not to mention executing this against O365 may take few minutes. It surely isn't a valid solution. – dstarkowski Feb 24 '16 at 17:59
  • 2
    And before someone suggests that. In general case it is possible to batch multiple operations in one execute. So it seems like you could move clientContext.ExecuteQuery() outside of foreach. This will make one CSOM call to load all the items. Unfortunately scheduling 1000 operations for one request can also be denied by SharePoint (assuming that it won't exceed WCF's message size limits first). – dstarkowski Feb 24 '16 at 18:19
-1

After This Code

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

Add This line

camlQuery.ViewFieldsOnly = true;
| improve this answer | |

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.