1

I have created the following method to fetch list items from a list. The method does return the right items, but no matter what I provide in the viewFields parameter, it always returns all fields.

public SPListItemCollection GetListItems(SPWeb web, string listTitle, Dictionary<string, string> queryParams, params string[] viewFields)
{
  var query = new SPQuery();

  //queryString is returning appropriate items
  var queryString = this.CreateQueryString(true, queryParams);
  query.Query = queryString;

  var queryViewFields = string.Empty;
  foreach (string viewField in viewFields)
  {
    var field = web.Fields[viewField];
    var queryViewField = string.Format("<FieldRef Name='{0}' />", field.InternalName);

    queryViewFields += queryViewField;
  }

  // To me, this queryViewFields also seems to be fine
  query.ViewFields = queryViewFields;

 // If no view fields are specified, return all fields
  query.ViewFieldsOnly = !string.IsNullOrEmpty(queryViewFields); 

  var list = web.Lists[listTitle];
  var items = list.GetItems(query);

  return items;
}

What am I doing wrong? Thanks.

4
  • What are you passing in params string[] viewFields ?
    – Hardik
    Commented May 13, 2016 at 14:17
  • I am passing display names of the fields.
    – Boris
    Commented May 13, 2016 at 14:18
  • Is that helps you?
    – Hardik
    Commented May 13, 2016 at 14:45
  • Sorry, that doesn't help. There is a line var field = web.Fields[viewField]; that gets the field from the display name parameter, and the next line var queryViewField = string.Format("<FieldRef Name='{0}' />", field.InternalName); that explicitly uses internal name.
    – Boris
    Commented May 13, 2016 at 14:53

1 Answer 1

1

For Server side code

You just need to change the code as mentioned below & try it again

public SPListItemCollection GetListItems(SPWeb web, string listTitle, Dictionary<string, string> queryParams, params string[] viewFields)
{
  SPQuery query = new SPQuery();

  //queryString is returning appropriate items
  var queryString = this.CreateQueryString(true, queryParams);
  query.Query = queryString;

  string queryViewFields = string.Empty;
  foreach (string viewField in viewFields)
  {
    string field = web.Fields[viewField];
    string queryViewField = string.Format("<FieldRef Name='{0}' />", field.InternalName);

    queryViewFields += queryViewField;
  }

  // To me, this queryViewFields also seems to be fine
  query.ViewFields = queryViewFields;

 // If no view fields are specified, return all fields
  query.ViewFieldsOnly = !string.IsNullOrEmpty(queryViewFields); 

  SPList list = web.Lists[listTitle];
  SPListItemCollection items = list.GetItems(query);

  return items;
}

If you are using client-object model than please check below solution

In client object model only specifying ViewFields is not enough. You need to combine it with a linq statement like this:

CamlQuery camlQuery = new CamlQuery();
camlQuery.ViewXml = "<View><Where><Eq>.<FieldRef Name='Country' /><Value Type='Text'>Belgium</Value></Eq></Where></View>";
ListItemCollection listItems = spList.GetItems(camlQuery);
clientContext.Load(listItems,
  items => items.Include(
  item => item.Id,
  item => item.DisplayName,
  item => item.HasUniqueRoleAssignments));
clientContext.ExecuteQuery();

It may work for you..

7
  • Hi Hardik, I am using server side code. I don't see that replacing a boolean logic with a boolean value makes any difference.
    – Boris
    Commented May 13, 2016 at 14:51
  • Can you remove that line & try it?
    – Hardik
    Commented May 13, 2016 at 14:57
  • Is the solutions works for you?
    – Hardik
    Commented May 16, 2016 at 7:32
  • No, it doesn't.
    – Boris
    Commented May 16, 2016 at 8:05
  • Please try updated code in above answer.
    – Hardik
    Commented May 16, 2016 at 8:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.