Mark Atkinson
Mark Atkinson

Reputation: 161

Querying files in SharePoint ClientObjectModel API

I'm using the SharePoint Managed Client Object Model API to retrieve files from SharePoint to my console app which is written in C#. My code (abbreviated here) looks something like this:

ClientContext clientContext = GetClientContext(sitePath);
Web rootWeb = clientContext.Web;

var files= rootWeb.GetFolderByServerRelativeUrl(relativeURL).Files;
clientContext.Load(files);
clientContext.ExecuteQuery();
// FileCollection files has no results

The returned FileCollection (files) has no results even though the folder specified (in the relativeURL) actually has a couple of files in it. When I run the bit of code below, the Folder object's ItemCount property is 2.

ClientContext clientContext = GetClientContext(sitePath);
Web rootWeb = clientContext.Web;

var folder = rootWeb.GetFolderByServerRelativeUrl(relativeURL);
clientContext.Load(folder);
clientContext.ExecuteQuery();

// folder.ItemCount == 2

I can't figure out why I can't seem to get the files. All the permissions seem OK. Are there any other gotcha's that exist in the API?

Upvotes: 3

Views: 3985

Answers (2)

Mark Atkinson
Mark Atkinson

Reputation: 161

I figured out my problem. My sitePath wasn't quite complete. I had part of the site prefixed to the relativeURL. So, I could create the site context and also get the folder but since the clientContext wasn't built completely with the site I wanted, I could never get the files.

Upvotes: 3

Luke
Luke

Reputation: 11

I'm just starting myself, but my code looks just like yours, but I have this loop following it

...         
clientContext.ExecuteQuery();
foreach( File file in files )
{
Debug.WriteLine( file.ServerRelativeUrl + "\t Level: " + file.Level + "\t modified: " + file.TimeLastModified + "\t by: " + file.ModifiedBy.ToString() + "\t CheckInComment: " + file.CheckInComment + "\t UIVersionLabel: " + file.UIVersionLabel );
}

Upvotes: 1

Related Questions