SPListItem How to get the last published version?

Posted on 2010-11-15
Medium Priority
Last Modified: 2012-05-10
Hi there,

I will appreciate if somebody can please post the code to find only the last published version of SPListItem.

Question by:m_a_n
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3

Author Comment

ID: 34138825
This gets the published SPListItem (omitting any entries which do not have a published version).

What I need is "Get the last published version in an SPListItem, even if the latest version is not published".

Thanks for the effort buddy!

Author Comment

ID: 34141256
Any clues? Anybody? This is still open!
How Blockchain Is Impacting Every Industry

Blockchain expert Alex Tapscott talks to Acronis VP Frank Jablonski about this revolutionary technology and how it's making inroads into other industries and facets of everyday life.

LVL 12

Accepted Solution

jagssidurala earned 2000 total points
ID: 34142172
For Example:
The list like this

1 --Published
2 -- UP
3 --UP
4 -- P
5-- UP
6 -- Up

Sample code:
SPList list = web.Lists[listname];

SPListItem LastPublishedItem = new SPListItem();

foreach (SPListItem item in list.Items)
              LastPublishedItem = item;      

after excecution of above code the "LastPublishedItem" contains last published version of the above list,
in this case the 4 is the final output.

Refer this link for various properties and methods for SPListItem.



Assisted Solution

m_a_n earned 0 total points
ID: 34150185
This is what worked for me.

foreach (SPListItemVersion _liv in _li.Versions)
 if (_liv.Level == SPFileLevel.Published)
  //Do something

Author Comment

ID: 34150227
agssidurala was quite close, just missing a few lines as noted in my comment.

Thanks all.
LVL 12

Expert Comment

ID: 34151890
Thank you for accepting my sol.

Can u also post ur entire code so that i will know whole functionality....

Author Closing Comment

ID: 34182484
jagssidurala was quite close, just missing a few lines as noted in my comment.

Thanks all.

Featured Post

 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Note:  There are two main ways to deploy InfoPath forms:  Server-side and directly through the SharePoint site.  Deploying a server-side InfoPath form means the form is approved by the Administrator, thus allowing greater functionality in the form. …
A recent project that involved parsing Tableau Desktop and Server log files to extract reusable user queries for use in other systems. I chose to use PowerShell to gather the data, and SharePoint to present it...
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question