Craig Yellick
asked on
TaxonomyField set field value in event receiver, SharePoint 2010
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.
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.
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
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.
ASKER
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:
Which is the only way I've seen to set the taxo field value.
fld.SetFieldValue(properties.ListItem, t)
Which is the only way I've seen to set the taxo field value.
ASKER
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>0x8102001 4</nativeh r><natives tack></nat ivestack>
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.
One or more field types are not installed properly. Go to the list settings page to delete these fields.<nativehr>0x8102001
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.
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?
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER
Wanted to mark as a solution for anyone else searching on this topic. It's far from obvious how taxo fields work.
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
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