From adfa31d4bd63dbb87494fe55dfa70b2d3f1deb95 Mon Sep 17 00:00:00 2001 From: reshmee011 Date: Sat, 1 Jun 2024 20:34:08 +0100 Subject: [PATCH 1/4] new cmdlet for Add-PnPTenantRestrictedSearchAllowedList --- ...dd-PnPTenantRestrictedSearchAllowedList.md | 108 ++++++++++++++++++ .../AddTenantRestrictedSearchAllowedList.cs | 85 ++++++++++++++ 2 files changed, 193 insertions(+) create mode 100644 documentation/Add-PnPTenantRestrictedSearchAllowedList.md create mode 100644 src/Commands/Admin/AddTenantRestrictedSearchAllowedList.cs diff --git a/documentation/Add-PnPTenantRestrictedSearchAllowedList.md b/documentation/Add-PnPTenantRestrictedSearchAllowedList.md new file mode 100644 index 000000000..dfe59c23b --- /dev/null +++ b/documentation/Add-PnPTenantRestrictedSearchAllowedList.md @@ -0,0 +1,108 @@ +--- +Module Name: PnP.PowerShell +schema: 2.0.0 +applicable: SharePoint Online +online version: https://pnp.github.io/powershell/cmdlets/Add-PnPTenantRestrictedSearchAllowedList.html +external help file: PnP.PowerShell.dll-Help.xml +title: Add-PnPTenantRestrictedSearchAllowedList +--- + +# Add-PnPTenantRestrictedSearchAllowedList + +## SYNOPSIS +Adds site URLs to the allowed list when Restricted SharePoint Search is enabled. The URLs can be provided as a string array or read from a CSV file. + +## SYNTAX + +```powershell +Add-PnPTenantRestrictedSearchAllowedList [-SitesListFileUrl ] [-SiteList ] [-ContainsHeaders ] [-Connection ] +``` + +## DESCRIPTION + +Adds site URLs to the allowed list when Restricted SharePoint Search is enabled. The URLs can be provided directly as a string array or read from a CSV file. + +## EXAMPLES + +### EXAMPLE 1 +```powershell +Add-PnPTenantRestrictedSearchAllowedList -SitesListFileUrl "C:\temp\sitelist.csv" -ContainsHeader +``` + +Adds site URLs to the allowed list from a CSV file. The first line, which is assumed to be a header, is skipped. + +### EXAMPLE 2 +```powershell +Add-PnPTenantRestrictedSearchAllowedList -SitesListFileUrl "C:\temp\sitelist.csv" +``` + +Adds site URLs to the allowed list from a CSV file. + +### EXAMPLE 3 +```powershell +Add-PnPTenantRestrictedSearchAllowedList @("https://contoso.sharepoint.com/sites/Company311","https://contoso.sharepoint.com/sites/contosoportal") +``` +Adds the specified sites to the allowed list. + +## PARAMETERS + +### -Connection +Optional connection to be used by the cmdlet. Retrieve the value for this parameter by either specifying -ReturnConnection on Connect-PnPOnline or by executing Get-PnPConnection. + +```yaml +Type: PnPConnection +Parameter Sets: (All) + +Required: False +Position: Named +Default value: None +Accept pipeline input: False +Accept wildcard characters: False +``` + +### -SitesListFileUrl +Specifies the path of the CSV file that contains a list of site URLs to be added to the allowed list when the tenant is set to Restricted Tenant Search Mode. + +```yaml +Type: String +Parameter Sets: (All) + +Required: False +Position: Named +Default value: None +Accept pipeline input: False +Accept wildcard characters: False +``` + +### -SitesList +Specifies a collection of sites to add to the allowed list. + +```yaml +Type: String[] +Parameter Sets: (All) + +Required: True +Position: Named +Default value: None +Accept pipeline input: False +Accept wildcard characters: False +``` + +### -Containsheader + +If specified, this switch skips the first line from the CSV file, which is assumed to be a header. + +```yaml +Type: SwitchParamter +Parameter Sets: File + +Required: False +Position: Named +Default value: None +Accept pipeline input: False +Accept wildcard characters: False +``` + +## RELATED LINKS + +[Microsoft 365 Patterns and Practices](https://aka.ms/m365pnp) diff --git a/src/Commands/Admin/AddTenantRestrictedSearchAllowedList.cs b/src/Commands/Admin/AddTenantRestrictedSearchAllowedList.cs new file mode 100644 index 000000000..8a84eb619 --- /dev/null +++ b/src/Commands/Admin/AddTenantRestrictedSearchAllowedList.cs @@ -0,0 +1,85 @@ +using Microsoft.Online.SharePoint.TenantAdministration; +using Microsoft.SharePoint.Client; +using PnP.PowerShell.Commands.Base; +using System.Collections.Generic; +using System.Management.Automation; +using System.Linq; +using System; + +namespace PnP.PowerShell.Commands.Files +{ + [Cmdlet(VerbsCommon.Add, "PnPTenantRestrictedSearchAllowedList")] + public class AddTenantRestrictedSearchAllowedList : PnPAdminCmdlet,IDynamicParameters + { + private const string ParameterSet_file = "file"; + private FileParameters _fileParameters; + + [Parameter(Mandatory = false)] + public string[] SiteList; + + [Parameter(Mandatory = false)] + public string SitesListFileUrl; + public object GetDynamicParameters() + { + if (!ParameterSpecified(nameof(SiteList)) && !ParameterSpecified(nameof(SitesListFileUrl))) + { + throw new ArgumentException("Parameter set cannot be resolved using the specified named parameters."); + } + else if (ParameterSpecified(nameof(SitesListFileUrl))) + { + _fileParameters = new FileParameters(); + return _fileParameters; + } + + return null; + } + protected override void ExecuteCmdlet() + { + IList _sitelist = null; + if (_fileParameters != null) + { + _sitelist = ReadFileContents(); + } + else if (SiteList != null) + { + _sitelist = SiteList; + } + else + { + throw new ArgumentException("Parameter set cannot be resolved using the specified named parameters."); + } + if (_sitelist == null) + { + throw new InvalidOperationException("SiteList cannot be null"); + } + + Tenant.AddSPORestrictedSearchAllowedList(_sitelist); + AdminContext.ExecuteQueryRetry(); + } + + public IList ReadFileContents() + { + var lines = System.IO.File.ReadAllLines(SitesListFileUrl); + if (_fileParameters.ContainsHeader) + { + lines = lines.Skip(1).ToArray(); + } + + foreach (var line in lines) + { + var columns = line.Split(','); + if (columns.Length != 1) + { + throw new InvalidOperationException("File should only contain one column"); + } + } + + return lines.ToList(); + } + public class FileParameters + { + [Parameter(Mandatory = false,ParameterSetName = ParameterSet_file)] + public SwitchParameter ContainsHeader; + } + } +} From 3cf58e335020a629d10021aa44aa8404793fa81d Mon Sep 17 00:00:00 2001 From: reshmee011 Date: Tue, 10 Sep 2024 06:57:07 +0100 Subject: [PATCH 2/4] Update to parameter set instead of dynmaic set --- ...dd-PnPTenantRestrictedSearchAllowedList.md | 4 +- .../AddTenantRestrictedSearchAllowedList.cs | 52 +++++++------------ 2 files changed, 21 insertions(+), 35 deletions(-) diff --git a/documentation/Add-PnPTenantRestrictedSearchAllowedList.md b/documentation/Add-PnPTenantRestrictedSearchAllowedList.md index dfe59c23b..f40789dbb 100644 --- a/documentation/Add-PnPTenantRestrictedSearchAllowedList.md +++ b/documentation/Add-PnPTenantRestrictedSearchAllowedList.md @@ -15,7 +15,7 @@ Adds site URLs to the allowed list when Restricted SharePoint Search is enabled. ## SYNTAX ```powershell -Add-PnPTenantRestrictedSearchAllowedList [-SitesListFileUrl ] [-SiteList ] [-ContainsHeaders ] [-Connection ] +Add-PnPTenantRestrictedSearchAllowedList [-SitesListFileUrl ] [-SitesList ] [-ContainsHeaders ] [-Connection ] ``` ## DESCRIPTION @@ -40,7 +40,7 @@ Adds site URLs to the allowed list from a CSV file. ### EXAMPLE 3 ```powershell -Add-PnPTenantRestrictedSearchAllowedList @("https://contoso.sharepoint.com/sites/Company311","https://contoso.sharepoint.com/sites/contosoportal") +Add-PnPTenantRestrictedSearchAllowedList -SitesList @("https://contoso.sharepoint.com/sites/Company311","https://contoso.sharepoint.com/sites/contosoportal") ``` Adds the specified sites to the allowed list. diff --git a/src/Commands/Admin/AddTenantRestrictedSearchAllowedList.cs b/src/Commands/Admin/AddTenantRestrictedSearchAllowedList.cs index 8a84eb619..2db07a6b3 100644 --- a/src/Commands/Admin/AddTenantRestrictedSearchAllowedList.cs +++ b/src/Commands/Admin/AddTenantRestrictedSearchAllowedList.cs @@ -8,46 +8,37 @@ namespace PnP.PowerShell.Commands.Files { - [Cmdlet(VerbsCommon.Add, "PnPTenantRestrictedSearchAllowedList")] - public class AddTenantRestrictedSearchAllowedList : PnPAdminCmdlet,IDynamicParameters + [Cmdlet(VerbsCommon.Add, "PnPTenantRestrictedSearchAllowedList", DefaultParameterSetName = ParameterSet_SiteList)] + public class AddTenantRestrictedSearchAllowedList : PnPAdminCmdlet { - private const string ParameterSet_file = "file"; - private FileParameters _fileParameters; + private const string ParameterSet_SiteList = "SiteList"; + private const string ParameterSet_File = "File"; - [Parameter(Mandatory = false)] - public string[] SiteList; + [Parameter(Mandatory = false, ParameterSetName = ParameterSet_SiteList)] + public string[] SitesList; - [Parameter(Mandatory = false)] + [Parameter(Mandatory = false, ParameterSetName = ParameterSet_File)] public string SitesListFileUrl; - public object GetDynamicParameters() - { - if (!ParameterSpecified(nameof(SiteList)) && !ParameterSpecified(nameof(SitesListFileUrl))) - { - throw new ArgumentException("Parameter set cannot be resolved using the specified named parameters."); - } - else if (ParameterSpecified(nameof(SitesListFileUrl))) - { - _fileParameters = new FileParameters(); - return _fileParameters; - } - - return null; - } + + [Parameter(Mandatory = false, ParameterSetName = ParameterSet_File)] + public SwitchParameter ContainsHeader; + protected override void ExecuteCmdlet() { IList _sitelist = null; - if (_fileParameters != null) + if (ParameterSetName == ParameterSet_File) { _sitelist = ReadFileContents(); } - else if (SiteList != null) + else if (ParameterSetName == ParameterSet_SiteList) { - _sitelist = SiteList; + _sitelist = SitesList; } else { throw new ArgumentException("Parameter set cannot be resolved using the specified named parameters."); } + if (_sitelist == null) { throw new InvalidOperationException("SiteList cannot be null"); @@ -57,10 +48,10 @@ protected override void ExecuteCmdlet() AdminContext.ExecuteQueryRetry(); } - public IList ReadFileContents() - { + private IList ReadFileContents() + { var lines = System.IO.File.ReadAllLines(SitesListFileUrl); - if (_fileParameters.ContainsHeader) + if (ContainsHeader) { lines = lines.Skip(1).ToArray(); } @@ -76,10 +67,5 @@ public IList ReadFileContents() return lines.ToList(); } - public class FileParameters - { - [Parameter(Mandatory = false,ParameterSetName = ParameterSet_file)] - public SwitchParameter ContainsHeader; - } } -} +} \ No newline at end of file From 732908adce78059c19a0c2aa135250f05c51ff07 Mon Sep 17 00:00:00 2001 From: Koen Zomers Date: Tue, 10 Sep 2024 14:19:28 +0200 Subject: [PATCH 3/4] Adding changelog entry --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 85ae8e11b..445b6e44a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/). - Added `Reset-PnPDocumentID` cmdlet to request resetting the document ID for a document [#4238](https://github.com/pnp/powershell/pull/4238) - Added `Get-PnPPriviledgedIdentityManagementEligibleAssignment`, `Get-PnPPriviledgedIdentityManagementRole` and `Enable-PnPPriviledgedIdentityManagement` cmdlets to allow scripting of enabling Privileged Identity Management roles for a user [#4039](https://github.com/pnp/powershell/pull/4039) +- Added `add-PnPTenantRestrictedSearchAllowedList` which allows setting up a list of allowed URLs for Restricted SharePoint Search [#3993](https://github.com/pnp/powershell/pull/3993) ### Changed @@ -24,6 +25,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/). ### Contributors +- Reshmee Auckloo [reshmee011] - Koen Zomers [koenzomers] ## [2.12.0] From 38737920950a4befb48f2da8ec909c1844b0853f Mon Sep 17 00:00:00 2001 From: Koen Zomers Date: Tue, 10 Sep 2024 14:19:34 +0200 Subject: [PATCH 4/4] Cleanup and minor adjustments --- .../Add-PnPTenantRestrictedSearchAllowedList.md | 11 ++++++----- .../AddTenantRestrictedSearchAllowedList.cs | 16 ++++++++++------ 2 files changed, 16 insertions(+), 11 deletions(-) diff --git a/documentation/Add-PnPTenantRestrictedSearchAllowedList.md b/documentation/Add-PnPTenantRestrictedSearchAllowedList.md index f40789dbb..1e68222d3 100644 --- a/documentation/Add-PnPTenantRestrictedSearchAllowedList.md +++ b/documentation/Add-PnPTenantRestrictedSearchAllowedList.md @@ -20,7 +20,7 @@ Add-PnPTenantRestrictedSearchAllowedList [-SitesListFileUrl ] [-SitesLis ## DESCRIPTION -Adds site URLs to the allowed list when Restricted SharePoint Search is enabled. The URLs can be provided directly as a string array or read from a CSV file. +Adds site URLs to the allowed list when Restricted SharePoint Search is enabled. The URLs can be provided directly as a string array or read from a CSV file. At present, a maximum of 100 sites can be added to the allowed list. ## EXAMPLES @@ -65,9 +65,9 @@ Specifies the path of the CSV file that contains a list of site URLs to be added ```yaml Type: String -Parameter Sets: (All) +Parameter Sets: File -Required: False +Required: True Position: Named Default value: None Accept pipeline input: False @@ -79,7 +79,7 @@ Specifies a collection of sites to add to the allowed list. ```yaml Type: String[] -Parameter Sets: (All) +Parameter Sets: SiteList Required: True Position: Named @@ -98,11 +98,12 @@ Parameter Sets: File Required: False Position: Named -Default value: None +Default value: False Accept pipeline input: False Accept wildcard characters: False ``` ## RELATED LINKS +[How does Restricted SharePoint Search work?](https://learn.microsoft.com/sharepoint/restricted-sharepoint-search) [Microsoft 365 Patterns and Practices](https://aka.ms/m365pnp) diff --git a/src/Commands/Admin/AddTenantRestrictedSearchAllowedList.cs b/src/Commands/Admin/AddTenantRestrictedSearchAllowedList.cs index 2db07a6b3..5433905e8 100644 --- a/src/Commands/Admin/AddTenantRestrictedSearchAllowedList.cs +++ b/src/Commands/Admin/AddTenantRestrictedSearchAllowedList.cs @@ -6,7 +6,7 @@ using System.Linq; using System; -namespace PnP.PowerShell.Commands.Files +namespace PnP.PowerShell.Commands.Admin { [Cmdlet(VerbsCommon.Add, "PnPTenantRestrictedSearchAllowedList", DefaultParameterSetName = ParameterSet_SiteList)] public class AddTenantRestrictedSearchAllowedList : PnPAdminCmdlet @@ -14,10 +14,10 @@ public class AddTenantRestrictedSearchAllowedList : PnPAdminCmdlet private const string ParameterSet_SiteList = "SiteList"; private const string ParameterSet_File = "File"; - [Parameter(Mandatory = false, ParameterSetName = ParameterSet_SiteList)] + [Parameter(Mandatory = true, ParameterSetName = ParameterSet_SiteList)] public string[] SitesList; - [Parameter(Mandatory = false, ParameterSetName = ParameterSet_File)] + [Parameter(Mandatory = true, ParameterSetName = ParameterSet_File)] public string SitesListFileUrl; [Parameter(Mandatory = false, ParameterSetName = ParameterSet_File)] @@ -44,6 +44,11 @@ protected override void ExecuteCmdlet() throw new InvalidOperationException("SiteList cannot be null"); } + if(_sitelist.Count > 100) + { + WriteWarning($"The maximum number of sites that can be added to the allowed list is 100. You have specified {_sitelist.Count} sites. Will try to add them anyway."); + } + Tenant.AddSPORestrictedSearchAllowedList(_sitelist); AdminContext.ExecuteQueryRetry(); } @@ -58,10 +63,9 @@ private IList ReadFileContents() foreach (var line in lines) { - var columns = line.Split(','); - if (columns.Length != 1) + if (line.Contains(',')) { - throw new InvalidOperationException("File should only contain one column"); + throw new InvalidOperationException("File should only contain one column and no commas"); } }