Skip to content

Commit

Permalink
Merge pull request #4053 from sankarkumar23/feature/exclude-deprecate…
Browse files Browse the repository at this point in the history
…d-terms

Feature/exclude deprecated terms
  • Loading branch information
KoenZomers authored Oct 11, 2024
2 parents 0cef71d + f9dd94b commit b803fe2
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 17 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
- Added `-Icon` and `-Color` parameters to `Set-PnPList` cmdlet. [#4409](https://github.com/pnp/powershell/pull/4409)
- 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)
- Added `Get-PnPDeletedFlow` cmdlet to retrieve a list of flows which are soft deleted. [#4396](https://github.com/pnp/powershell/pull/4396)
- Added `-ExcludeDeprecated` to `Export-PnpTaxonomy` which allows for deprecated terms to be excluded from the export [#4053](https://github.com/pnp/powershell/pull/4053)

### Changed

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

### Contributors

- San [sankarkumar23]
- Christian Veenhuis [ChVeen]
- Nishkalank Bezawada [NishkalankBezawada]
- Dan Toft [Tanddant]
Expand Down
36 changes: 32 additions & 4 deletions documentation/Export-PnPTaxonomy.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Exports a taxonomy to either the output or to a file.
## SYNTAX

```powershell
Export-PnPTaxonomy [-TermSetId <Guid>] [-IncludeID] [-Path <String>] [-TermStoreName <String>] [-Force]
Export-PnPTaxonomy [-TermSetId <Guid>] [-IncludeID] [-ExcludeDeprecated] [-Path <String>] [-TermStoreName <String>] [-Force]
[-Delimiter <String>] [-Lcid <Int32>] [-Encoding <Encoding>] [-Connection <PnPConnection>]
```
Expand Down Expand Up @@ -142,12 +142,40 @@ Accept pipeline input: False
Accept wildcard characters: False
```
### -ExcludeDeprecated
If specified will exclude the deprecated taxonomy items in the output. Applicable only if you specify TermSetId or TermStoreName.
```yaml
Type: SwitchParameter
Parameter Sets: (All)

Required: False
Position: Named
Default value: None
Accept pipeline input: False
Accept wildcard characters: False
```
### -ExcludeDeprecated
If specified will exclude the deprecated taxonomy items in the output. Applicable only if you specify TermSetId or TermStoreName.
```yaml
Type: SwitchParameter
Parameter Sets: TermSet

Required: False
Position: Named
Default value: None
Accept pipeline input: False
Accept wildcard characters: False
```
### -Lcid
Specify the language code for the exported terms
```yaml
Type: Int32
Parameter Sets: (All)
Parameter Sets: TermSet

Required: False
Position: Named
Expand Down Expand Up @@ -175,7 +203,7 @@ If specified, will export the specified termset only
```yaml
Type: Guid
Parameter Sets: (All)
Parameter Sets: TermSet

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

Required: False
Position: Named
Expand Down
80 changes: 67 additions & 13 deletions src/Commands/Taxonomy/ExportTaxonomy.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
using System;
using System.Collections.Generic;
using System.Management.Automation;
using Microsoft.SharePoint.Client;
using Microsoft.SharePoint.Client;
using Microsoft.SharePoint.Client.Taxonomy;
using PnP.PowerShell.Commands.Enums;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Management.Automation;
using File = System.IO.File;
using Resources = PnP.PowerShell.Commands.Properties.Resources;

Expand All @@ -12,16 +13,21 @@ namespace PnP.PowerShell.Commands.Taxonomy
[Cmdlet(VerbsData.Export, "PnPTaxonomy")]
public class ExportTaxonomy : PnPSharePointCmdlet
{
[Parameter(Mandatory = false, ParameterSetName = "TermSet")]
private const string ParameterSet_TermSet = "TermSet";

[Parameter(Mandatory = false, ParameterSetName = ParameterSet_TermSet)]
public Guid TermSetId;

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

[Parameter(Mandatory = false, ParameterSetName = ParameterSet_TermSet)]
public SwitchParameter ExcludeDeprecated = false;

[Parameter(Mandatory = false)]
public string Path;

[Parameter(Mandatory = false, ParameterSetName = "TermSet")]
[Parameter(Mandatory = false, ParameterSetName = ParameterSet_TermSet)]
public string TermStoreName;

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

[Parameter(Mandatory = false, ParameterSetName = "TermSet")]
[Parameter(Mandatory = false, ParameterSetName = ParameterSet_TermSet)]
public int Lcid = 0;

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


protected override void ExecuteCmdlet()
{
List<string> exportedTerms;
if (ParameterSetName == "TermSet")
if (ParameterSetName == ParameterSet_TermSet)
{
if (Delimiter != "|" && Delimiter == ";#")
{
throw new Exception("Restricted delimiter specified");
}

if (ExcludeDeprecated && Delimiter != "|")
{
throw new PSArgumentException($"{nameof(ExcludeDeprecated)} works only on the default delimiter", nameof(ExcludeDeprecated));
}

if (!string.IsNullOrEmpty(TermStoreName))
{
var taxSession = TaxonomySession.GetTaxonomySession(ClientContext);
var termStore = taxSession.TermStores.GetByName(TermStoreName);
exportedTerms = ClientContext.Site.ExportTermSet(TermSetId, IncludeID, termStore, Delimiter, Lcid);
exportedTerms = ClientContext.Site.ExportTermSet(TermSetId, (IncludeID || ExcludeDeprecated), termStore, Delimiter, Lcid);
}
else
{
exportedTerms = ClientContext.Site.ExportTermSet(TermSetId, IncludeID, Delimiter, Lcid);
exportedTerms = ClientContext.Site.ExportTermSet(TermSetId, (IncludeID || ExcludeDeprecated), Delimiter, Lcid);
}

if (ExcludeDeprecated)
{
exportedTerms = RemoveDeprecatedTerms(exportedTerms);
}
}
else
Expand All @@ -74,7 +90,7 @@ protected override void ExecuteCmdlet()
}

System.Text.Encoding textEncoding = System.Text.Encoding.Unicode;
if(Encoding == Encoding.UTF7)
if (Encoding == Encoding.UTF7)
{
WriteWarning("UTF-7 Encoding is no longer supported. Defaulting to UTF-8");
Encoding = Encoding.UTF8;
Expand Down Expand Up @@ -107,7 +123,6 @@ protected override void ExecuteCmdlet()
textEncoding = System.Text.Encoding.Unicode;
break;
}

}

if (File.Exists(Path))
Expand All @@ -124,5 +139,44 @@ protected override void ExecuteCmdlet()
}
}

private List<string> RemoveDeprecatedTerms(List<string> exportedTerms)
{
var termIds = exportedTerms.Select(t => t.Split(";#").Last().ToGuid());
var taxSession = TaxonomySession.GetTaxonomySession(ClientContext);
if (termIds.Any())
{
//refetch all the terms (500 per call) again just to check the term is deprecated or not
var termGroups = termIds.Select((termId, index) => new { termId, index })
.GroupBy(x => x.index / 500, g => g.termId);
foreach (var termGroup in termGroups)
{
var terms = taxSession.GetTermsById(termGroup.ToArray());
ClientContext.Load(terms);
ClientContext.ExecuteQuery();
var deprecatedTerms = terms.Where(t => t.IsDeprecated);
//remove all deprecated terms
foreach (var deprecatedTerm in deprecatedTerms)
{
var index = exportedTerms.FindIndex(s => s.EndsWith(deprecatedTerm.Id.ToString()));
if (index > -1)
{
exportedTerms.RemoveAt(index);
}
}
}
}

if (!IncludeID)
{
//remove the ids from the term string.
var exportedTermsWithoutId = new List<string>();
foreach (var term in exportedTerms)
{
exportedTermsWithoutId.Add(string.Join(Delimiter, term.Split(Delimiter).Select(t => t.Split(";#").First())));
}
return exportedTermsWithoutId;
}
return exportedTerms;
}
}
}

0 comments on commit b803fe2

Please sign in to comment.