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));
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);
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>";
When I executed it I failed with the following exception:
Microsoft.SharePoint.Client.InvalidQueryExpressionException was unhandled
Message=The query expression is not supported.
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));