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)

    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 !!

Monday, July 20, 2009

Interrogate the Document Version within the ItemCheckingIn Event Receiver?

I've added an event receiver to a content type, and I've overridden the ItemCheckingIn event. My scenario is that if the document is being checked in as a Major version (i.e. x dot zero) then unless it fulfils some other criteria I want to prevent the check in from occurring (i.e. set the properties.Cancel property to true, and set the properties.ErrorMessage property to some message indicate that the user must perform some action or other before a major version can be checked in).

So my scenario is that in the ItemCheckingIn event receiver method I want to interrogate whether the document is being checked in as a minor or a major version - sounds simple enough, right?

I first of all checked the following property within my event receiver:


But I soon realised that since ItemCheckingIn is the synchronous event receiver method, then the item hasn't actually been checked in yet, so I won't have the new version number applied yet. So I changed this to check the following within my event receiver:


However this still doesn't work – when I check in a major version, it just gives me the minor version number. If I check this property afterwards in the ItemCheckedIn event then I DO get the correct major version number, but obviously this is too late to cancel the check in operation since ItemCheckedIn is the asynchronous event receiver and it's not possible to prevent the operation in this event receiver method.

Has anyone come across this problem before – in the ItemCheckingIn event receiver method how can I tell what version of the document is being checked in i.e. whether it's a major or a minor version?!