Jawab List Item Version History - Always one version off

  • 02 Mei 2012 3:54
     
      Memiliki Kode

    I am trying to obtain the version history for SharePoint documnet library items - in particular I want to get the details of the major versions (X.0)  of each document.

    In the following approach, it seems to work but is always off by one major version, as in, it is showing the check in comment and version number for up to one item less than the current version.  Can anyone help me with the following? It iterates through the SPListItems and the SPListItem.File.Versions and then does some field lookups and most importantly, grabs the version number from the loop. However, the results are as stated always one version off.. :


    foreach (SPListItem spListItem in docLib.Items)
    {
    	if (spListItem.File.MajorVersion == 0// return if there are no major version
    	continue;
     
    	int itemMajorVersionIndex = 0, fileTempIndex = 0, fileMajorVersionIndex = 0;
    	string fileVersionNumber = "";
    	foreach (SPListItemVersion itemVersion in spListItem.Versions)
    	{
    		int versionId;
    		bool isInt = int.TryParse(itemVersion.VersionLabel.Replace(".0"""), out versionId);
    		if (isInt)
    		{
    		break;
    		}
    		itemMajorVersionIndex++;
    		}
     
    		foreach (SPFileVersion version in spListItem.File.Versions)
    		{
    		int versionId;
    		bool isInt = int.TryParse(version.VersionLabel.Replace(".0"""), out versionId);
    		if (isInt)
    		{
    			fileMajorVersionIndex = fileTempIndex;
    			fileVersionNumber = version.VersionLabel;
    		}
    		fileTempIndex++;
    		}
     	        docs.Add(new Doc(spListItem.ID, ContentType.MyContentType)
    		{
    			Site = spListItem["ContentType"].ToString(), 
    			Section = GetFieldValueLookup(spListItem, "Sections"),
    			SubSection = GetFieldValueLookup(spListItem, "Sub Sections"),
    			Title = GetFieldValueLookup(spListItem, "Title").Trim(),
    			FileName = spListItem.File.Name,
    			DateModified = spListItem.Versions[itemMajorVersionIndex].Created,
    			Version = fileVersionNumber,
    			CheckInComment = spListItem.File.Versions.Count == 0 ? spListItem.File.CheckInComment : spListItem.File.Versions[fileMajorVersionIndex].CheckInComment,
    			ItemMajorVersionIndex = itemMajorVersionIndex,
    			FileMajorVersionIndex = fileMajorVersionIndex,
    			FileVersionCount = spListItem.File.Versions.Count
    		});

Semua Balasan

  • 02 Mei 2012 6:11
    Penjawab Pertanyaan
     
     Jawab Memiliki Kode

    Hello,

    I've done a similar control and to get the same trouble as you.

    for the current version I've had to use the "ListItem.Version" instead of the "listitem.file.version"

    To resolve this I've added this for the last version

     protected void Page_Load(object sender, EventArgs e)
            {
                try
                {
                    Controls.Add(new LiteralControl("<table border='1' width='100%'>"));
                    Controls.Add(new LiteralControl("<tr align='center'>"));
    
                    Controls.Add(new LiteralControl(string.Format("<th>Revision Number</th><th>Date</th><th>Created By</th><th>Comments</th>")));
                    Controls.Add(new LiteralControl("</tr>"));
    
                    foreach (SPFileVersion fileVersion in SPContext.Current.ListItem.File.Versions)
                    {
                        Controls.Add(new LiteralControl("<tr>"));
    
                        Controls.Add(new LiteralControl(string.Format("<td>{0}</td><td>{1}</td><td>{2}</td><td>{3}</td>", fileVersion.VersionLabel, fileVersion.Created, fileVersion.CreatedBy.Name,
                            fileVersion.CheckInComment)));
                        Controls.Add(new LiteralControl("</tr>"));
    
                    }
                    Controls.Add(new LiteralControl("<tr>"));
                    string[] temp = SPContext.Current.ListItem.Versions[SPContext.Current.ListItem.Versions.Count - 1]["Check In Comment"].ToString().Split('#');
                    Controls.Add(new LiteralControl(string.Format("<td>{0}</td><td>{1}</td><td>{2}</td><td>{3}</td>", SPContext.Current.ListItem.File.UIVersionLabel,
                        SPContext.Current.ListItem.File.TimeLastModified, SPContext.Current.ListItem.File.ModifiedBy.Name, (temp.Length > 1) ? temp[1] : string.Empty)));
                    Controls.Add(new LiteralControl("</tr>"));
                    Controls.Add(new LiteralControl("</Table>"));
    
                }
                catch (Exception ex)
                {
    
                    Controls.Add(new LiteralControl("Error during versioning processing : " + ex.Message));
                }
            }

    Hope this help


    Best regards, Christopher.
    Blog | Mail

    • Ditandai sebagai Jawaban oleh TobiT 02 Mei 2012 20:31
    •  
  • 02 Mei 2012 7:21
     
     Jawab Memiliki Kode

    OK, you have an issue with .Versions property. It does always give you one version less, from a simple reason that .Versions store the old versions, not the current one :)

    There is a working code which might help you:

    using (SPSite site = new SPSite("http://localhost"))
    {
        using (SPWeb web = site.OpenWeb())
        {

            SPList docsList = web.Lists["Shared Documents"];

            SPListItemCollection items = docsList.GetItems(new SPQuery());

            foreach (SPListItem item in items)
            {

                Console.WriteLine(item.File.Name);
                Console.WriteLine("=============");
                Console.WriteLine("Current version: " + item.File.UIVersionLabel);
                Console.WriteLine("Current version check in comment: " + item.File.CheckInComment);

                Console.WriteLine("Old Versions:");
                Console.WriteLine("-------------");
                foreach (SPFileVersion version in item.File.Versions)
                {
                    Console.WriteLine(version.VersionLabel  + " : " +  version.Level);
                    Console.WriteLine("CheckIn Comment: " + version.CheckInComment);
                    Console.WriteLine("");
                }

            }

        }
    }

    This lists current AND previous file versions (in 2 steps, you'll note), with check in comment. You can of course retrieve all the other properties you want, and do with them whatever you want.

    This is a copy/paste of the results of a previous code, for a document which is currently in the version 4.3:

    dokument 1 in deutsch.docx
    =============
    Current version: 4.3
    Current version check in comment: Comment to minor 4.3 version
    
    Old Versions:
    -------------
    1.0 : Published
    CheckIn Comment:
    
    2.0 : Published
    CheckIn Comment:
    
    3.0 : Published
    CheckIn Comment:
    
    3.1 : Draft
    CheckIn Comment:
    
    4.0 : Published
    CheckIn Comment: Some comment to 4.0
    
    4.1 : Draft
    CheckIn Comment:
    
    4.2 : Draft
    CheckIn Comment:

    Hope this might help :)

    Cheers, Adis

    PS. Oh yes, please don't iterate items in the list, like "foreach (SPListItem spListItem in docLib.Items)"

    If you do: erthquakes will happen, volcanoes will start spitting fire, Manchester City will win the english Premier league... and other terrible things. No, jokes aside, it's just not nice and performant to do so. Use the SPQuery to get the items in SPItemCollection, it's much nicer and faster, not to mention that it is according to the best practices :)


    Adis Jugo
    Microsoft MVP for SharePoint Server, Managing Consultant at PlanB.
    Blog: http://blog.sharedove.com/adisjugo/
    Twitter: @adisjugo