I'm wanting to check a document out from an SP site, but my code/CAMLQuery can't find it (it's defo there). It stops at the ExecuteNonQuery line, saying it can't find the FileIWantToGet.xlsx I've not done any SP work before nor heard of CAMLQuery until this bit of work.

I have asked this on StackOverFlow but can't move it to here.

Any help appreciated.

using System;

using System.IO; using System.Linq; using DocumentFormat.OpenXml.Packaging; using DocumentFormat.OpenXml.Office2013.Excel; using Microsoft.SharePoint.Client; // The following using directive resolves the ambiguity // between the System.IO.File class and Microsoft.SharePoint.Client.File // class. using ClientOM = Microsoft.SharePoint.Client; using System.Net; using System.Data;

class Program { static private void CopyStream(Stream source, Stream destination) { byte[] buffer = new byte[32768]; int bytesRead; do { bytesRead = source.Read(buffer, 0, buffer.Length); destination.Write(buffer, 0, bytesRead); } while (bytesRead != 0); }

static void Main(string[] args)
    NetworkCredential cred = new NetworkCredential("USERNAME", "PASS", "WINAD");
    string SPSite = "https://www.COMPANYPORTAL.COM";
    string DirStruc = "/sp/CorporateITPro/BusinessDocuments/Migration/Templates/Master";       
    string dFile = "MigClassification.xslx";

    ClientContext clientContext;
    clientContext = new ClientContext(SPSite);
    clientContext.Credentials = cred;

    Web spWebsite;
    spWebsite = clientContext.Web;

    List list = clientContext.Web.Lists.GetByTitle(dFile);        
    CamlQuery camlQuery = new CamlQuery();
    camlQuery.ViewXml =("<View Scope='RecursiveAll'><Query><Where><Neq><FieldRef Name='ContentType' /><Value Type='Text'>Folder</Value></Neq></Where></Query></View>");
    camlQuery.FolderServerRelativeUrl = DirStruc;

    ListItemCollection listItems = list.GetItems(camlQuery);

    clientContext.Load(listItems, items => items.Include(
        item => item.File.Name,
        item => item.File.Author,
        item => item.File.ServerRelativeUrl,
        item => item["Proposalx0020Navigationx0020Grouping"],
        item => item["Modified"])

        clientContext.ExecuteQuery();// NOT FOUND ERROR HERE
        if (listItems != null)
            foreach (Microsoft.SharePoint.Client.ListItem item in listItems)

                // PUT DATA IN THE FILE

                item.File.CheckIn("", CheckinType.MajorCheckIn);
    catch (Exception ex)
        if (System.Diagnostics.Debugger.IsAttached == true)


1 Answer 1


When you want to search a file by its name, you should use the 'FileLeafRef' as the FieldRef in your CamlQuery, because 'FileDirRef' basically points to the directory.

So, your camlQuery should look like:

camlQuery.ViewXml = @"<View Scope='RecursiveAll'>
                                      <FieldRef Name='FileLeafRef'/>
                                      <Value Type='Text'>/subdir0/subdir1/subdir2/subdir3/" + dFile + @"</Value>
  • Hi, @Andy thanks for that but it still can't find the file. Is this code correct 'code' List list = _clientContext.Web.Lists.GetByTitle(dFile); As it erros that it "can't find the List"
    – ItsDanny
    Oct 12, 2016 at 10:18
  • (pressed Enter by mistake there...) when this line execs code _clientContext.ExecuteQuery();
    – ItsDanny
    Oct 12, 2016 at 10:19
  • When "can't find the List" error occurred, it means that you've used wrong list name. For example, in those line: List list = _spWebsite.Lists.GetByTitle(dFile); you passed fileName as list name, and of course it won't find the list (except the list and the file have the same name). Please make sure you have proper list name based on the list in your SharePoint site. Oct 13, 2016 at 2:34
  • I wonder then if I'm doing this right. It seems to Find the Calendar (or at least doesn't error). but the File (an xlsx) can be found. Do I need to look for a file instead?
    – ItsDanny
    Oct 13, 2016 at 8:34
  • .."Can't be found "that was meant to say.
    – ItsDanny
    Oct 13, 2016 at 9:21

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.