Skip to content
This repository has been archived by the owner on Aug 24, 2021. It is now read-only.

Commit

Permalink
fixed bug with onprem creation of taxonomy groups.
Browse files Browse the repository at this point in the history
1) termStore.GetGroup(groupId) does not work. had to use termStore.Groups.GetById(groupId) instead.

2)also removed creation of new guids for each group/termset/term. this was causing the subsequent terms to not find the group and termset already created.
  • Loading branch information
vman committed Nov 19, 2014
1 parent a005b2b commit a42fe94
Showing 1 changed file with 131 additions and 133 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -429,141 +429,139 @@ public static void ImportTerms(this Site site, string[] termLines, int lcid, Ter
}
clientContext.Load(termStore);
clientContext.ExecuteQuery();
foreach (string line in termLines)
{
// split up
string[] items = line.Split(new string[] { delimiter }, StringSplitOptions.None);
if (items.Count() > 0)
{
string groupItem = items[0];
string groupName = groupItem;
Guid groupId = Guid.Empty;
if (groupItem.IndexOf(";#") > -1)
{
groupName = groupItem.Split(new string[] { ";#" }, StringSplitOptions.None)[0];
groupId = new Guid(groupItem.Split(new string[] { ";#" }, StringSplitOptions.None)[1]);
}
TermGroup termGroup = null;
if (groupId != Guid.Empty)
{
termGroup = termStore.GetGroup(groupId);
}
else
{
termGroup = termStore.Groups.GetByName(NormalizeName(groupName));
}
try
{
clientContext.Load(termGroup);
clientContext.ExecuteQuery();
}
catch
{

}
if (termGroup.ServerObjectIsNull == null)
{
groupId = Guid.NewGuid();
termGroup = termStore.CreateGroup(NormalizeName(groupName), groupId);
clientContext.Load(termGroup);
clientContext.ExecuteQuery();
}
if (items.Count() > 1)
{
// TermSet
if (termGroup.ServerObjectIsNull == false)
{
string termsetItem = items[1];
string termsetName = termsetItem;
Guid termsetId = Guid.Empty;
if (termsetItem.IndexOf(";#") > -1)
{
termsetName = termsetItem.Split(new string[] { ";#" }, StringSplitOptions.None)[0];
termsetId = new Guid(termsetItem.Split(new string[] { ";#" }, StringSplitOptions.None)[1]);
}
TermSet termSet = null;
if (termsetId != Guid.Empty)
{
termSet = termGroup.TermSets.GetById(termsetId);
}
else
{
termSet = termGroup.TermSets.GetByName(NormalizeName(termsetName));
}
clientContext.Load(termSet);
try
{
clientContext.ExecuteQuery();
}
catch { }
if (termSet.ServerObjectIsNull == null)
{
termsetId = Guid.NewGuid();
termSet = termGroup.CreateTermSet(NormalizeName(termsetName), termsetId, lcid);
clientContext.Load(termSet);
clientContext.ExecuteQuery();
}
if (items.Count() > 2)
{
// Term(s)

if (termSet.ServerObjectIsNull == false)
{
string termItem = items[2];
string termName = termItem;
Guid termId = Guid.Empty;
if (termItem.IndexOf(";#") > -1)
{
termName = termItem.Split(new string[] { ";#" }, StringSplitOptions.None)[0];
termId = new Guid(termItem.Split(new string[] { ";#" }, StringSplitOptions.None)[1]);
}
Term term = null;
if (termId != Guid.Empty)
{
term = termSet.Terms.GetById(termId);
}
else
{
term = termSet.Terms.GetByName(NormalizeName(termName));
}
clientContext.Load(term);
try
{
clientContext.ExecuteQuery();
}
catch { }
if (term.ServerObjectIsNull == null)
{
termId = Guid.NewGuid();
term = termSet.CreateTerm(NormalizeName(termName), lcid, termId);
clientContext.ExecuteQuery();
}
foreach (string line in termLines)
{
// split up
string[] items = line.Split(new string[] { delimiter }, StringSplitOptions.None);
if (items.Count() > 0)
{
string groupItem = items[0];
string groupName = groupItem;
Guid groupId = Guid.Empty;
if (groupItem.IndexOf(";#") > -1)
{
groupName = groupItem.Split(new string[] { ";#" }, StringSplitOptions.None)[0];
groupId = new Guid(groupItem.Split(new string[] { ";#" }, StringSplitOptions.None)[1]);
}
TermGroup termGroup = null;
if (groupId != Guid.Empty)
{
termGroup = termStore.Groups.GetById(groupId);
}
else
{
termGroup = termStore.Groups.GetByName(NormalizeName(groupName));
}
try
{
clientContext.Load(termGroup);
clientContext.ExecuteQuery();
}
catch
{

if (items.Count() > 3)
{
clientContext.Load(term);
clientContext.ExecuteQuery();
if (term.ServerObjectIsNull == false)
{
for (int q = 3; q < items.Count(); q++)
{
termName = items[q];
termId = Guid.Empty;
if (termItem.IndexOf(";#") > -1)
{
termName = termItem.Split(new string[] { ";#" }, StringSplitOptions.None)[0];
termId = new Guid(termItem.Split(new string[] { ";#" }, StringSplitOptions.None)[1]);
}
term = term.AddTermToTerm(lcid, termName, termId);
}
}
}
}
}
}
}
}
}
}
if (termGroup.ServerObjectIsNull == null)
{
termGroup = termStore.CreateGroup(NormalizeName(groupName), groupId);
clientContext.Load(termGroup);
clientContext.ExecuteQuery();
}
if (items.Count() > 1)
{
// TermSet
if (termGroup.ServerObjectIsNull == false)
{
string termsetItem = items[1];
string termsetName = termsetItem;
Guid termsetId = Guid.Empty;
if (termsetItem.IndexOf(";#") > -1)
{
termsetName = termsetItem.Split(new string[] { ";#" }, StringSplitOptions.None)[0];
termsetId = new Guid(termsetItem.Split(new string[] { ";#" }, StringSplitOptions.None)[1]);
}
TermSet termSet = null;
if (termsetId != Guid.Empty)
{
termSet = termGroup.TermSets.GetById(termsetId);
}
else
{
termSet = termGroup.TermSets.GetByName(NormalizeName(termsetName));
}
clientContext.Load(termSet);
try
{
clientContext.ExecuteQuery();
}
catch { }
if (termSet.ServerObjectIsNull == null)
{
termSet = termGroup.CreateTermSet(NormalizeName(termsetName), termsetId, lcid);
clientContext.Load(termSet);
clientContext.ExecuteQuery();
}
if (items.Count() > 2)
{
// Term(s)

if (termSet.ServerObjectIsNull == false)
{
string termItem = items[2];
string termName = termItem;
Guid termId = Guid.Empty;
if (termItem.IndexOf(";#") > -1)
{
termName = termItem.Split(new string[] { ";#" }, StringSplitOptions.None)[0];
termId = new Guid(termItem.Split(new string[] { ";#" }, StringSplitOptions.None)[1]);
}
Term term = null;
if (termId != Guid.Empty)
{
term = termSet.Terms.GetById(termId);
}
else
{
term = termSet.Terms.GetByName(NormalizeName(termName));
}
clientContext.Load(term);
try
{
clientContext.ExecuteQuery();
}
catch { }
if (term.ServerObjectIsNull == null)
{
term = termSet.CreateTerm(NormalizeName(termName), lcid, termId);
clientContext.ExecuteQuery();
}

if (items.Count() > 3)
{
clientContext.Load(term);
clientContext.ExecuteQuery();
if (term.ServerObjectIsNull == false)
{
for (int q = 3; q < items.Count(); q++)
{
termName = items[q];
termId = Guid.Empty;
if (termItem.IndexOf(";#") > -1)
{
termName = termItem.Split(new string[] { ";#" }, StringSplitOptions.None)[0];
termId = new Guid(termItem.Split(new string[] { ";#" }, StringSplitOptions.None)[1]);
}
term = term.AddTermToTerm(lcid, termName, termId);
}
}
}
}
}
}
}
}
}
}

private static Term AddTermToTerm(this Term term, int lcid, string termLabel, Guid termId)
Expand Down

0 comments on commit a42fe94

Please sign in to comment.