/// <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) { TaxonomyValueCollection convertedValues = null; var taxonomyFieldValueCollection = value as TaxonomyFieldValueCollection; if (taxonomyFieldValueCollection == null) { var stringValue = value as string; if (!string.IsNullOrEmpty(stringValue)) { taxonomyFieldValueCollection = new TaxonomyFieldValueCollection(stringValue); } } if (taxonomyFieldValueCollection != null) { 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 var underLyingTerms = new List<Term>(); foreach (TaxonomyFieldValue taxonomyFieldValue in taxonomyFieldValueCollection) { if (!string.IsNullOrEmpty(taxonomyFieldValue.TermGuid)) { var foundTerm = this.taxonomyService.GetTermForId(SPContext.Current.Site, new Guid(taxonomyFieldValue.TermGuid)); if (foundTerm != null) { underLyingTerms.Add(foundTerm); } } } convertedValues = new TaxonomyValueCollection(underLyingTerms); } 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 TaxonomyValueCollection constructor convertedValues = new TaxonomyValueCollection(taxonomyFieldValueCollection); } } return convertedValues; }
private void AddTags(string message, TaxonomyValueCollection taxonomyValueCollection) { var hashTags = message.Split(' ').Where(token => token.StartsWith("#", StringComparison.OrdinalIgnoreCase)).Select(hashTag => hashTag.Remove(0, 1)); foreach (string hashTag in hashTags) { var correspondingTaxonomyValue = this._taxonomyService.GetTaxonomyValueForLabel(SPContext.Current.Site, ProjectTaxonomy.TermStoreGroupName, ProjectTaxonomy.WallTermSetName, hashTag); if (correspondingTaxonomyValue != null) { // A term with that label actually exists - use it as tag taxonomyValueCollection.Add(correspondingTaxonomyValue); } } }
/// <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) { TaxonomyValueCollection convertedValues = null; if (value == DBNull.Value) { return null; } var taxonomyFieldValueCollection = value as TaxonomyFieldValueCollection; if (taxonomyFieldValueCollection == 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) { taxonomyFieldValueCollection = new TaxonomyFieldValueCollection(fieldObject); taxonomyFieldValueCollection.PopulateFromLabelGuidPairs(stringValue); } else { return null; } } } if (taxonomyFieldValueCollection != null) { 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 var underLyingTerms = new List<Term>(); foreach (TaxonomyFieldValue taxonomyFieldValue in taxonomyFieldValueCollection) { if (!string.IsNullOrEmpty(taxonomyFieldValue.TermGuid)) { var foundTerm = this.taxonomyService.GetTermForId(SPContext.Current.Site, new Guid(taxonomyFieldValue.TermGuid)); if (foundTerm != null) { underLyingTerms.Add(foundTerm); } } } convertedValues = new TaxonomyValueCollection(underLyingTerms); } 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 TaxonomyValueCollection constructor convertedValues = new TaxonomyValueCollection(taxonomyFieldValueCollection); } } return convertedValues; }