From f1b45d60f831be3f799c2f909b95a6baa471bc71 Mon Sep 17 00:00:00 2001 From: Gautam Sheth Date: Sat, 19 Oct 2024 20:46:32 +0300 Subject: [PATCH] Fix #468: Refactor GetTerm method to properly load child terms and retrieve a specific term by ID or name (#4454) Co-authored-by: Gautam Sheth --- src/Commands/Taxonomy/GetTerm.cs | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/src/Commands/Taxonomy/GetTerm.cs b/src/Commands/Taxonomy/GetTerm.cs index 8d5c0aa36..1f55346a8 100644 --- a/src/Commands/Taxonomy/GetTerm.cs +++ b/src/Commands/Taxonomy/GetTerm.cs @@ -33,7 +33,7 @@ public class GetTerm : PnPRetrievalsCmdlet [Parameter(Mandatory = false, ParameterSetName = ParameterAttribute.AllParameterSets)] public SwitchParameter IncludeChildTerms; - + [Parameter(Mandatory = false, ParameterSetName = ParameterSet_TERMNAME)] public TaxonomyTermPipeBind ParentTerm; @@ -67,7 +67,7 @@ protected override void ExecuteCmdlet() LoadChildTerms(term); } WriteObject(term); - } + } else { throw new PSArgumentException("Insufficient Parameters specified to determine the term to retrieve"); @@ -90,7 +90,21 @@ protected override void ExecuteCmdlet() } else if (Identity != null && ParentTerm != null) { - var term = ParentTerm.GetTerm(ClientContext, termStore, termSet, Recursive, RetrievalExpressions, IncludeDeprecated); + var parentTerm = ParentTerm.GetTerm(ClientContext, termStore, termSet, Recursive, RetrievalExpressions, IncludeDeprecated); + LoadChildTerms(parentTerm); + + Term term = null; + if (Identity.Id != Guid.Empty) + { + term = parentTerm.Terms.GetById(Identity.Id); + } + else + { + term = parentTerm.Terms.GetByName(Identity.Title); + } + + ClientContext.Load(term, RetrievalExpressions); + ClientContext.ExecuteQueryRetry(); if (IncludeChildTerms.IsPresent && term.TermsCount > 0) {