Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement updating terms for Import-PnPTermGroupFromXml commandlet #1077

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -177,14 +177,18 @@ internal static List<ReusedTerm> ProcessGroup(ClientContext context, TaxonomySes
}
else
{
// todo: add handling for reused term?
modelTerm.Id = term.Id;
if (modelTermGroup.UpdateBehavior == Model.TermGroupUpdateBehavior.Overwrite)
{
UpdateTerm(modelTerm, term, parser, termStore, context, scope);
}
}
}
else
{
// todo: add handling for reused term?
modelTerm.Id = term.Id;
if (modelTermGroup.UpdateBehavior == Model.TermGroupUpdateBehavior.Overwrite)
{
UpdateTerm(modelTerm, term, parser, termStore, context, scope);
}
}

if (term != null)
Expand Down Expand Up @@ -351,6 +355,79 @@ internal static Tuple<Guid, TokenParser, List<ReusedTerm>> CreateTerm(ClientCont
return Tuple.Create(modelTerm.Id, parser, reusedTerms);
}

internal static Model.Term UpdateTerm(Model.Term modelTerm, Term term, TokenParser parser, TermStore termStore, ClientContext context, PnPMonitoredScope scope)
{
if (modelTerm.Labels.Any())
{
RemoveLabelsFromTerm(term, termStore, context);

context.Load(term.Labels);
context.Load(term);
context.ExecuteQueryRetry();
termStore.CommitAll();

foreach (var modelLabel in modelTerm.Labels)
{
if (((modelLabel.IsDefaultForLanguage && modelLabel.Language != termStore.DefaultLanguage) || modelLabel.IsDefaultForLanguage == false) && termStore.Languages.Contains(modelLabel.Language))
{
term.CreateLabel(parser.ParseString(modelLabel.Value), modelLabel.Language, modelLabel.IsDefaultForLanguage);
}
else
{
scope.LogWarning(CoreResources.Provisioning_ObjectHandlers_TermGroups_Skipping_label__0___label_is_to_set_to_default_for_language__1__while_the_default_termstore_language_is_also__1_, modelLabel.Value, modelLabel.Language);
}
}
}

context.Load(term);
context.ExecuteQueryRetry();
termStore.CommitAll();

term.Name = modelTerm.Name;
term.Owner = modelTerm.Owner;
term.SetDescription(parser.ParseString(modelTerm.Description), modelTerm.Language != null && modelTerm.Language != 0 ? modelTerm.Language.Value : termStore.DefaultLanguage);
term.IsAvailableForTagging = modelTerm.IsAvailableForTagging;

context.Load(term);
context.ExecuteQueryRetry();
termStore.CommitAll();

modelTerm.Id = term.Id;

return modelTerm;
}

private static void RemoveLabelsFromTerm(Term term, TermStore termStore, ClientContext context)
{
context.Load(term.Labels);
// don't change this to async! Managed Metadata Service can't keep up and CSOM will actually not load anything.
context.ExecuteQueryRetry();

var existingLanguages = term.Labels.Select(x => x.Language).Distinct().ToList();

foreach (var language in existingLanguages)
{
var labelsInLanguage = term.Labels.Where(x => x.Language == language);
foreach (var nonDefaultLabel in labelsInLanguage.Where(x => x.IsDefaultForLanguage == false))
{
nonDefaultLabel.DeleteObject();
}

var defaultLabel = labelsInLanguage.Where(x => x.IsDefaultForLanguage == true).Single();

// Check if the current label is the default label for default language - that can not be deleted. All others are free game!
if (termStore.DefaultLanguage != defaultLabel.Language)
{
defaultLabel.DeleteObject();
}

// Trying to perform multiple deletions in a batch will cause conflicts that one needs to handle so it's easier to just give up and commit changes once per label change
context.Load(term.Labels);
context.ExecuteQueryRetry();
}

termStore.CommitAll();
}

private static void CreateTermLabels(Model.Term modelTerm, TermStore termStore, TokenParser parser, PnPMonitoredScope scope, Term term)
{
Expand Down Expand Up @@ -629,7 +706,7 @@ private static bool CheckUser(ClientContext context, string loginName)
return true;
}
}
catch(ServerException ex)
catch (ServerException ex)
{
if (ex.ServerErrorCode == -2146232832 && ex.ServerErrorTypeName.Equals("Microsoft.SharePoint.SPException", StringComparison.InvariantCultureIgnoreCase))
{
Expand Down
Loading