I am trying to use sharepoint foundation 2010 as file store for a website. Every document update must go thru an approval cycle and finally gets to a approved status to show up on the website for end users. When a document is updated in sharepoint the status is reset to "Draft" even if the previous version was approved. This is the intended behavior.
File.Versions gives me the list of versions.
How to get the version that was last "Approved"?
You will need to loop through the versions of the ListItem and find the latest one that has been Published. Use the SPListItemVersionCollection to loop through the versions and check the SPFileLevel
As per the Sebastian Wojciechowski's community addition for the MSDN article on SPListItemVersionCollection
SPListItem.Versions[0] //this is current version of the item
SPListItem.Versions[1] //this is previous version of the item
SPListItem.Versions[SPListItem.Versions.Count - 1] //this is first version of the item
the versions are indexed in reverse order (newest to oldest) thus your code will be something like:
// Retrieve all versions
SPListItemVersionCollection itemVersions = item.Versions;
for (int i = 0; i < itemVersions.Count - 1; i++)
{
// Check if version is published
if (itemVersions[i].Level == SPFileLevel.Published)
{
return itemVersions[i];
}
}