Solved

TaxonomyField set field value in event receiver, SharePoint 2010

Posted on 2013-05-23
6
1,885 Views
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.
0
Comment
Question by:CraigYellick
  • 4
  • 2
6 Comments
 
LVL 18

Expert Comment

by:ivan_vagunin
Comment Utility
Hi!
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
0
 
LVL 11

Author Comment

by:CraigYellick
Comment Utility
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.
0
 
LVL 11

Author Comment

by:CraigYellick
Comment Utility
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.
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 18

Expert Comment

by:ivan_vagunin
Comment Utility
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?
0
 
LVL 11

Accepted Solution

by:
CraigYellick earned 0 total points
Comment Utility
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.

http://social.technet.microsoft.com/forums/en-US/sharepointdevelopmentprevious/thread/4634ec93-4aa3-4e1a-a0c3-6a66c3b3246e


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.
0
 
LVL 11

Author Closing Comment

by:CraigYellick
Comment Utility
Wanted to mark as a solution for anyone else searching on this topic. It's far from obvious how taxo fields work.
0

Featured Post

Complete Microsoft Windows PC® & Mac Backup

Backup and recovery solutions to protect all your PCs & Mac– on-premises or in remote locations. Acronis backs up entire PC or Mac with patented reliable disk imaging technology and you will be able to restore workstations to a new, dissimilar hardware in minutes.

Join & Write a Comment

I used to be SharePoint evangelist in our company, so my Outlook always full of questions about how to do this, or where I can find that. One day I found such an email with the following question: "how to attach 3-State workflow (one of the workflow…
Pimping Sharepoint 2007 without Server-Side Code Part 1 One of my biggest frustrations with Sharepoint 2007 in the corporate world is that while good-intentioned managers lock down the more interesting capabilities of Sharepoint programming in…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

728 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now