diff --git a/CHANGELOG.md b/CHANGELOG.md index 1b30c0508..60360448e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -31,6 +31,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/). - Fixed `New-PnPAzureADGroup` cmdlet throwing null reference error when owners and members are not specified. [#3308](https://github.com/pnp/powershell/pull/3308) - Fixed `New-PnPTeamsTeam` cmdlet, it will now throw error if it fails to teamify a Microsoft 365 group. [#3310](https://github.com/pnp/powershell/pull/3310) - Fixed `Connect-PnPOnline` cmdlet throwing host not reachable errors. [#3337](https://github.com/pnp/powershell/pull/3337) +- Fixed `Set-PnPTerm` cmdlet throwing object reference error when only the term Id is specified. ### Changed diff --git a/src/Commands/Taxonomy/SetTerm.cs b/src/Commands/Taxonomy/SetTerm.cs index b1aec9c33..4a1691153 100644 --- a/src/Commands/Taxonomy/SetTerm.cs +++ b/src/Commands/Taxonomy/SetTerm.cs @@ -1,6 +1,7 @@ using System; using System.Collections; using System.Globalization; +using System.Linq.Expressions; using System.Management.Automation; using Microsoft.SharePoint.Client; using Microsoft.SharePoint.Client.Taxonomy; @@ -10,9 +11,8 @@ namespace PnP.PowerShell.Commands.Taxonomy { [Cmdlet(VerbsCommon.Set, "PnPTerm")] - public class SetTerm : PnPSharePointCmdlet + public class SetTerm : PnPRetrievalsCmdlet { - private const string ParameterSet_BYID = "By Term Id"; private const string ParameterSet_BYNAME = "By Term Name"; @@ -55,6 +55,8 @@ public class SetTerm : PnPSharePointCmdlet protected override void ExecuteCmdlet() { + DefaultRetrievalExpressions = new Expression>[] { g => g.Name, g => g.TermsCount, g => g.Id }; + Term term; var taxonomySession = TaxonomySession.GetTaxonomySession(ClientContext); // Get Term Store TermStore termStore = null; @@ -68,9 +70,25 @@ protected override void ExecuteCmdlet() } termStore.EnsureProperty(ts => ts.DefaultLanguage); - var termGroup = TermGroup.GetGroup(termStore); - var termSet = TermSet.GetTermSet(termGroup); - var term = Identity.GetTerm(ClientContext, termStore, termSet, false, null); + if (ParameterSetName == ParameterSet_BYID) + { + if (Identity.Id != Guid.Empty) + { + term = termStore.GetTerm(Identity.Id); + ClientContext.Load(term, RetrievalExpressions); + ClientContext.ExecuteQueryRetry(); + } + else + { + throw new PSArgumentException("Insufficient Parameters specified to determine the term to retrieve"); + } + } + else + { + var termGroup = TermGroup.GetGroup(termStore); + var termSet = TermSet.GetTermSet(termGroup); + term = Identity.GetTerm(ClientContext, termStore, termSet, false, null); + } if (ParameterSpecified(nameof(Name))) {