Skip to content

Commit b803fe2

Browse files
authored
Merge pull request #4053 from sankarkumar23/feature/exclude-deprecated-terms
Feature/exclude deprecated terms
2 parents 0cef71d + f9dd94b commit b803fe2

File tree

3 files changed

+101
-17
lines changed

3 files changed

+101
-17
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
2727
- Added `-Icon` and `-Color` parameters to `Set-PnPList` cmdlet. [#4409](https://github.com/pnp/powershell/pull/4409)
2828
- Added `Remove-PnPTenantRestrictedSearchAllowedList` cmdlet to removes site URLs from the allowed list when Restricted SharePoint Search is enabled. [#4399](https://github.com/pnp/powershell/pull/4399)
2929
- Added `Get-PnPDeletedFlow` cmdlet to retrieve a list of flows which are soft deleted. [#4396](https://github.com/pnp/powershell/pull/4396)
30+
- Added `-ExcludeDeprecated` to `Export-PnpTaxonomy` which allows for deprecated terms to be excluded from the export [#4053](https://github.com/pnp/powershell/pull/4053)
3031

3132
### Changed
3233

@@ -67,6 +68,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
6768

6869
### Contributors
6970

71+
- San [sankarkumar23]
7072
- Christian Veenhuis [ChVeen]
7173
- Nishkalank Bezawada [NishkalankBezawada]
7274
- Dan Toft [Tanddant]

documentation/Export-PnPTaxonomy.md

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ Exports a taxonomy to either the output or to a file.
1515
## SYNTAX
1616

1717
```powershell
18-
Export-PnPTaxonomy [-TermSetId <Guid>] [-IncludeID] [-Path <String>] [-TermStoreName <String>] [-Force]
18+
Export-PnPTaxonomy [-TermSetId <Guid>] [-IncludeID] [-ExcludeDeprecated] [-Path <String>] [-TermStoreName <String>] [-Force]
1919
[-Delimiter <String>] [-Lcid <Int32>] [-Encoding <Encoding>] [-Connection <PnPConnection>]
2020
2121
```
@@ -142,12 +142,40 @@ Accept pipeline input: False
142142
Accept wildcard characters: False
143143
```
144144
145+
### -ExcludeDeprecated
146+
If specified will exclude the deprecated taxonomy items in the output. Applicable only if you specify TermSetId or TermStoreName.
147+
148+
```yaml
149+
Type: SwitchParameter
150+
Parameter Sets: (All)
151+
152+
Required: False
153+
Position: Named
154+
Default value: None
155+
Accept pipeline input: False
156+
Accept wildcard characters: False
157+
```
158+
159+
### -ExcludeDeprecated
160+
If specified will exclude the deprecated taxonomy items in the output. Applicable only if you specify TermSetId or TermStoreName.
161+
162+
```yaml
163+
Type: SwitchParameter
164+
Parameter Sets: TermSet
165+
166+
Required: False
167+
Position: Named
168+
Default value: None
169+
Accept pipeline input: False
170+
Accept wildcard characters: False
171+
```
172+
145173
### -Lcid
146174
Specify the language code for the exported terms
147175
148176
```yaml
149177
Type: Int32
150-
Parameter Sets: (All)
178+
Parameter Sets: TermSet
151179

152180
Required: False
153181
Position: Named
@@ -175,7 +203,7 @@ If specified, will export the specified termset only
175203
176204
```yaml
177205
Type: Guid
178-
Parameter Sets: (All)
206+
Parameter Sets: TermSet
179207

180208
Required: False
181209
Position: Named
@@ -189,7 +217,7 @@ Term store to export; if not specified the default term store is used.
189217
190218
```yaml
191219
Type: String
192-
Parameter Sets: (All)
220+
Parameter Sets: TermSet
193221

194222
Required: False
195223
Position: Named

src/Commands/Taxonomy/ExportTaxonomy.cs

Lines changed: 67 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Management.Automation;
4-
using Microsoft.SharePoint.Client;
1+
using Microsoft.SharePoint.Client;
52
using Microsoft.SharePoint.Client.Taxonomy;
63
using PnP.PowerShell.Commands.Enums;
4+
using System;
5+
using System.Collections.Generic;
6+
using System.Linq;
7+
using System.Management.Automation;
78
using File = System.IO.File;
89
using Resources = PnP.PowerShell.Commands.Properties.Resources;
910

@@ -12,16 +13,21 @@ namespace PnP.PowerShell.Commands.Taxonomy
1213
[Cmdlet(VerbsData.Export, "PnPTaxonomy")]
1314
public class ExportTaxonomy : PnPSharePointCmdlet
1415
{
15-
[Parameter(Mandatory = false, ParameterSetName = "TermSet")]
16+
private const string ParameterSet_TermSet = "TermSet";
17+
18+
[Parameter(Mandatory = false, ParameterSetName = ParameterSet_TermSet)]
1619
public Guid TermSetId;
1720

1821
[Parameter(Mandatory = false)]
1922
public SwitchParameter IncludeID = false;
2023

24+
[Parameter(Mandatory = false, ParameterSetName = ParameterSet_TermSet)]
25+
public SwitchParameter ExcludeDeprecated = false;
26+
2127
[Parameter(Mandatory = false)]
2228
public string Path;
2329

24-
[Parameter(Mandatory = false, ParameterSetName = "TermSet")]
30+
[Parameter(Mandatory = false, ParameterSetName = ParameterSet_TermSet)]
2531
public string TermStoreName;
2632

2733
[Parameter(Mandatory = false)]
@@ -30,31 +36,41 @@ public class ExportTaxonomy : PnPSharePointCmdlet
3036
[Parameter(Mandatory = false)]
3137
public string Delimiter = "|";
3238

33-
[Parameter(Mandatory = false, ParameterSetName = "TermSet")]
39+
[Parameter(Mandatory = false, ParameterSetName = ParameterSet_TermSet)]
3440
public int Lcid = 0;
3541

3642
[Parameter(Mandatory = false)]
3743
public Encoding Encoding = Encoding.Unicode;
3844

39-
4045
protected override void ExecuteCmdlet()
4146
{
4247
List<string> exportedTerms;
43-
if (ParameterSetName == "TermSet")
48+
if (ParameterSetName == ParameterSet_TermSet)
4449
{
4550
if (Delimiter != "|" && Delimiter == ";#")
4651
{
4752
throw new Exception("Restricted delimiter specified");
4853
}
54+
55+
if (ExcludeDeprecated && Delimiter != "|")
56+
{
57+
throw new PSArgumentException($"{nameof(ExcludeDeprecated)} works only on the default delimiter", nameof(ExcludeDeprecated));
58+
}
59+
4960
if (!string.IsNullOrEmpty(TermStoreName))
5061
{
5162
var taxSession = TaxonomySession.GetTaxonomySession(ClientContext);
5263
var termStore = taxSession.TermStores.GetByName(TermStoreName);
53-
exportedTerms = ClientContext.Site.ExportTermSet(TermSetId, IncludeID, termStore, Delimiter, Lcid);
64+
exportedTerms = ClientContext.Site.ExportTermSet(TermSetId, (IncludeID || ExcludeDeprecated), termStore, Delimiter, Lcid);
5465
}
5566
else
5667
{
57-
exportedTerms = ClientContext.Site.ExportTermSet(TermSetId, IncludeID, Delimiter, Lcid);
68+
exportedTerms = ClientContext.Site.ExportTermSet(TermSetId, (IncludeID || ExcludeDeprecated), Delimiter, Lcid);
69+
}
70+
71+
if (ExcludeDeprecated)
72+
{
73+
exportedTerms = RemoveDeprecatedTerms(exportedTerms);
5874
}
5975
}
6076
else
@@ -74,7 +90,7 @@ protected override void ExecuteCmdlet()
7490
}
7591

7692
System.Text.Encoding textEncoding = System.Text.Encoding.Unicode;
77-
if(Encoding == Encoding.UTF7)
93+
if (Encoding == Encoding.UTF7)
7894
{
7995
WriteWarning("UTF-7 Encoding is no longer supported. Defaulting to UTF-8");
8096
Encoding = Encoding.UTF8;
@@ -107,7 +123,6 @@ protected override void ExecuteCmdlet()
107123
textEncoding = System.Text.Encoding.Unicode;
108124
break;
109125
}
110-
111126
}
112127

113128
if (File.Exists(Path))
@@ -124,5 +139,44 @@ protected override void ExecuteCmdlet()
124139
}
125140
}
126141

142+
private List<string> RemoveDeprecatedTerms(List<string> exportedTerms)
143+
{
144+
var termIds = exportedTerms.Select(t => t.Split(";#").Last().ToGuid());
145+
var taxSession = TaxonomySession.GetTaxonomySession(ClientContext);
146+
if (termIds.Any())
147+
{
148+
//refetch all the terms (500 per call) again just to check the term is deprecated or not
149+
var termGroups = termIds.Select((termId, index) => new { termId, index })
150+
.GroupBy(x => x.index / 500, g => g.termId);
151+
foreach (var termGroup in termGroups)
152+
{
153+
var terms = taxSession.GetTermsById(termGroup.ToArray());
154+
ClientContext.Load(terms);
155+
ClientContext.ExecuteQuery();
156+
var deprecatedTerms = terms.Where(t => t.IsDeprecated);
157+
//remove all deprecated terms
158+
foreach (var deprecatedTerm in deprecatedTerms)
159+
{
160+
var index = exportedTerms.FindIndex(s => s.EndsWith(deprecatedTerm.Id.ToString()));
161+
if (index > -1)
162+
{
163+
exportedTerms.RemoveAt(index);
164+
}
165+
}
166+
}
167+
}
168+
169+
if (!IncludeID)
170+
{
171+
//remove the ids from the term string.
172+
var exportedTermsWithoutId = new List<string>();
173+
foreach (var term in exportedTerms)
174+
{
175+
exportedTermsWithoutId.Add(string.Join(Delimiter, term.Split(Delimiter).Select(t => t.Split(";#").First())));
176+
}
177+
return exportedTermsWithoutId;
178+
}
179+
return exportedTerms;
180+
}
127181
}
128182
}

0 commit comments

Comments
 (0)