Sharepoint 2010 Client OM - How to get the Last approved document version?

Go To StackoverFlow.com

1

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"?

2012-04-03 20:41
by Bhuvan


1

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];
    }
}
2012-04-24 17:26
by mundeep
Thanks for this code. This code has to run on the server side but I am trying to avoid server side programming for various reasons include creation of DEV box on desktop and deployment. As I am working on windows App, connection via CLOM, looking for a solution on that side - Bhuvan 2012-04-24 21:20