{"id":429,"date":"2010-06-13T19:05:41","date_gmt":"2010-06-13T17:05:41","guid":{"rendered":"http:\/\/www.itidea.nl\/?p=429"},"modified":"2015-09-08T20:31:57","modified_gmt":"2015-09-08T18:31:57","slug":"documents-and-versioning","status":"publish","type":"post","link":"https:\/\/www.itidea.nl\/index.php\/documents-and-versioning\/","title":{"rendered":"Documents and versioning"},"content":{"rendered":"<p>A nice part of document libraries and lists is the version history. Nothing new, we had it in MOSS 2007, but I previously never dived really into the version history by code.<br \/>\nRecently I did and discovered something that might help you out some time.\u00a0<\/p>\n<p>Let\u2019s get the files of the Shared Documents folder and get all the versions by code:<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nSPDocumentLibrary sharedDocs = SPContext.Current.Web.Lists&#x5B;'Shared Documents'] as SPDocumentLibrary;\r\nresultBox.Text = string.Empty;\r\nforeach (SPListItem doc in sharedDocs.Items)\r\n{\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 SPFileVersionCollection coll = doc.File.Versions;\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 if (coll.Count != 0)\r\n\u00a0\u00a0\u00a0\u00a0 {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 resultBox.Text += 'Versions of ' + doc.File.Name + Environment.NewLine;\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 foreach (SPFileVersion version in coll)\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0{\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 resultBox.Text += 'VersionLabel: ' + version.VersionLabel + ' IsCurrentVersion: ' + version.IsCurrentVersion + Environment.NewLine;\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}\r\n\u00a0\u00a0\u00a0\u00a0 }\r\n}\r\n<\/pre>\n<p>\u00a0Nothing quite impressive, I used the file version collection and asked some of the properties from the SPFileVersion. Here\u2019s the result:<\/p>\n<p><a href=\"https:\/\/www.itidea.nl\/wp-content\/uploads\/2010\/06\/DocLibVersionHistoryResultByCode.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-433\" title=\"DocLibVersionHistoryResultByCode\" src=\"https:\/\/www.itidea.nl\/wp-content\/uploads\/2010\/06\/DocLibVersionHistoryResultByCode.png\" alt=\"\" width=\"343\" height=\"98\" srcset=\"https:\/\/www.itidea.nl\/wp-content\/uploads\/2010\/06\/DocLibVersionHistoryResultByCode.png 343w, https:\/\/www.itidea.nl\/wp-content\/uploads\/2010\/06\/DocLibVersionHistoryResultByCode-300x85.png 300w\" sizes=\"auto, (max-width: 343px) 100vw, 343px\" \/><\/a><\/p>\n<p>Notice that version 5.0 is the currently published\u00a0version and a draft version 5.1 is available too.<br \/>\nNow let\u2019s take a look at the version history of the file at the document library itself:<\/p>\n<p><a href=\"https:\/\/www.itidea.nl\/wp-content\/uploads\/2010\/06\/DocLibVersionHistory.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-434\" title=\"DocLibVersionHistory\" src=\"https:\/\/www.itidea.nl\/wp-content\/uploads\/2010\/06\/DocLibVersionHistory.png\" alt=\"\" width=\"501\" height=\"367\" srcset=\"https:\/\/www.itidea.nl\/wp-content\/uploads\/2010\/06\/DocLibVersionHistory.png 501w, https:\/\/www.itidea.nl\/wp-content\/uploads\/2010\/06\/DocLibVersionHistory-300x219.png 300w\" sizes=\"auto, (max-width: 501px) 100vw, 501px\" \/><\/a>\u00a0<\/p>\n<p>Hey, there is another version: 5.2! Why does this file not show up by code?\u00a0<\/p>\n<p>There is another approach to get all the versions, let\u2019s use the versions of the list items instead of the files:\u00a0<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nSPDocumentLibrary sharedDocs = SPContext.Current.Web.Lists&#x5B;'Shared Documents'] as SPDocumentLibrary;\r\nresultBox.Text = string.Empty;\r\nforeach (SPListItem doc in sharedDocs.Items)\r\n{\r\n\u00a0\u00a0\u00a0\u00a0 SPListItemVersionCollection coll = doc.Versions;\r\n\u00a0\u00a0\u00a0\u00a0 resultBox.Text += 'Versions of\u00a0 '\u00a0+ doc.Name + Environment.NewLine;\r\n\u00a0\u00a0\u00a0\u00a0 foreach (SPListItemVersion version in coll)\r\n\u00a0\u00a0\u00a0\u00a0\u00a0{\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 resultBox.Text += 'VersionLabel:\u00a0' + version.VersionLabel +\u00a0' IsCurrentVersion:\u00a0' + version.IsCurrentVersion + Environment.NewLine;\r\n\u00a0\u00a0\u00a0\u00a0 }\r\n}\r\n<\/pre>\n<p>And the result:<br \/>\n<a href=\"https:\/\/www.itidea.nl\/wp-content\/uploads\/2010\/06\/DocLibVersionHistoryResultByCodeListItems.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-432\" title=\"DocLibVersionHistoryResultByCodeListItems\" src=\"https:\/\/www.itidea.nl\/wp-content\/uploads\/2010\/06\/DocLibVersionHistoryResultByCodeListItems.png\" alt=\"\" width=\"342\" height=\"115\" srcset=\"https:\/\/www.itidea.nl\/wp-content\/uploads\/2010\/06\/DocLibVersionHistoryResultByCodeListItems.png 342w, https:\/\/www.itidea.nl\/wp-content\/uploads\/2010\/06\/DocLibVersionHistoryResultByCodeListItems-300x100.png 300w\" sizes=\"auto, (max-width: 342px) 100vw, 342px\" \/><\/a>\u00a0<\/p>\n<p>Notice that all the existing versions, including the current version, are displayed. Also the sorting is the other way around compared to the listing based on the file versions.\u00a0<\/p>\n<p>The conclusion here is that the SPFileVersionCollection only shows the versions, <span style=\"text-decoration: underline;\">without the current version<\/span> and the SPListItemVersionCollection shows all the versions, <span style=\"text-decoration: underline;\">including the current version<\/span>.\u00a0<\/p>\n<p>Suppose you want to delete or put all the previous versions of a file to the recycle bin, you can easily use the SPFileVersionCollection option demonstrated above. This way you don\u2019t have to exclude the current version from your deleting code as you would with the SPListItemVersionCollection option.\u00a0<\/p>\n<p>Here\u2019s an example of this:<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nSPDocumentLibrary sharedDocs = SPContext.Current.Web.Lists&#x5B;'Shared Documents'] as SPDocumentLibrary;\r\nresultBox.Text = 'To recycle bin:\u00a0' + Environment.NewLine;\r\nforeach (SPListItem doc in sharedDocs.Items)\r\n{\r\n\u00a0\u00a0\u00a0\u00a0 SPFileVersionCollection coll = doc.File.Versions;\r\n\u00a0\u00a0\u00a0\u00a0 if (coll.Count != 0)\r\n\u00a0\u00a0\u00a0\u00a0 {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 resultBox.Text += doc.Name + Environment.NewLine;\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0doc.File.Versions.RecycleAll();\r\n\u00a0\u00a0\u00a0\u00a0 }\r\n}\r\n<\/pre>\n<p>The result of questioning the SPFileVersionCollection after this action:<br \/>\n<a href=\"https:\/\/www.itidea.nl\/wp-content\/uploads\/2010\/06\/SPFileVersionCollectionAfterRecyle.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-431\" title=\"SPFileVersionCollectionAfterRecyle\" src=\"https:\/\/www.itidea.nl\/wp-content\/uploads\/2010\/06\/SPFileVersionCollectionAfterRecyle.png\" alt=\"\" width=\"333\" height=\"36\" srcset=\"https:\/\/www.itidea.nl\/wp-content\/uploads\/2010\/06\/SPFileVersionCollectionAfterRecyle.png 333w, https:\/\/www.itidea.nl\/wp-content\/uploads\/2010\/06\/SPFileVersionCollectionAfterRecyle-300x32.png 300w\" sizes=\"auto, (max-width: 333px) 100vw, 333px\" \/><\/a><\/p>\n<p>The result of questioning the SPListItemVersionCollection after this action:<br \/>\n<a href=\"https:\/\/www.itidea.nl\/wp-content\/uploads\/2010\/06\/SPListItemVersionCollectionAfterRecyle.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-430\" title=\"SPListItemVersionCollectionAfterRecyle\" src=\"https:\/\/www.itidea.nl\/wp-content\/uploads\/2010\/06\/SPListItemVersionCollectionAfterRecyle.png\" alt=\"\" width=\"327\" height=\"53\" srcset=\"https:\/\/www.itidea.nl\/wp-content\/uploads\/2010\/06\/SPListItemVersionCollectionAfterRecyle.png 327w, https:\/\/www.itidea.nl\/wp-content\/uploads\/2010\/06\/SPListItemVersionCollectionAfterRecyle-300x48.png 300w\" sizes=\"auto, (max-width: 327px) 100vw, 327px\" \/><\/a><\/p>\n<p>Notice that the current version of the file still exists after the recycle action. With the SPFileVersionCollection there is one version besides the actual current version, which is not listed here. This is because the actual current version is still a draft version.<br \/>\nWith the SPListItemVersionCollection both versions are listed, the top one is the draft, the second the published version.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A nice part of document libraries and lists is the version history. Nothing new, we had it in MOSS 2007, but I previously never dived really into the version history by code. Recently I did and discovered something that might &#8230; <a class=\"more-link\" href=\"https:\/\/www.itidea.nl\/index.php\/documents-and-versioning\/\">Read More &raquo;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[20,25,42,6],"class_list":["post-429","post","type-post","status-publish","format-standard","hentry","category-sharepoint-2010","tag-c","tag-sandboxed-solutions","tag-sharepoint-2010","tag-visual-studio"],"_links":{"self":[{"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/posts\/429","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/comments?post=429"}],"version-history":[{"count":8,"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/posts\/429\/revisions"}],"predecessor-version":[{"id":440,"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/posts\/429\/revisions\/440"}],"wp:attachment":[{"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/media?parent=429"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/categories?post=429"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/tags?post=429"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}