Menu Home

SharePoint ClientContext The query expression is not supported

Sometimes you come across an exception, that at first seems strange but when you figure out what caused it you feel like an idiot.
Last week I was creating a sync job as part of an Azure Web Job, I was just doing some SharePoint integration with the client side framework and then handed the code over to another developer for him to finish it.
When he returned the code to me, he requested that I optimized the code from doing one request per document library in the site, to a single request that would download information from all document libraries in one go.
No a big problem. I refactored the code to look like this

using (ClientContext context = new ClientContext(ConfigurationManager.AppSettings["SharePointWebSite"]))
{
   context.Credentials = GetCredentials();
   //Fetch SharePoint files

   var spResults = new List<Tuple<List, ListItemCollection>>();
   var lists = context.LoadQuery(context.Web.Lists.Where(l => l.BaseType == BaseType.DocumentLibrary));
   context.ExecuteQuery();
   foreach (var list in lists)
   {
      var items = list.GetItems(CreateAllFilesQuery());
      context.Load(items, icol => icol.Include(i => i.File));
      context.Load(list.RootFolder, r => r.Name);

      spResults.Add(Tuple.Create(list, items));
   }
   context.ExecuteQuery();
}

private static CamlQuery CreateAllFilesQuery()
{
   var qry = new CamlQuery();
   qry.ViewXml = "<View Scope=\"RecursiveAll\"><Query><Where><Eq><FieldRef Name=\"FSObjType\" /><Value Type=\"Integer\">0</Value></Eq></Where></Query></View>";
   return qry;
}

When I executed it I failed with the following exception:
Microsoft.SharePoint.Client.InvalidQueryExpressionException was unhandled
HResult=-2146233088
Message=The query expression is not supported.
Source=Microsoft.SharePoint.Client.Runtime
StackTrace:
at Microsoft.SharePoint.Client.DataRetrieval.ProcessMethodCallQueryExpression(ClientQueryInternal rootQuery, ClientObject rootClientObject, MethodCallExpression exp, Boolean leaf, QueryProcessInfo queryInfo, QueryMethodAggregator aggregator)
at Microsoft.SharePoint.Client.DataRetrieval.ProcessQueryExpression(ClientQueryInternal rootQuery, ClientObject rootClientObject, Expression exp, Boolean leaf, QueryProcessInfo queryInfo, QueryMethodAggregator aggregator)
at Microsoft.SharePoint.Client.DataRetrieval.Load[T](T clientObject, Expression`1[] retrievals)
at Microsoft.SharePoint.Client.ClientRuntimeContext.Load[T](T clientObject, Expression`1[] retrievals)

Strange, as I had tested the code in another project where it worked just fine. Also google told me that it was valid code. After much head scratching I figured out that the code was failing because the other developer had added using System.Data.Entity; and then the line context.Load(items, icol => icol.Include(i => i.File)); was now using a different extension method, that of course is wasn’t compatible with the ClientContext. Not the best exception message, so it was not immediate obvious what the problem was.
Once the problem was identified it was easy to change it to

context.Load(items, icol => ClientObjectQueryableExtension.Include(icol, i => i.File));

Categories: SharePoint

Tagged as:

Simon J.K. Pedersen

1 reply

  1. Hi Simon,
    I had the same error in my code and found your blog. I had to do something different as I needed to use p => p.RoleAssignments.Include() and so couldnt reference the ClientObjectQueryableExtension in the LINQ. The way I got round the issue was to move the using statement for Microsoft.SharePoint.Client to inside my namespace & this sorted out the conflict.
    Thanks for sharing!!

Leave a Reply

Your email address will not be published. Required fields are marked *