Prasad Bolla's SharePoint Blog

Click Here to go through the Interesting posts within my Blog.

Click Here to go through the new posts in my blog.

Monday, November 28, 2011

Programmatically get versions for files in sharepoint 2010 document library

Programmatically get versions for files in sharepoint 2010 document library

Here is code snippet to get Versions or should say version information for files in Sharepoint 2010 document library. The Code was written in a Console application.

This code assumes that you have a document library called “Mydocumentlib” set up with atleast one document and versioning enabled. In the below code the outer foreach loop inspects all the files, while the inner loop retrieves all the versions for each file.

using (SPSite site = new SPSite(“http://SharePointSite))
{
using (SPWeb web = site.OpenWeb())
{
SPList docs = web.Lists["Mydocumentlib"];

foreach (SPFile file in docs.RootFolder.Files)
{
Console.WriteLine(“File {0} has next version {1}. Version History:”, file.Url, file.UIVersionLabel);

foreach (SPFileVersion v in file.Versions.Cast().Reverse())
{
Console.WriteLine(” Version {0} checked in at {1} with this comment: ‘{2}’”, v.VersionLabel, v.Created, v.CheckInComment);
}
}
}}

No comments:

Post a Comment