El Blanco's Office 2007 Blog

Wednesday, July 29, 2009

Update: ItemCheckingIn Event and Detecting a Major Document Version

In my last post I detailed a problem where within the ItemCheckingIn event receiver I couldn't determine whether the document was being checked in as a major or a minor version and this was causing me big problems. I posted on the MSDN forums and Aaron Han from Microsoft very kindly investigated the issue in detail and came up with a solution.

The solution is only for those of us running Service Pack 2, but I am running it so it's not an issue for me. If you're hitting this same issue, then here's a reason to update !

The solution revolves around the "vti_level" property that is available in the BeforeProperties and AfterProperties collections. Here's the breakdown of what this contains when checking in a major version:

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)

Thanks to SP2 we can now write code such as the following within our ItemCheckingIn event receiver that can detect whether the item being checked in is a major version:

public override void ItemCheckingIn(SPItemEventProperties properties)
{
    base.ItemCheckingIn(properties);

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

    if((beforeLevel==255) && (afterLevel==1))
    {
        // This is a major version that is being checked in . . .
        int majorVer= properties.ListItem.File.MajorVersion+1;
    }
}

Hopefully this is helpful as I can't believe I'm the only one who's ever needed this information. Thanks to Aaron Han from Microsoft !!

0 Comments:

Post a Comment

<< Home