Skip to content

Commit

Permalink
Fix GetTerm method to throw proper error message, use retry in export…
Browse files Browse the repository at this point in the history
…-taxonomy (#4451)

Co-authored-by: Gautam Sheth <[email protected]>
  • Loading branch information
gautamdsheth and Gautam Sheth authored Oct 18, 2024
1 parent 9a83b8e commit 1f7591f
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 46 deletions.
101 changes: 56 additions & 45 deletions src/Commands/Base/PipeBinds/TaxonomyTermPipeBind.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using System;
using Microsoft.SharePoint.Client;
using Microsoft.SharePoint.Client.Taxonomy;
using System;
using System.Linq;
using System.Linq.Expressions;
using System.Management.Automation;
using Microsoft.SharePoint.Client;
using Microsoft.SharePoint.Client.Taxonomy;


namespace PnP.PowerShell.Commands.Base.PipeBinds
Expand Down Expand Up @@ -41,65 +41,76 @@ public TaxonomyTermPipeBind(Term item)

public Term GetTerm(ClientContext clientContext, TermStore termStore, TermSet termSet, bool recursive, Expression<Func<Term, object>>[] expressions = null, bool includeDeprecated = false)
{
Term term = null;
if (_id != Guid.Empty)
{
term = termStore.GetTerm(_id);
}
else if (!string.IsNullOrEmpty(_title) && termSet != null && termStore != null)
try
{
var termName = TaxonomyExtensions.NormalizeName(_title);
if (!recursive)
Term term = null;
if (_id != Guid.Empty)
{
term = termSet.Terms.GetByName(termName);
term = termStore.GetTerm(_id);
}
else
else if (!string.IsNullOrEmpty(_title) && termSet != null && termStore != null)
{
if (includeDeprecated)
var termName = TaxonomyExtensions.NormalizeName(_title);
if (!recursive)
{
var allTerms = termSet.GetAllTermsIncludeDeprecated();
clientContext.Load(allTerms);
clientContext.ExecuteQueryRetry();

if (allTerms.AreItemsAvailable)
{
term = allTerms.Where(t => t.Name == termName).FirstOrDefault();
}
term = termSet.Terms.GetByName(termName);
}
else
{
var lmi = new LabelMatchInformation(clientContext)
if (includeDeprecated)
{
TrimUnavailable = true,
TermLabel = termName
};

var termMatches = termSet.GetTerms(lmi);
clientContext.Load(termMatches);
clientContext.ExecuteQueryRetry();
var allTerms = termSet.GetAllTermsIncludeDeprecated();
clientContext.Load(allTerms);
clientContext.ExecuteQueryRetry();

if (termMatches.AreItemsAvailable)
if (allTerms.AreItemsAvailable)
{
term = allTerms.Where(t => t.Name == termName).FirstOrDefault();
}
}
else
{
term = termMatches.FirstOrDefault();
var lmi = new LabelMatchInformation(clientContext)
{
TrimUnavailable = true,
TermLabel = termName
};

var termMatches = termSet.GetTerms(lmi);
clientContext.Load(termMatches);
clientContext.ExecuteQueryRetry();

if (termMatches.AreItemsAvailable)
{
term = termMatches.FirstOrDefault();
}
}
}

}
}
else
{
throw new PSArgumentException("Not enough parameters specified to succesfully find the term");
}
if (expressions != null)
{
clientContext.Load(term, expressions);
}
else
{
clientContext.Load(term);
}
clientContext.ExecuteQueryRetry();
return term;
}
else
{
throw new PSArgumentException("Not enough parameters specified to succesfully find the term");
}
if (expressions != null)
{
clientContext.Load(term, expressions);
}
else
catch (ServerException e)
{
clientContext.Load(term);
if (e.ServerErrorTypeName == "System.ArgumentOutOfRangeException")
{
throw new PSArgumentException("The specified term does not exist");
}
throw;
}
clientContext.ExecuteQueryRetry();
return term;
}

public TaxonomyTermPipeBind()
Expand Down
2 changes: 1 addition & 1 deletion src/Commands/Taxonomy/ExportTaxonomy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ private List<string> RemoveDeprecatedTerms(List<string> exportedTerms)
{
var terms = taxSession.GetTermsById(termGroup.ToArray());
ClientContext.Load(terms);
ClientContext.ExecuteQuery();
ClientContext.ExecuteQueryRetry();
var deprecatedTerms = terms.Where(t => t.IsDeprecated);
//remove all deprecated terms
foreach (var deprecatedTerm in deprecatedTerms)
Expand Down

0 comments on commit 1f7591f

Please sign in to comment.