1

I'm trying to retrieve the selected TaxonomyField value that the user selects from the TaxonomyWebTaggingControl rather than just a string representation. The idea being to eventually create a TaxonomyFieldValue object from that result. i.e.

string selectedTaxonomyTerm = this.TaxonomyWebTaggingControl.Text; // represents the selected term's Label | Guid

TaxonomyFieldValue taxonomyFieldValue = TaxonomyFieldControl.GetTaxonomyValue(selectedTaxonomyTerm); // Converts to a TaxonomyFieldValue object, however ValidateString throws exception because no TaxonomyField is assigned.

// what i want
var selectedTaxonomyField = this.TaxonomyWebTaggingControl.Field // Get the selected Field 
var newTaxonomyValue = new TaxonomyFieldValue(selectedTaxonomyField);
newTaxonomyValue.PopulateFromLabelGuidPair(selectedTaxonomyTerm);

So basically i want a way to retrieve the underlying TaxonomyField from the selection of the TaxonomyWebTaggingControl. Any help would be great.

2 Answers 2

2

Just get the field from the list and then cast it to a TaxonomyField like this:

SPListItem item = ...;
TaxonomyField taxField = item.Fields["FieldName"] as TaxonomyField;
TaxonomyFieldValue taxFieldValue = new TaxonomyFieldValue(taxField);
taxFieldValue.PopulateFromLabelGuidPair(taxonomyCtrl.Text);
1
  • +1 I would probably use list.Fields intead of item.Fields Oct 9, 2011 at 14:27
1

I like Wictor's answer and already vote it up. But it might not fit your question, so I'll add my 2 cents here too.

Another approach you might want to explore, if you're looking for a more abstract way of putting this in the control, could be to go through the TaxonomyHiddenList, look for a term based on your label, and get all the information you need from the TaxonomySession. Is it worth the extra trouble? What is your scenario?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.