/// <summary> /// Converts the specified value. /// </summary> /// <param name="value">The value.</param> /// <param name="arguments">The arguments.</param> /// <returns> /// The converted value. /// </returns> public override object Convert(object value, SharePointListItemConversionArguments arguments) { TaxonomyValue convertedValue = null; if (value == DBNull.Value) { return null; } var taxonomyFieldValue = value as TaxonomyFieldValue; if (taxonomyFieldValue == null) { var stringValue = value as string; if (!string.IsNullOrEmpty(stringValue)) { taxonomyFieldValue = new TaxonomyFieldValue(stringValue); } } if (taxonomyFieldValue != null && !string.IsNullOrEmpty(taxonomyFieldValue.TermGuid)) { if (SPContext.Current != null) { // Resolve the Term from the term store, because we want all Labels and we want to // create the TaxonomyValue object with a label in the correct LCID (we want one with // LCID = CurrentUICUlture.LCID Term underlyingTerm = this.taxonomyService.GetTermForId(SPContext.Current.Site, new Guid(taxonomyFieldValue.TermGuid)); if (underlyingTerm != null) { convertedValue = new TaxonomyValue(underlyingTerm); } } else { // We don't have access to a SPContext (needed to use the TaxonomyService), so we need to // fall back on the non-UICulture-respecting TaxonomyValue constructor convertedValue = new TaxonomyValue(taxonomyFieldValue); } } return convertedValue; }
private static TaxonomyValue GetTaxonomyValueForLabelInternal(TermStore termStore, Group termStoreGroup, TermSet termSet, string termLabel) { Term term = GetTermForLabelInternal(termStore, termStoreGroup, termSet, termLabel); TaxonomyValue value = null; if (term != null) { value = new TaxonomyValue(term); } return value; }
/// <summary> /// Converts the specified value. /// </summary> /// <param name="value">The value.</param> /// <param name="arguments">The arguments.</param> /// <returns> /// The converted value. /// </returns> public override object Convert(object value, DataRowConversionArguments arguments) { if (value == System.DBNull.Value) { return null; } TaxonomyValue convertedValue = null; var taxonomyFieldValue = value as TaxonomyFieldValue; if (taxonomyFieldValue == null) { var stringValue = value as string; if (!string.IsNullOrEmpty(stringValue)) { var fieldObject = arguments.FieldCollection.Cast<SPField>() .FirstOrDefault(f => f.InternalName == arguments.ValueKey); if (fieldObject != null) { taxonomyFieldValue = new TaxonomyFieldValue(fieldObject); taxonomyFieldValue.PopulateFromLabelGuidPair(stringValue); } else { return null; } } } if (taxonomyFieldValue != null && !string.IsNullOrEmpty(taxonomyFieldValue.TermGuid)) { if (SPContext.Current != null) { // Resolve the Term from the term store, because we want all Labels and we want to // create the TaxonomyValue object with a label in the correct LCID (we want one with // LCID = CurrentUICUlture.LCID Term underlyingTerm = this.taxonomyService.GetTermForId(SPContext.Current.Site, new Guid(taxonomyFieldValue.TermGuid)); if (underlyingTerm != null) { convertedValue = new TaxonomyValue(underlyingTerm); } } else { // We don't have access to a SPContext (needed to use the TaxonomyService), so we need to // fall back on the non-UICulture-respecting TaxonomyValue constructor convertedValue = new TaxonomyValue(taxonomyFieldValue); } } return convertedValue; }