• 关键字类型托管元数据保存 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]);
                }
            }   

    MOSS技术交流群:69022156


            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]);
                }
            }  

    上善若水 海纳百川
  • 相关阅读:
    sns矩阵
    家庭博客
    媒体治国
    论坛有
    模型指向同一地址,结构体指向不同地址
    苹果开发者公司账号添加个人开发者 加入不了开发者团队
    SourceTree下载 及使用
    关于swift语言中导入OC三方类找不到头文件的解决方法
    iPhone iPad 各种控件默认高度
    iOS 基于MVC设计模式的基类设计
  • 原文地址:https://www.cnblogs.com/NetUser/p/2608657.html
Copyright © 2020-2023  润新知