locked
How to get the minor version count of a list item using C# RRS feed

  • Question

  • Hi,

    I am trying to work on an application that would go in a list, then look at every item in the list and calculate the number of major and minor versions that exist indivdually. I seem to be running into the issue where file.MajorVersion can only tell me the highest version of the file and File.Versions.Count gives all the versions that exist regardless of whether they are Major or Minor. That being said, I was hoping to be able to get the item minor version count and delete the versions that go over the limit per item.

    Is there a way to do that please using C#?

    Thank you


    Stech

    Friday, April 19, 2013 5:02 PM

Answers

  • Hi Stanley,

    From your description, I understand that you want to limit the major and minor numbers of version that kept, if so, SharePoint by default have settings for this, you can go to library settings->version settings, there are options to limit the number of versions to retain: keep the following number of major versions and keep drafts for the following number of major versions.

    If you want to do this programmatically, you can use SPFile.Versions.VersionLabel, it will show the version number, you can iterate all the versions and check it is major or minor version based on versionlabel, then delete the unwanted versions.

    Code sample:

    using (SPSite site = new SPSite("http://siteurl/"))

                {

                    using (SPWeb web = site.OpenWeb())

                    {

                        SPList list = web.Lists["document1"];

                        SPListItem item = list.GetItemById(110);

                        SPFile file = item.File;

                        SPFileVersionCollection vcoll = file.Versions;

                        foreach (SPFileVersion fversion in vcoll)

                        {

                            Console.WriteLine("version label:{0},url:{1}", fversion.VersionLabel, fversion.Url);

                        }

                        Console.WriteLine("***********************************");

                       

                        SPListItemVersionCollection ivcoll = item.Versions;

                        foreach (SPListItemVersion iversion in ivcoll)

                        {

                            Console.WriteLine("version label:{0},url:{1}", iversion.VersionLabel, iversion.Url);

                        }

                    }

                }

    Thanks,
    Qiao
    Forum Support
    Please remember to mark the replies as answers if they help and unmark them if they provide no help. If you have feedback for TechNet Subscriber Support, contact
    tnmff@microsoft.com.


    Qiao Wei
    TechNet Community Support

    Monday, April 22, 2013 9:22 AM
    Moderator
  • Hi Stech,

    You can check whether it is a major and minor version based on the version label, then delete the unused versions based on the version limit, for example, first get a version file, check the version label, if it is larger than *.2, or if it is not *.0,*.1,*.2, then delete them.

    Thanks,


    Qiao Wei
    TechNet Community Support

    Wednesday, April 24, 2013 1:49 AM
    Moderator

All replies

  • Hi Stanley,

    From your description, I understand that you want to limit the major and minor numbers of version that kept, if so, SharePoint by default have settings for this, you can go to library settings->version settings, there are options to limit the number of versions to retain: keep the following number of major versions and keep drafts for the following number of major versions.

    If you want to do this programmatically, you can use SPFile.Versions.VersionLabel, it will show the version number, you can iterate all the versions and check it is major or minor version based on versionlabel, then delete the unwanted versions.

    Code sample:

    using (SPSite site = new SPSite("http://siteurl/"))

                {

                    using (SPWeb web = site.OpenWeb())

                    {

                        SPList list = web.Lists["document1"];

                        SPListItem item = list.GetItemById(110);

                        SPFile file = item.File;

                        SPFileVersionCollection vcoll = file.Versions;

                        foreach (SPFileVersion fversion in vcoll)

                        {

                            Console.WriteLine("version label:{0},url:{1}", fversion.VersionLabel, fversion.Url);

                        }

                        Console.WriteLine("***********************************");

                       

                        SPListItemVersionCollection ivcoll = item.Versions;

                        foreach (SPListItemVersion iversion in ivcoll)

                        {

                            Console.WriteLine("version label:{0},url:{1}", iversion.VersionLabel, iversion.Url);

                        }

                    }

                }

    Thanks,
    Qiao
    Forum Support
    Please remember to mark the replies as answers if they help and unmark them if they provide no help. If you have feedback for TechNet Subscriber Support, contact
    tnmff@microsoft.com.


    Qiao Wei
    TechNet Community Support

    Monday, April 22, 2013 9:22 AM
    Moderator
  •  

    The following is the code snippet which is used to do the versioning settings.

    /***************************************************************/
            ///Enable Versioning to lists
            ///
            SPContext.Current.Web.AllowUnsafeUpdates = true;
            SPList objList = SPContext.Current.Web.Lists["TestDoc"];
     
            //EnableVersioning:- True if versioning is enabled for the document library; Otherwise false. 
            objList.EnableVersioning = true;
            //EnableMinorVersions:- true if minor versions are enabled when versioning is enabled for the document library; otherwise false.
            objList.EnableMinorVersions = true;
            //EnableModeration: property is true if Content Approval is enabled; otherwise, false
            objList.EnableModeration = true;
            //ForceCheckout:- This property is true if Forced checkout is enabled for the document library; otherwise false
            objList.ForceCheckout = true;
            //DraftVersion        objList.DraftVersionVisibility = DraftVisibilityType.Author;
            objList.Update();
      SPContext.Current.Web.AllowUnsafeUpdates = false;
            /****************************************************************/ 

    In this we can play with the versioning settings for a list.


    Thanks, Amit Kumar, LinkedIn Profile ** My Blog

    Tuesday, April 23, 2013 5:21 AM
  • Hi Qiao and Amit

    I would like to thank you for your replies about the versioning aspect of my application. However, what I am trying to accomplish is a little more than just setting the version limit or displaying the version label. Let's say for instance I have this item/file that has four majors and 15 minors and the policy is Major Limit is 4 and the MajorwithMinor Limit 2:

    1.0 ; 1.1 ; 1.2 ; 1.3 ; 1.4

    2.0 ; 2.1 ; 2.3

    3.0

    4.0 ; 4.1 ; 4.2 ; 4.3

    5.0 ; 5.1 ; 5.2 ; 5.3 ; 5.4

    6.0 ; 6.1 ; 6.2 

    I want to be able to delete any number of minor versions and major tho meet the above policy. In other words, the result we end up being something like:

    1.0 ; 1.1 ; 1.2

    2.0 ; 2.1 ; 2.3

    3.0

    4.0 ; 4.1 ; 4.2

    5.0 ; 5.1 ; 5.2

    6.0 ; 6.1 ; 6.2

    The problem that I am having right now is that I can't seem to get:

    1) the number of minor versions per item/file

    2) A way to delete only the minor versions.

    Stanley


    Stech

    Tuesday, April 23, 2013 3:42 PM
  • Hi Stech,

    You can check whether it is a major and minor version based on the version label, then delete the unused versions based on the version limit, for example, first get a version file, check the version label, if it is larger than *.2, or if it is not *.0,*.1,*.2, then delete them.

    Thanks,


    Qiao Wei
    TechNet Community Support

    Wednesday, April 24, 2013 1:49 AM
    Moderator
  • Hi,

    You can use the below code to get the Major versions and minor versions.

    Major versions(always version id should multiple or 512)

    item.Versions.Cast<SPListItemVersion>().Where(I => I.VersionId % 512 == 0).ToList();

    Minor Versions

    item.Versions.Cast<SPListItemVersion>().Where(I => I.VersionId % 512 == 0).ToList();

    Thanks,

    Rajesh

    Thursday, November 14, 2013 3:50 PM