Sunday, February 18, 2007

SPItemEventReceiver - Mapping end-user events to SharePoint events

How do you map the end-user events with the various SharePoint events you can capture and modify?




  • ItemAdding - i.e. adding a new item - ok
  • ItemAdded - the new item has been added - ok
  • ItemUpdating - updating an existing item
  • ItemUpdated - an existing item has been updated
  • ItemCheckingIn - about to checkin an item
  • ItemCheckedIn - item checked in
  • ItemCheckingOut - about to checkout an item
  • ItemCheckedOut - item checked out
  • ItemDeleting - about to delete an existing item
  • ItemDeleted - an existing item is deleted
  • ItemUncheckingOut -what does this really mean?
  • ItemUncheckedOut - well

If you look at the SharePoint menu for an approved item it'll show the following menu items:



What puzzles me is the "Unpublish this version" menu item? Which event is linked with this operation?

When you check-in an item you can also choose to check-in and overwrite the existing version - thereby damaging the version control.

Conclusion: The ItemUpdating event is the answer! If you inspect the ModerationInformation field of the listitem you can find out the state - and block the Unpublish This Version event.

4 comments:

Dinosaur man said...

I have a similar problem. I need to block unpublish event on a document library and also our sites are provisioned through code and I cannot hardcode the site anywhere. Any help from you would be highly appreciated.

Mano Mangaldas said...

Even if you get the moderationinformation.. you can never tell for what event the itemupdated is fired.

e.g if you want to block unpublish of a document.. you cannot handle it in the itemUpdated because.. there is nothing to say that 'Unpublish this Version' was clicked. That event will fire for multiple operations.. you cannot do anything specific when the UnPublish is clicked.

You can check from the URL the there is a querystring 'UnPublish=true' .. but it is useless because we cannot get the HTTPContext in eventhandlers.

you can go and modify the core.js to block it.. it is not a good practice.

the only remaining option is to override the function from the core.js file by adding your custom js file in the templates\1033 folder. Just make sure that the function name and the number of parameters are the same.
Now you can reference your custom js file from the master page using SharePoint:ScriptLink .. just place it after the core.js reference and 'defer=true'.

Anonymous said...

Good for people to know.

Morten Marquard said...

Apart from the fact that modifyimg core.js isn't good practise it doesn't fix unpublish dine programatically