Ask a questionAsk a question
 

AnswerItemCheckingIn event and document version nunmber

Answers

  • Thursday, July 23, 2009 11:29 AMAaron Han - MSFTModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer

    Hi, Chris


        Thanks for your reminding, and after some deeper research, I think I need to do more detailed explanation about ItemCheckIn, ItemCheckOut related events.

    Yes, you can’t get major version inside the ItemCheckingIn, all these proprieties show the minor version:

    properties.AfterProperties["vti_sourcecontrolversion"]--- minor version

    properties.BeforeProperties["vti_sourcecontrolversion"]--- minor version

         properties.ListItem.File.UIVersionLabel--- minor version


        
    But there are 2 points I have to mention:

    ·         The minor version changed when you check out the file, so when ItemCheckingIn happens, the minor version has already be changed.

     

    ·         Usually, we use event with “ing” (ItemCheckingIn) to get original value(both in BeforeProperties and AfterProperties),and use event with “ed” to get updated value like this:

                properties.AfterProperties["vti_sourcecontrolversion"]--- minor version(before value)

    properties.BeforeProperties["vti_sourcecontrolversion"]--- major version(updated value)

         properties.ListItem.File.UIVersionLabel--- major version(updated value)

     


           So in your situation, you can get minor version correctly just because its value has been changed in check-out event.

       

           And as your requirement, you need to get major version in ItemCheckingIn event, you can get a walkaround if your installed SP2.

     

           When SP2 is installed, you can get if this is a major-version event or not, some properties changed

     

           Without SP2

     

              properties.BeforeProperties["vti_level"] (value is 255)

     

              properties.AfterProperties["vti_level"]  (value is 255)

     

           With SP2

           

              properties.BeforeProperties["vti_level"] (value is 255)

     

              properties.AfterProperties["vti_level"]  (value is 1)

     

         

          And you can get the major version like this(with SP2):

           

    ------------------------------------------------------------------------------------------

    public override void ItemCheckedIn(SPItemEventProperties properties)

    {

         base.ItemCheckedIn(properties);

         int beforeLevel= (int)properties.BeforeProperties["vti_level"]

         int afterLevel= (int)properties.AfterProperties ["vti_level"];

        //This condition to confirm this event is doing major version checking.

        if((beforeLevel==255)|| (afterLevel==1))

        {

             //Old major version+1 for new major version

             int majorVer= properties.ListItem.File.MajorVersion+1

        }

     }

    ------------------------------------------------------------------------------------------

    Also, if you use ItemCheckedIn event, you can get the differences between the changed versions easily.

     

    Hope this can help.

     

    Best Regards,

     

    -Aaron.

    • Marked As Answer byElBlanco Monday, July 27, 2009 10:52 AM
    •  

All Replies

  • Tuesday, July 21, 2009 10:17 AMAaron Han - MSFTModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    Hi, Chris

    It seems that you need to get ListItem major version inside the ItemCheckingIn event receiver.

    After some research, SPListItem use SPFileVersion’s Count property to display SPFile’s(SPListItem) major version.

    Some code sample like this:

    ------------------------------------------------------------------------------------------

    public override void ItemCheckedIn(SPItemEventProperties properties)

    {

         base.ItemCheckedIn(properties);

         int majorVersion= properties.ListItem.File.Versions.Count;

     }

    ------------------------------------------------------------------------------------------

    Hope this can help.

     

    Best Regards,

     

    Aaron

  • Tuesday, July 21, 2009 10:47 AMElBlanco Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hi Aaron,

    Thanks for the reply, but your solution doesn't appear to work.

    properties.ListItem.File.Versions.Count is incremented each time a version of the document is added, whether or not this is a major or a minor version. Hence I could check in 5 minor versions (i.e. version number is 0.5) and properties.ListItem.File.Versions.Count would give me 5, but the major version of the document is actually zero.

    Another minor point, but the code you show also uses ItemCheckedIn whereas I need to know whether the item being checked in is a major version inside the ItemCheckingIn event receiver.

    Kind Regards,

    Chris
  • Thursday, July 23, 2009 11:29 AMAaron Han - MSFTModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer

    Hi, Chris


        Thanks for your reminding, and after some deeper research, I think I need to do more detailed explanation about ItemCheckIn, ItemCheckOut related events.

    Yes, you can’t get major version inside the ItemCheckingIn, all these proprieties show the minor version:

    properties.AfterProperties["vti_sourcecontrolversion"]--- minor version

    properties.BeforeProperties["vti_sourcecontrolversion"]--- minor version

         properties.ListItem.File.UIVersionLabel--- minor version


        
    But there are 2 points I have to mention:

    ·         The minor version changed when you check out the file, so when ItemCheckingIn happens, the minor version has already be changed.

     

    ·         Usually, we use event with “ing” (ItemCheckingIn) to get original value(both in BeforeProperties and AfterProperties),and use event with “ed” to get updated value like this:

                properties.AfterProperties["vti_sourcecontrolversion"]--- minor version(before value)

    properties.BeforeProperties["vti_sourcecontrolversion"]--- major version(updated value)

         properties.ListItem.File.UIVersionLabel--- major version(updated value)

     


           So in your situation, you can get minor version correctly just because its value has been changed in check-out event.

       

           And as your requirement, you need to get major version in ItemCheckingIn event, you can get a walkaround if your installed SP2.

     

           When SP2 is installed, you can get if this is a major-version event or not, some properties changed

     

           Without SP2

     

              properties.BeforeProperties["vti_level"] (value is 255)

     

              properties.AfterProperties["vti_level"]  (value is 255)

     

           With SP2

           

              properties.BeforeProperties["vti_level"] (value is 255)

     

              properties.AfterProperties["vti_level"]  (value is 1)

     

         

          And you can get the major version like this(with SP2):

           

    ------------------------------------------------------------------------------------------

    public override void ItemCheckedIn(SPItemEventProperties properties)

    {

         base.ItemCheckedIn(properties);

         int beforeLevel= (int)properties.BeforeProperties["vti_level"]

         int afterLevel= (int)properties.AfterProperties ["vti_level"];

        //This condition to confirm this event is doing major version checking.

        if((beforeLevel==255)|| (afterLevel==1))

        {

             //Old major version+1 for new major version

             int majorVer= properties.ListItem.File.MajorVersion+1

        }

     }

    ------------------------------------------------------------------------------------------

    Also, if you use ItemCheckedIn event, you can get the differences between the changed versions easily.

     

    Hope this can help.

     

    Best Regards,

     

    -Aaron.

    • Marked As Answer byElBlanco Monday, July 27, 2009 10:52 AM
    •  
  • Monday, July 27, 2009 10:54 AMElBlanco Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hi Aaron,

    Many thanks for taking the time to investigate this further.

    I am running SP2, and I had to change one line in the sample you gave me - it should be an AND rather than an OR to test for a major version:

    if((beforeLevel==255) && (afterLevel==1))

    but apart from that it works like a charm.

    Many thanks again for getting to the bottom of this one for me !!!

    Kind Regards,

    Chris