1

I currently have a term set of document types that goes two levels deep. I'm using a table that a poweruser manages to sync the terms in the term store.

The problem I'm having is when I compare a string field with existing term store labels it does not match 100% of the time even though that label exists. So when I try to create a term it throws an error.

For example

string label = "(A1)Doc Type A & Misc";
Term childTerm = parentTerm.Terms[label];

//This throws an error saying it can't find the term so I try:

Term childTerm = parentTerm.CreateTerm(label, lcid);

//This throws an error saying the term already exists

After stepping through the code I found that label seems to get transposed so when it is made a term. Specifically double spaces are turned into single spaces and & seems to be changed as well. The only solution I found was the query below which seemed very hackish.

Term foundTerm2 = parentTerm.Terms.Where(tr =>
Encoding.UTF8.GetString(Encoding.Default.GetBytes(tr.GetDefaultLabel(lcid))) ==
  Encoding.UTF8.GetString(Encoding.Default.GetBytes(label.Replace("  ", " ")))
                                        ).FirstOrDefault();

Question
Is there a cleaner way to determine if a term exists based off a given label? I'm currently working in SharePoint Server 2010

  • Maybee a stupid question, but why don't you just give the power user contribute permissions on the term set and hence let them manage it themselfs? :) – Robert Lindgren Aug 8 '14 at 17:13
  • That's a valid question and probably a model we will go to down the road but the there are some other dependencies on this list that are requiring this process. I also would like to know if there is a cleaner method for this down the road. – RWL01 Aug 8 '14 at 17:46
2

I don't know if my way is cleaner, but it works. I basically format the term the way SharePoint stores it and then check to see if it already exists.

http://nickhobbs.wordpress.com/2012/03/29/sharepoint-2010-managed-metadata-converts-ampersand-and-double-quotes-to-unicode/

 //set term the way SharePoint stores it so that we can get a good compare with items already in store


                    string tag = term.Trim();
                    if (tag.Contains('&'))
                    {
                        tag = tag.Replace('&', Convert.ToChar(0xff06));
                    }
                    if (tag.Contains("  "))
                    {
                        tag = Regex.Replace(tag, @"\s+", " ");
                    }
                    if (tag.Contains("\""))
                    {
                        tag = tag.Replace('\"', Convert.ToChar(0xff02));
                    }

                    Term keywordTerm;

                    if (!termSet[0].Terms.Any(t => t.Name == tag))
                    {
                        Console.ForegroundColor = ConsoleColor.Red;
                        Console.WriteLine("Adding {0} to the term set", tag);
                        keywordTerm = termSet[0].CreateTerm(tag, CultureInfo.CurrentCulture.LCID);
                        needToCommit = true;
                    }
                    else
                    {
                        Console.ForegroundColor = ConsoleColor.Green;
                        Console.WriteLine("{0} already exists in the term set", tag);
                    }
  • How did you figure out this is the way SharePoint stores it? Do you know if this is an inclusive list of special cases? – RWL01 Aug 8 '14 at 18:18
  • It is ... I will look for a link where I found it and add it to the answer – AdamBT Aug 8 '14 at 18:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.