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