public static bool IsEmptyWord(string word, string lang, AppWebHelper hlp) { if (_EmptyWords == null || EmptyKeywordsNeedsRefresh) { lock (LockObject) { if (_EmptyWords == null || EmptyKeywordsNeedsRefresh) { AutoTaggingHelper.FillEmptyWords(hlp); EmptyKeywordsNeedsRefresh = false; } } } var ww = _EmptyWords.Where(w => w.word == word && w.lang == lang).SingleOrDefault(); return((ww != null) ? true:false); }
/// <summary> /// Helper Method to set a Taxonomy Field on a list item /// </summary> /// <param name="ctx">The Authenticated ClientContext</param> /// <param name="listItem">The listitem to modify</param> /// <param name="model">Domain Object of key/value pairs of the taxonomy field & value</param> public static void SetTaxonomyFields(ClientContext ctx, ListItem listItem, string FileContent, string ListId, string url) { FieldCollection _fields = listItem.ParentList.Fields; ctx.Load(ctx.Web.AllProperties); ctx.Load(_fields); ctx.ExecuteQuery(); AppWebHelper hlp = new AppWebHelper(url, false); List <GlobalSetting> settings = GetGlobalConfig(hlp); LogHelper.Log(settings.Count.ToString()); var enabled = settings.Where(s => s.key == Constants.EnableKeywordCreation).SingleOrDefault(); bool KeywordCreationEnabled = Convert.ToBoolean( settings.Where(s => s.key == Constants.EnableKeywordCreation).SingleOrDefault().value); int KeywordRecognitionTreshold = Convert.ToInt32( settings.Where(s => s.key == Constants.KeywordRecognitionTreshold).SingleOrDefault().value); int KeywordCreationTreshold = Convert.ToInt32( settings.Where(s => s.key == Constants.KeywordCreationTreshold).SingleOrDefault().value); List <string> ConfiguredFields = hlp.ListTaxFields(ListId); foreach (var _f in _fields) { if (ConfiguredFields.Contains(_f.Id.ToString())) { TaxonomyField _field = ctx.CastTo <TaxonomyField>(_fields.GetById(_f.Id)); if (_f.InternalName != Constants.TaxFieldInternalName) { ctx.Load(_field); ctx.ExecuteQuery(); Collection <Term> MatchingTerms = null; MatchingTerms = AutoTaggingHelper.MatchingTerms(FileContent, ctx, _field.TermSetId, _field.AnchorId); if (MatchingTerms.Count > 0) { LogHelper.Log("Updating taxfield " + _field.Title); if (_field.AllowMultipleValues) { _field.SetFieldValueByCollection(listItem, MatchingTerms, 1033); } else { _field.SetFieldValueByTerm(listItem, MatchingTerms.First(), 1033); } listItem.Update(); ctx.ExecuteQuery(); } } else { TaxonomyTerms tt = new TaxonomyTerms(ctx); string TextLanguage = AutoTaggingHelper.LanguageIdentifier.Identify(FileContent).FirstOrDefault().Item1.Iso639_3; StringBuilder EntKeyWordsValue = new StringBuilder(); Dictionary <string, int> tokens = Tokenize(FileContent, KeywordRecognitionTreshold, TextLanguage); StringBuilder TokenString = new StringBuilder(); foreach (KeyValuePair <string, int> token in tokens) { Guid KeywordId = TaxonomyTerms.GetKeyword(token.Key); TokenString.AppendFormat("{0}|", token.Key); if (KeywordId != Guid.Empty) { EntKeyWordsValue.AppendFormat("-1;#{0}|{1};", token.Key, KeywordId); } else { if (KeywordCreationEnabled && token.Value >= KeywordCreationTreshold && !AutoTaggingHelper.IsEmptyWord(token.Key.ToLowerInvariant(), TextLanguage, hlp)) { Guid g = AddKeyWord(token.Key, ctx); if (g != Guid.Empty) { EntKeyWordsValue.AppendFormat("-1;#{0}|{1};", token.Key, g); } } } } LogHelper.Log(TokenString.ToString()); if (EntKeyWordsValue.ToString().Length > 0) { LogHelper.Log("keyword value " + EntKeyWordsValue.ToString(), LogSeverity.Error); TaxonomyFieldValueCollection col = new TaxonomyFieldValueCollection(ctx, string.Empty, _field); col.PopulateFromLabelGuidPairs(EntKeyWordsValue.ToString()); _field.SetFieldValueByValueCollection(listItem, col); listItem.Update(); ctx.ExecuteQuery(); } } } } }