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