Skip to content

Fix #3338 - issue with Set-PnPTerm throwing error when only GUID is s… #3341

New issue

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

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

Already on GitHub? # to your account

Merged
merged 1 commit into from
Aug 9, 2023
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
28 changes: 23 additions & 5 deletions src/Commands/Taxonomy/SetTerm.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -10,9 +11,8 @@
namespace PnP.PowerShell.Commands.Taxonomy
{
[Cmdlet(VerbsCommon.Set, "PnPTerm")]
public class SetTerm : PnPSharePointCmdlet
public class SetTerm : PnPRetrievalsCmdlet<Term>
{

private const string ParameterSet_BYID = "By Term Id";
private const string ParameterSet_BYNAME = "By Term Name";

Expand Down Expand Up @@ -55,6 +55,8 @@ public class SetTerm : PnPSharePointCmdlet

protected override void ExecuteCmdlet()
{
DefaultRetrievalExpressions = new Expression<Func<Term, object>>[] { g => g.Name, g => g.TermsCount, g => g.Id };
Term term;
var taxonomySession = TaxonomySession.GetTaxonomySession(ClientContext);
// Get Term Store
TermStore termStore = null;
Expand All @@ -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)))
{
Expand Down