Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 992
Mark Atkinson Avatar asked Aug 17 '26 15:08

Mark Atkinson


2 Answers

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.

like image 121
Mark Atkinson Avatar answered Aug 20 '26 04:08

Mark Atkinson


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 );
}
like image 31
Luke Avatar answered Aug 20 '26 04:08

Luke



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!