locked
Document set items Version history RRS feed

  • Question

  • Hi,

    I'm trying to to retrieve document set version history. here i need to retrieve all modified versions of a document in document set ? Can any body help?

    I have tried but didnt find anyway .. how to get version history of a document in document set.

    Thanks in advance.

     SPSite site = SPContext.Current.Site;
                      SPWeb web = site.OpenWeb();
    
                      string ver = web.Url + "/" + sharedDocs;
    
    
                      //Get the selected document
                      SPListItem listItem = SPContext.Current.Web.Lists["Contract Documents"].GetItemById(99);
           if (listItem.FileSystemObjectType == SPFileSystemObjectType.Folder)
            {
              //Get the folder 
              SPFolder myFolder = listItem.Folder;
    
              //Make sure the folder has items
              if (myFolder.ItemCount > 0)
              {
                //For each item get the document name
                foreach (SPFile item in myFolder.Files)
                { 
                  string name = item.Name;
                  foreach (SPListItem vh in myFolder.Files)
                  {
                     SPListItemVersionCollection coll = vh.Versions;
                     foreach (SPListItemVersion version in coll)
                     {
                         var VersionLabel =version.VersionLabel;
    
    
                     }
                  }
                }

    • Edited by Flying Hope Wednesday, April 15, 2015 8:57 AM
    Tuesday, April 14, 2015 10:17 AM

Answers

  • Hi,

    If this is still not resolved, you can have a look at below link 

    http://paulryan.com.au/2012/spfileversion-gochas/

    This link says -

    1. SPFileVersionCollection does NOT contain an entry for the latest version. This means that if the document has only a single version then this collection will be empty. If you want to access the latest version you must verify that the version label refers to the latest version by comparing it to the first entry in the SPListItemVersionCollection and if it is then access the SPFile directly. If you are accessing properties that are also present on an SPListItemVersion (ie not opening a stream) you can use that collection instead but you must also take note of the next point.

    Or you can check these links too-

    https://msarangam.wordpress.com/2011/09/14/explaination-on-spfileversioncollection-and-splistitemversioncollection/

    http://stackoverflow.com/questions/2340827/spfileversioncollection-why-versions-are-sorted-in-mixed-order

    Thanks,
    Danny

    Please remember to Mark as Answer if it helps.

    Friday, April 24, 2015 1:35 PM

All replies

  • A Document Set and its Documents each have their own version history. So to get version history for one of the documents within the Document Set you should open the Document Set and either:

    • Click on the context drop-down menu (ECB) next to the document you wish to view the version history for and click on Version History. OR
    • Select the document and click on Version History on the Ribbon

    If you don't see Version History in the context drop-down, or if it's disabled on the Ribbon then you probably don't have version history turned on for the library.

    To turn on Version History you would need to click on the Library Tab on the Ribbon and click on Library Settings, then click on Versioning settings, and setup what version history you would like (Major, Minor, etc.)

    Wednesday, April 15, 2015 2:59 AM
  • I actually read this post on my phone so didn't see the code section and replied with the OOTB method of retrieving the versions. So my answer above doesn't actually answer the question, but might help someone else I guess.
    Thursday, April 16, 2015 2:45 AM
  • No Jenkis,, No thats my fault.. I forgot to add code below.

    And I have tried the below code which is fine getting versions but it is not retrieving current version.

    My case, i have a document with versions 1.0, 1.1 but here 

    filecollection.Count is showing 1. that is 1.0 version.

    Is any other way to get current version?

     SPSite site = SPContext.Current.Site;
          SPWeb web = site.OpenWeb();
    
        //Get the selected document
        SPListItem listItem = SPContext.Current.Web.Lists["Contract Documents"].GetItemById(99);
    
        if (listItem.FileSystemObjectType == SPFileSystemObjectType.Folder)
        {
          //Get the folder 
          SPFolder myFolder = listItem.Folder;
    
          //Make sure the folder has items
          if (myFolder.ItemCount > 0)
          {
            //For each item get the document name
            foreach (SPFile item in myFolder.Files)
            { 
              string name = item.Name;
              SPFileVersionCollection filecollection = item.Versions;
              if (filecollection.Count > 0)
                 {
                   foreach (SPFileVersion v in filecollection)
                   {
                      var VersionLabel =v.VersionLabel;
                   }
    
              }
            }

    } }

    Thanks in advance.

    Thursday, April 16, 2015 8:45 AM
  • Have you tried to use versions of the SPListItem itself instead of the SPFile.

    So instead of getting each SPFile within the folder, get each SPListItem within the folder and get the versions for the SPListItem. Reason is that SPFile does not include the current version in the Versions collection, but SPListItem does.

    Extract from http://sharepointer2010.blogspot.com.au/2010/06/file-version-history-c.html

    "SPListItem.Versions.Count is always 1 more than SPListItem.SPFile.Versions.Count. The reason for this is because SPFile deems the latest version as the current version and therefore does not include this in its version collection. Therefore SPListItem.Versions.Coun[1] represents the same entity as SPListItem.SPFile.Versions.Count[0]."



    Thursday, April 16, 2015 9:51 AM
  • Hi,

    If this is still not resolved, you can have a look at below link 

    http://paulryan.com.au/2012/spfileversion-gochas/

    This link says -

    1. SPFileVersionCollection does NOT contain an entry for the latest version. This means that if the document has only a single version then this collection will be empty. If you want to access the latest version you must verify that the version label refers to the latest version by comparing it to the first entry in the SPListItemVersionCollection and if it is then access the SPFile directly. If you are accessing properties that are also present on an SPListItemVersion (ie not opening a stream) you can use that collection instead but you must also take note of the next point.

    Or you can check these links too-

    https://msarangam.wordpress.com/2011/09/14/explaination-on-spfileversioncollection-and-splistitemversioncollection/

    http://stackoverflow.com/questions/2340827/spfileversioncollection-why-versions-are-sorted-in-mixed-order

    Thanks,
    Danny

    Please remember to Mark as Answer if it helps.

    Friday, April 24, 2015 1:35 PM