title: How can I query a SharePoint List for different ContentTypes?
body: <p>I am developing a console application to extract data from a SharePoint 2013 server using the <a href="https://msdn.microsoft.com/en-us/library/office/Dn423226.aspx#bk_QueryNETcsom" rel="nofollow noreferrer">.NET client object model</a> of the <a href="http://www.microsoft.com/en-us/download/details.aspx?id=35585" rel="nofollow noreferrer">SharePoint Server 2013 Client Components SDK</a>:</p>
  
  <pre><code>using (var clientContext = new ClientContext(SharePointUrl))
  {
      var list = clientContext.Web.Lists.GetByTitle(listName);
      var camlQuery = CamlQuery.CreateAllItemsQuery();
      var listItems = list.GetItems(camlQuery);
      clientContext.Load(listItems, GetFields(fields));
      clientContext.ExecuteQuery();
      return listItems.ToList();
  }
  </code></pre>
  
  <p>This is the GetFields() method I call to construct the list of fields I request:</p>
  
  <pre><code>private Expression&lt;Func&lt;ListItemCollection, object&gt;&gt;[] GetFields(IEnumerable&lt;string&gt; fields)
  {
      return fields
          .Select(field =&gt; (Expression&lt;Func&lt;ListItemCollection, object&gt;&gt;)(items =&gt; items.Include(item =&gt; item[field])))
          .ToArray();
  }
  </code></pre>
  
  <hr>
  
  <p>There is one big list -- <code>"Demands"</code> -- that is giving me troubles for some User-type fields: <strong>whenever I include these as a field that needs to be retrieved</strong> via the <code>GetFields()</code> method (see above), <strong>I get <a href="https://sharepoint.stackexchange.com/q/105397/45188">the infamous "Value does not fall within the expected range." error</a></strong>. For instance, <code>"DTBusinessResponsible"</code> can be retrieved, yet <code>"DTSupplyLeadResponsible"</code> cannot.</p>
  
  <p>I have been informed that <code>"Demands"</code> contains two <code>ContentTypes</code>: <code>"Demand"</code> and <code>"I-Demand"</code>. The fields that are giving me issues are exclusive to <code>"I-Demand"</code>.</p>
  
  <hr>
  
  <p>I cannot figure out how I can retrieve these problematic fields. I've tried retrieving the <code>ContentType</code>s for <code>"Demand"</code> and <code>"I-Demand"</code> and adding them to <code>list</code>:</p>
  
  <pre><code>var demandContentType = clientContext.Web.ContentTypes.GetById(contentTypeByName["Demand"]);
  var iDemandContentType = clientContext.Web.ContentTypes.GetById(contentTypeByName["I-Demand"]);
  
  list.ContentTypesEnabled = true;
  list.ContentTypes.AddExistingContentType(demandContentType);
  list.ContentTypes.AddExistingContentType(iDemandContentType);
  </code></pre>
  
  <p>But this is no use, I get an error telling me both <code>ContentType</code>s are already present.</p>
  
  <hr>
  
  <p>(Note that <a href="https://sharepoint.stackexchange.com/q/156640/45188">I have already asked this over at the SharePoint SE</a>, but I didn't get any useful responses there.)</p>
  
  <hr>
  
  <p><strong>EDIT: Let's simplify the question.</strong> </p>
  
  <p>I need to retrieve a number of fields from a SharePoint List called <code>"Demands"</code>. To that end, I add the fields I require as <a href="https://msdn.microsoft.com/en-us/library/office/jj171831.aspx" rel="nofollow noreferrer"><code>Expression&lt;Func&lt;T, object&gt;&gt;[]</code></a>.</p>
  
  <p>However, some of the fields I need to retrieve are exclusive to <code>"I-Demand"</code>, which is one of the <code>ContentTypes</code> of the <code>"Demands"</code> list. Whenever I add those fields, the query crashes and produces <a href="https://sharepoint.stackexchange.com/q/105397/45188">the infamous "Value does not fall within the expected range." error</a>. I have no trouble retrieving dozens of other fields which are all part of the <code>"Demand"</code> content type.</p>
  
  <p>How can I retrieve those fields that are exclusive to the <code>"I-Demand"</code> content type?</p>
  
  <hr>
  
  <p><strong>EDIT: Here's what doesn't work...</strong> </p>
  
  <pre><code>var fields = new List&lt;string&gt;
  {
      "DTBusinessRequestor",
      "DTBusinessResponsible",
      "DTBusinessSponsor",
      "DTDemandAccountable",
      "DTIICTBusinessAnalyst",
      "DTSupplyAccountable",
      "DTSupplyLeadResponsible",
      "Title",
      "Author",
      "Created",
      "Editor",
      "Modified",
      "ID"
  };
  
  var viewXml = new StringBuilder();
  
  viewXml.AppendLine("&lt;ViewFields&gt;");
  foreach (var field in fields)
  {
      viewXml.AppendFormat("&lt;FieldRef Name=\"{0}\" /&gt;", field).AppendLine();
  }
  viewXml.AppendLine("&lt;/ViewFields&gt;");
  
  var camlQuery = new CamlQuery { ViewXml = string.Format("&lt;View&gt;{0}&lt;/View&gt;", viewXml) };
  
  List&lt;ListItem&gt; results;
  
  using (var clientContext = GetClientContext())
  {
      var list = clientContext.Web.Lists.GetByTitle("Demands");
      var listItems = list.GetItems(camlQuery);
      clientContext.Load(listItems);
      clientContext.ExecuteQuery();
      results = listItems.ToList();
  }
  
  var validNames = new HashSet&lt;string&gt;();
  
  foreach (var listItem in results)
  {
      foreach (var field in fields)
      {
          object value;
          if (!listItem.FieldValues.TryGetValue(field, out value))
              continue;
  
          if (value != null)
          {
              validNames.Add(field);
          }
      }
  }
  </code></pre>
  
  <p>When I execute the above code:</p>
  
  <ul>
  <li><code>results</code> contains 10 (ten) items while there are 160 entries in SharePoint;</li>
  <li><code>validNames</code> contains six fields: "Title", "Author", "Created", "Editor", "Modified", "ID". </li>
  </ul>
  
  <p>Which is a slightly better result than the one I had, since "Author" and "Editor" were problematic. The rest of the fields return null, but that could be because the ten results do not have values. However, I cannot compare to the values in SharePoint since the ten titles that are returned are <strong><em>not</em></strong> ones that are found in the Demands table...</p>
  
  <hr>
  
  <p>I try to filter by ContentType by adding this line after <code>viewXml.AppendLine("&lt;/ViewFields&gt;");</code>:</p>
  
  <pre><code>viewXml.AppendLine("&lt;Query&gt;&lt;Where&gt;&lt;Eq&gt;&lt;FieldRef Name='ContentType' /&gt;&lt;Value Type='Computed'&gt;I-Demand&lt;/Value&gt;&lt;/Eq&gt;&lt;/Where&gt;&lt;/Query&gt;");
  </code></pre>
  
  <p>Result: <code>results</code> contains no entries.</p>
  
  <hr>
  
  <p>I remove the filter by ContentType and change this:</p>
  
  <pre><code>var camlQuery = new CamlQuery { ViewXml = string.Format("&lt;View&gt;{0}&lt;/View&gt;", viewXml) };
  </code></pre>
  
  <p>to this:</p>
  
  <pre><code>var camlQuery = CamlQuery.CreateAllItemsQuery();
  camlQuery.ViewXml = string.Format("&lt;View&gt;{0}&lt;/View&gt;", viewXml);
  </code></pre>
  
  <p>Same result as originally: ten items, same fields.</p>
  
  <p>I then remove this line:</p>
  
  <pre><code>camlQuery.ViewXml = string.Format("&lt;View&gt;{0}&lt;/View&gt;", viewXml);
  </code></pre>
  
  <p>Result: 160 items, values for "DTBusinessRequestor", "DTBusinessResponsible", "DTBusinessSponsor" and "DTIICTBusinessAnalyst" <strong><em>BUT</em></strong> no results for "Author" and "Editor" -- so I'm back where I started.</p>
  
  <hr>
  
  <p>I have not found a single solution where it returns me values for all 160 entries in the Demands list combined with values for "Author" and "Editor" and also "DTDemandAccountable", "DTSupplyAccountable" and "DTSupplyLeadResponsible" (where applicable). </p>
  
  <p>Whenever I add <code>&lt;Query&gt;&lt;Where&gt;&lt;Eq&gt;&lt;FieldRef Name='ContentType' /&gt;&lt;Value Type='Computed'&gt;I-Demand&lt;/Value&gt;&lt;/Eq&gt;&lt;/Where&gt;&lt;/Query&gt;</code> I get zero results.</p>