I have a document library with versioning enabled in SharePoint 2010. I want to get the file which is checked-in and for any checked-out file, i need to get its latest checked-in version (document). I am using c# server side object model on SharePoint 2010. Can anyone help me out please?
Asked
Active
Viewed 688 times
0
-
1It would be awesome if you could provide a [mcve] of your attempt so far. – mjwills Jan 03 '19 at 11:17
1 Answers
0
This could help. It will iterate through all items in the list and if the item is Checked Out it will find latest Published version.
using Microsoft.SharePoint;
using System;
using System.Linq;
class Program
{
static void Main(string[] args)
{
using (SPSite site = new SPSite("https://sharepoint.domain.com"))
using (SPWeb web = site.OpenWeb())
{
SPList list = web.GetList($"{web.ServerRelativeUrl.TrimEnd('/')}/DocumentLibrary");
SPListItemCollection items = list.GetItems(new SPQuery());
foreach (SPListItem item in items)
{
object checkedOutTo = item[SPBuiltInFieldId.CheckoutUser];
if (checkedOutTo == null)
{
// Latest version
Console.WriteLine($"{item.ID} | {item.Versions[0].VersionLabel}");
// Here are bytes of the file itself
byte[] bytes = item.File.OpenBinary();
}
else
{
// Find latest published version
SPFileVersion version = item.File.Versions
.Cast<SPFileVersion>()
.OrderByDescending(v => v.ID)
.Where(v => v.Level == SPFileLevel.Published)
.FirstOrDefault();
if (version != null)
{
Console.WriteLine($"{item.ID} | {version.VersionLabel} | {checkedOutTo}");
// Here are bytes of the file itself
byte[] bytes = version.OpenBinary();
}
else
Console.WriteLine($"{item.ID} | No published version | {checkedOutTo}");
}
}
}
}
}

Lukas Nespor
- 1,238
- 1
- 14
- 22
-
Hi Lukas thanks for your valuable response! This will give me only the version number whereas i need a actual document of that version number. – Kalpesh Jan 04 '19 at 03:41
-