Skip to main content
3 of 3
Code updated

Sharepoint 2010 CAML QUERY Can't find file

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"])
        );

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

                // GET SOME DATA RELATED TO THE FILE FROM A DB
                // PUT DATA IN THE FILE

                item.File.CheckIn("", CheckinType.MajorCheckIn);
                clientContext.ExecuteQuery();
            }
        }
    }
    catch (Exception ex)
    {
        if (System.Diagnostics.Debugger.IsAttached == true)
        {
            Console.WriteLine(ex.Message);
        }
    }        
}

}