Skip to main content
1 of 3

Sharepoint 2010 CAMLQUERY 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.Wordprocessing;
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)
{
string SPSite = "https://www.remotewebiste.com";
string dFile = "FileIWantToGet.xlsx"; // a hard coded variable at the momnent...

ClientContext _clientContext;
Web _spWebsite;
NetworkCredential cred = new NetworkCredential("user.name", "somePassword", "RemoteDomain");
_clientContext = new ClientContext(SPSite);
_clientContext.Credentials = cred;
_spWebsite = _clientContext.Web;

List list = _spWebsite.Lists.GetByTitle(dFile);
CamlQuery camlQuery = new CamlQuery();
camlQuery.ViewXml = @"<View Scope='RecursiveAll'><Query><Where>
                        <BeginsWith><FieldRef Name='FileDirRef'/>
                            <Value Type='Text'>/subdir0/subdir1/subdir2/subdir3/" + dFile + @"</Value>
                        </BeginsWith></Where></Query></View>";
//camlQuery.FolderServerRelativeUrl = "/";

ListItemCollection listItems = list.GetItems(camlQuery);

_clientContext.Load(listItems, items => items.Include(
    item => item.File.Name,
    item => item.File.ServerRelativeUrl,
    item => item["Proposal_x0020_Navigation_x0020_Grouping"],
    item => item["Modified"]));

_clientContext.ExecuteQuery();// Errors as con't find dfile file

if (listItems != null)
{
    foreach (Microsoft.SharePoint.Client.ListItem item in listItems)
    {
        item.CheckOut(); 
        // shove some data in it and 

        item.CheckIn();  

        Console.WriteLine(item.DisplayName);
    }
}
}
}