• 保存列表中的托管元数据,TaxonomyField 字段类型


            internal const int MAX_LENGTH = 255;
            internal static readonly Regex NameRegex = new Regex("[;<>|\t]", RegexOptions.Compiled | RegexOptions.IgnoreCase);
            internal static readonly Regex SpaceRegex = new Regex("[ ]{2,}", RegexOptions.Compiled | RegexOptions.IgnoreCase);
            private string GetValidTermName(string name)
            {
                if (NameRegex.IsMatch(name))
                {
                    name = NameRegex.Replace(name, " ");
    
                }
                name = SpaceRegex.Replace(name, " ");
                name = name.Length > MAX_LENGTH ? name.Substring(0, MAX_LENGTH) : name;
                name = name.Replace(Convert.ToChar(38), Convert.ToChar(65286));
                return name.Trim();
            }
            private void WriteTagsToFolksonomyColumn(IEnumerable<string> tags, SPListItem item, TaxonomyField column)
            {
                if (!column.IsTermSetValid) return;
                if (!column.Open) return;
                var session = new TaxonomySession(item.Web.Site);
                var mms = session.TermStores[column.SspId];
                var ts = mms.GetTermSet(column.TermSetId);
                if (!ts.IsOpenForTermCreation) return;
                var addedTerms = new List<Term>();
                bool hasChanges = false;
                foreach (var tag in tags)
                {
                    if (tag.Length > MAX_LENGTH) continue;
                    var validTag = GetValidTermName(tag);
                    Term matchingTerm=null;
                    if (column.IsKeyword)
                    {
                        TermCollection terms = mms.GetTerms(validTag, false, StringMatchOption.ExactMatch, 1, true);
                        if (terms.Count > 0)
                            matchingTerm = terms[0];
                    }
                    else
                    {
                        TermCollection terms = ts.GetTerms(validTag, false, StringMatchOption.ExactMatch, 1, true);
                        if (terms.Count > 0)
                            matchingTerm = terms[0];
                    }
                    if (matchingTerm == null)
                    {
                        matchingTerm = ts.CreateTerm(validTag, mms.WorkingLanguage);
                        hasChanges = true;
                    }
                    if (!addedTerms.Contains(matchingTerm))
                    {
                        addedTerms.Add(matchingTerm);
                    }
                }
                if (hasChanges) mms.CommitAll();
                if (addedTerms.Count > 0)
                { 
                    if (column.AllowMultipleValues)
                        column.SetFieldValue(item, addedTerms, mms.WorkingLanguage);
                    else
                        column.SetFieldValue(item, addedTerms[0]);
                }
            }   
    

      

    上善若水 海纳百川
  • 相关阅读:
    Azure开发者任务之二:Cloud Service项目添加到ASP.Net Web中
    Azure开发者任务之一:解决Azure Storage Emulator初始化失败
    Configuring a Windows Azure Project
    How to manage the certificates in the PC
    在此声明我的博客已经搬到CSDN 中了
    http://www.cnblogs.com/Sniper-quay/archive/2011/06/22/2086636.html
    杂乱的UDPsocket
    socket下server端支持多客户端并发访问简单实现
    Qt 的udpSocket通信
    正则表达式
  • 原文地址:https://www.cnblogs.com/NetUser/p/2733891.html
Copyright © 2020-2023  润新知