10

I am trying to pull back just specific columns in a list using the .NET client object model. (i.e. using Microsoft.SharePoint.Client;)

This is the CamlQuery specifying just fields in the DefaultView ViewFields for the list

<View>
    <ViewFields>
        <FieldRef Name="LinkTitle" />
        <FieldRef Name="FoodName" />
    </ViewFields>
</View>

However, when I run the query using:

CamlQuery camlQuery = new CamlQuery();
camlQuery.ViewXml = "<View><ViewFields><FieldRef Name="LinkTitle" /><FieldRef Name="FoodName" /></ViewFields></View>";
ListItemCollection items = list.GetItems(camlQuery);
clientContext.Load(items);
clientContext.ExecuteQuery();

then each item's FieldValues has the following keys (columns) pulled back:

  • FoodName
  • MetaInfo
  • _ModerationStatus
  • _Level
  • ID
  • UniqueId
  • owshiddenversion
  • FSObjType
  • Created_x0020_Date
  • Created
  • Title
  • FileLeafRef
  • Modified
  • FileRef

Why is SharePoint pulling back these additional field values?

3 Answers 3

8

Update for client object model:

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

Source: http://karinebosch.wordpress.com/2012/02/03/caml-and-the-client-object-model/

4
  • That example is not using the SharePoint client object model. And actually, I'm not convinced my syntax is wrong for the CamlQuery.ViewXml syntax, if I specify just the fieldRef elements (<FieldRef Name="LinkTitle" /><FieldRef Name="FoodName" />), I get even more fields returned.
    – Nathan
    Commented May 31, 2013 at 19:37
  • Sorry about that, altered my answer Commented May 31, 2013 at 19:47
  • 3
    Just for completeness sake, to get non-built in columns, you use the syntax clientContext.Load(listItems, items => items.Include(item => item["FoodName"], item => item["LinkTitle"]));
    – Nathan
    Commented May 31, 2013 at 20:07
  • I think that you'll get the items anyway - the linq will just delete them after they are being fetched.
    – XristosK
    Commented Nov 20, 2014 at 22:14
3

If you look here: http://karinebosch.wordpress.com/2012/02/03/caml-and-the-client-object-model/ under the section "ViewFields", you'll see the text: "But this also returns a number of system columns. If you really want to limit the columns returned to the columns you specify, you have to use a LINQ query within the Load method. The code looks as follows:"

1

This is a side note to the question... but it turns out that if you want non-field data such as EffectiveBasePermissions and HasUniqueRoleAssignments AS WELL AS item data from fields (e.g. Title, Modified by) you effectively call load on the listItems twice, as below:

clientContext.Load(listItems,
"Include(EffectiveBasePermissions, HasUniqueRoleAssignments)");
// Follow the statement above with this if you need special "fields" like 
//  HasUniqueRoleAssignments, EffectiveBasePermissions as well as the normal fields
clientContext.Load(listItems);
1
  • 1
    Hello and welcome to the community! While your answer seems to address the question asked, you should provide more context (see what I did there? :D) so that people reading it understand not just that it fixes the problem but why. As it stands your answer is not that helpful to someone who doesn't understand why you need to use your approach in the first place.
    – John-M
    Commented Nov 20, 2014 at 17:11

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.