I'm making a .txt to store each version of a file after I perform a certain action.

The code I'm running is simple:

write("version number: 0.1");
write("modified : " + file.Item["Created"].ToString());
write("modified by: " + file.Author.Name);
write("Check in comment : " + file.CheckInComment);

foreach( SPFileVersion version in file.Versions){
  if (version.VersionLabel != "0.1"){
      write("version number: "+ version.VersionLabel);
      write("modified : " + version.Created.ToLocalTime());
      write("modified by: " + version.CreatedBy.Name);
      write("Check in comment : " + version.CheckInComment);

The code does not go inside the foreach. If the code I wrote is correct, that means my files does not have any versions stored? If so, how do I do this?

I added a new .docx on a library, did check-out and check-in. Edited the file with another user, and then again a check-out and check-in. But I can't seem to get in the foreach.


  • Are you sure the code does not go inside the foreach? It might be the if condition is not valid? Commented Oct 29, 2014 at 16:36
  • It does not go inside. I put a break point on both, the foreach and the if.
    – smith
    Commented Oct 29, 2014 at 16:41
  • Are you using CSOM? Is so did you load the file.Versions?
    – Mike Lutge
    Commented Oct 29, 2014 at 18:37
  • Is the versioning enabled for the document library? Commented Oct 30, 2014 at 7:54

1 Answer 1


Try below code

List LibraryName = clientContext.Web.Lists.GetByTitle(lib); 
CamlQuery camlQuery = new CamlQuery();
camlQuery.ViewXml = "";

Microsoft.SharePoint.Client.ListItemCollection collListItem = LibraryName.GetItems(camlQuery);
clientContext.Load(collListItem, items => items.Include(item => item.File.Versions));

foreach (Microsoft.SharePoint.Client.ListItem oListItem in collListItem)
    foreach (FileVersion version in oListItem.File.Versions)
        if ("0.1" != version.VersionLabel)


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.