TaxonomyField set field value in event receiver, SharePoint 2010

Posted on 2013-05-23
Medium Priority
Last Modified: 2013-05-29
I'm trying to set a taxonomy field to a specific value. The code below works but does not save the change. I know it works because if I force the change via the Update method, the user sees an error message about a save conflict but the value *is* changed in the list.

Dim fld As TaxonomyField = DirectCast(properties.ListItem.Fields("Product"), TaxonomyField)
Dim taxo As New TaxonomySession(web.Site)
Dim t As Term = taxo.DefaultSiteCollectionTermStore.GetTermSet(fld.TermSetId).GetTerm(New Guid(g))            
fld.SetFieldValue(properties.ListItem, t)
properties.ListItem.Update() '<< Works only if Update is called

Open in new window

Is there some other method that has to get called to save such a change? I tried fld.Update() with no effect.

The other non-taxo fields being updated in this event receiver work just fine.
Question by:CraigYellick
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
LVL 18

Expert Comment

ID: 39193622
If you are using synchronous event receivers (e.g. ItemAdding or ItemUpdating) you should not call Update on updating items, since it will cause conflict. Instead you should use AfterProperties collection to change item field values, like this:

properties.AfterProperties["TaxonomyFieldInternalName"] = "TermLabel|TermGuid";

no need to call Update after changing AfterProperties.

More info: http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spitemeventproperties.afterproperties(v=office.14).aspx
LVL 11

Author Comment

ID: 39194534
Thanks for the response. I know that I can't call Update. The question is, why is the property change not being saved.  The AfterProperties object is not compatible with:  

fld.SetFieldValue(properties.ListItem, t)

Open in new window

Which is the only way I've seen to set the taxo field value.
LVL 11

Author Comment

ID: 39194572
I should point out that assigning the AfterProperties for the field with the "Label|GUID" string results in this error message:

One or more field types are not installed properly. Go to the list settings page to delete these fields.<nativehr>0x81020014</nativehr><nativestack></nativestack>

The error message is not correct, the fields are fine and can be updated in the UI. Fields also work just fine with the ListItem.Update. If it wasn't for the conflict error this would work.
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

LVL 18

Expert Comment

ID: 39194687
This probably means that field is not returned in the query you used to get item before update or field name is incorrect. Can you post code that gets item and calls update?
LVL 11

Accepted Solution

CraigYellick earned 0 total points
ID: 39194710
Solved it! What an ordeal.  Turns out is it flat-out impossible to update a taxo field from an ItemAdding event.  Can only be done through the ListItem object, in the ItemAdded event.

If you scroll way down in the following article you'll come across the discussion.


I learned something else as well: You can set the ItemAdded event to run synchronously , so it behaves like ItemAdding.  That's important so the user sees the change immediately when the dialog closes and they see the list item.
LVL 11

Author Closing Comment

ID: 39203934
Wanted to mark as a solution for anyone else searching on this topic. It's far from obvious how taxo fields work.

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There is one common problem that all we SharePoint developers share: custom solution deployment. This topic can't be covered fully in this short article, so all I want to do in this one is to review it from a development-to-operations perspectiv…
SharePoint Designer 2010 has tools and commands to do everything that can be done with web parts in the browser, and then some – except uploading a web part straight into a page that is edited in SPD. So, can it be done? Scenario For a recent pr…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

801 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question