Friday, December 31, 2010

SharePoint 2010: Taxonomy Event Receiver?

One of the power of SharePoint is extensibility. You can hook your custom code in different places in different time with Event Receiver. Taxonomy is new and powerful feature added in SharePoint 2010. Unfortunately, the taxonomy missing event receiver feature. If you want to do something when taxonomy added, deleted or updated, you are unlucky that SharePoint 2010 doesn’t provide event receiver in SharePoint 2010 for taxonomy.


Why taxonomy event receiver needed?

With taxonomy support, SharePoint is a good place to manage taxonomy. However, if I need this taxonomy to use in another application (like another application) then with the event receiver I could sync my application with SharePoint taxonomy. Also when a taxonomy is deleted I would like to run my own code to allow or disallow the deletion. So taxonomy event receiver would be very handy if it would be available.


How taxonomy managed in SharePoint?

Let’s discuss a bit about how taxonomy managed in SharePoint. Taxonomy is managed by central administration (with managed metadata service). So when you add/delete/update taxonomy, the taxonomies are managed in central administration site. However, for faster retrieval of taxonomies in individual web, a hidden list of taxonomies maintained in each site collection. So in each site collection, there’s a hidden list TaxonomyHiddenList that keeps the copy of taxonomies from central administration site. And in every hour a timer job “Taxonomy Update Scheduler” is run to sync the taxonomies between central admin and site collection. However, the hidden list in site collection doesn’t contain all taxonomies from central admin rather taxonomies that are used in the webs of the site collection. So the hidden taxonomy list have only keywords and taxonomies used in the webs of the site collection. You can get the contents of the hidden list by browsing “http://mysitecollection/Lists/TaxonomyHiddenList” where mysitecollection is your site collection url. Few things to notice for the hidden list:

  • In case of edit mode, the taxonomy is read from central administration and as user save the list item, the taxonomy/keywords is saved from hidden list.
  • In case of viewing an item, it’s for sure that the taxonomy/keyword is in hidden list (it was put in hidden list during edit mode). So the taxonomy/keyword is shown from hidden list.


Event Receiver for Taxonomy Hidden List

Now we know the taxonomies are kept in a hidden list at site collection level. And in every hour,  the hidden list is synchronized with central administration site. So if we add event receiver for the hidden taxonomy list,then in every hour our event receiver will be fired while the hidden list will be synchronized. So adding an event receiver for hidden taxonomy list will solve the problem apparently. But there’s still problem. Not all taxonomies are added to the hidden list from central administration. Only the taxonomies added to subsites of site collection, are added to the hidden list. Now the problem of event receiver for taxonomies can be divided into two categories:

  • Need event receiver only for taxonomies used in subsite of site collection: If you need the event receiver only for taxonomies used in subsites of site collection, then having the event receiver for hidden taxonomies will do. However, you’ll have to wait for one hour to fire the event receiver.
  • Need event receiver for all taxonomies: If you need your event receiver to be fired for every taxonomy (added/edited/deleted) manipulation, then tapping the hidden taxonomy list will not work as not all taxonomies will be added from central admin to hidden taxonomy list. So what might be the solution for this? One solution is to create a custom timer job of your own and in that timer job, use all taxonomies in a test list. Since all taxonomies are used in your site, so all taxonomies will be synchronized with the site collection and your event receiver will be fired for all taxonomies.

Solution (Re-explained)

so if only care for taxonomies used in the site collection then adding an event receiver for hidden list will work. In every hour the hidden taxonomy lists will be synchronized and your event receiver for that hidden list will be fired.

However, if you need to care all taxonomies (not just taxonomies used in the site collection), then you need to follow the steps:

  1. Create a test list with one metadata field.
  2. Create a timer job to add all metadata from central admin to the list’s metadata field. The purpose of this job is make sure all taxonomies from central admin are used in site collection.
  3. Now add the event receiver for hidden list and you are done.

1 comment:

Note: Only a member of this blog may post a comment.