Wednesday, November 14, 2012

Unable to update SharePoint 2010's managed metadata field in custom developed Content Types/Library on SharePoint 2010


We have developed a Custom SharePoint 2010 site definition with all the libraries, lists and content types. Its been successfully deployed to production too.

Then we had an unusual problem, i.e.Updating the document managed metadata(Taxonomy field) property via 3rd party tool Colligo, doesn't get updated in the SharePoint library. 

Initially thought it would be colligo problem, but later in testing realised that, browsing directly in custom SharePoint site too, it updates only once then the managed metadata property doesn't get updated.

Went through lots of sites/blogs, about taxonomy field. 
Recommend to read this link" Dissecting-the-SharePoint-2010-Taxonomy-fields " for understanding internal of taxonomy field.

Everybody mentioning about the taxonomy hidden list and timer jobs, but it didn't solve the problem.

On testing the same taxonomy field in normal team site it works perfectly.

Later got revealed the taxonomy hidden facts, i,e, there are some in-built Taxonomy Event Receivers which needs to get added to the list/library to get it working similar in normal Team site.

The code for adding the taxonmy event reciever via a normal application
using (SPSite projectSite = new SPSite(siteText.Text.Trim()))
{
using (SPWeb projectWeb = projectSite.OpenWeb())
{
SPList projectList = projectWeb.Lists[Custom List Title];
//Initialise all the event recievers of the above list to the a collection
        SPEventReceiverDefinitionCollection listEventRecieverCollection = projectList.EventReceivers;
//Add the TaxonomyItemSynchronousAddedEventReceiver to the list (Built in Event Reciever from Microsoft - not custom code reciever)
SPEventReceiverDefinition AddedEventdef = listEventRecieverCollection.Add();
AddedEventdef.Assembly = "Microsoft.SharePoint.Taxonomy, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c";
AddedEventdef.Class = "Microsoft.SharePoint.Taxonomy.TaxonomyItemEventReceiver";
AddedEventdef.Name = "TaxonomyItemSynchronousAddedEventReceiver";
AddedEventdef.Type = SPEventReceiverType.ItemAdding;
AddedEventdef.SequenceNumber = 10000;
AddedEventdef.Synchronization = SPEventReceiverSynchronization.Synchronous;
AddedEventdef.Update();
//Add the TaxonomyItemUpdatingEventReceiver to the library (Built in Event Reciever from Microsoft - not custom code reciever)
SPEventReceiverDefinition UpdatingEventdef = listEventRecieverCollection.Add();
UpdatingEventdef.Assembly = "Microsoft.SharePoint.Taxonomy, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c";
UpdatingEventdef.Class = "Microsoft.SharePoint.Taxonomy.TaxonomyItemEventReceiver";
UpdatingEventdef.Name = "TaxonomyItemUpdatingEventReceiver";
UpdatingEventdef.Type = SPEventReceiverType.ItemUpdating;
UpdatingEventdef.SequenceNumber = 10000;
UpdatingEventdef.Synchronization = SPEventReceiverSynchronization.Synchronous;
UpdatingEventdef.Update();
}
}


Add the above in-built Taxonomy Event receiver while developing any custom solution with lists/libraries.

This solves a huge problem in updating the managed metadata in custom sites based solution.

2 comments: