Skip to content

Commit e16551f

Browse files
authored
Merge pull request #3341 from gautamdsheth/bugfix/3338
Fix #3338 - issue with Set-PnPTerm throwing error when only GUID is s…
2 parents b5eae39 + 282784d commit e16551f

File tree

2 files changed

+24
-5
lines changed

2 files changed

+24
-5
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
3131
- Fixed `New-PnPAzureADGroup` cmdlet throwing null reference error when owners and members are not specified. [#3308](https://github.com/pnp/powershell/pull/3308)
3232
- 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)
3333
- Fixed `Connect-PnPOnline` cmdlet throwing host not reachable errors. [#3337](https://github.com/pnp/powershell/pull/3337)
34+
- Fixed `Set-PnPTerm` cmdlet throwing object reference error when only the term Id is specified.
3435

3536
### Changed
3637

src/Commands/Taxonomy/SetTerm.cs

+23-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections;
33
using System.Globalization;
4+
using System.Linq.Expressions;
45
using System.Management.Automation;
56
using Microsoft.SharePoint.Client;
67
using Microsoft.SharePoint.Client.Taxonomy;
@@ -10,9 +11,8 @@
1011
namespace PnP.PowerShell.Commands.Taxonomy
1112
{
1213
[Cmdlet(VerbsCommon.Set, "PnPTerm")]
13-
public class SetTerm : PnPSharePointCmdlet
14+
public class SetTerm : PnPRetrievalsCmdlet<Term>
1415
{
15-
1616
private const string ParameterSet_BYID = "By Term Id";
1717
private const string ParameterSet_BYNAME = "By Term Name";
1818

@@ -55,6 +55,8 @@ public class SetTerm : PnPSharePointCmdlet
5555

5656
protected override void ExecuteCmdlet()
5757
{
58+
DefaultRetrievalExpressions = new Expression<Func<Term, object>>[] { g => g.Name, g => g.TermsCount, g => g.Id };
59+
Term term;
5860
var taxonomySession = TaxonomySession.GetTaxonomySession(ClientContext);
5961
// Get Term Store
6062
TermStore termStore = null;
@@ -68,9 +70,25 @@ protected override void ExecuteCmdlet()
6870
}
6971
termStore.EnsureProperty(ts => ts.DefaultLanguage);
7072

71-
var termGroup = TermGroup.GetGroup(termStore);
72-
var termSet = TermSet.GetTermSet(termGroup);
73-
var term = Identity.GetTerm(ClientContext, termStore, termSet, false, null);
73+
if (ParameterSetName == ParameterSet_BYID)
74+
{
75+
if (Identity.Id != Guid.Empty)
76+
{
77+
term = termStore.GetTerm(Identity.Id);
78+
ClientContext.Load(term, RetrievalExpressions);
79+
ClientContext.ExecuteQueryRetry();
80+
}
81+
else
82+
{
83+
throw new PSArgumentException("Insufficient Parameters specified to determine the term to retrieve");
84+
}
85+
}
86+
else
87+
{
88+
var termGroup = TermGroup.GetGroup(termStore);
89+
var termSet = TermSet.GetTermSet(termGroup);
90+
term = Identity.GetTerm(ClientContext, termStore, termSet, false, null);
91+
}
7492

7593
if (ParameterSpecified(nameof(Name)))
7694
{

0 commit comments

Comments
 (0)