Skip to content

Commit

Permalink
Merge pull request #3993 from reshmee011/rssaddsites
Browse files Browse the repository at this point in the history
new cmdlet for Add-PnPTenantRestrictedSearchAllowedList
  • Loading branch information
KoenZomers authored Sep 10, 2024
2 parents 54e99a5 + 3873792 commit 23ce48d
Show file tree
Hide file tree
Showing 3 changed files with 186 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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]
Expand Down
109 changes: 109 additions & 0 deletions documentation/Add-PnPTenantRestrictedSearchAllowedList.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
---
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 <String>] [-SitesList <String[]>] [-ContainsHeaders <SwitchParameter>] [-Connection <PnPConnection>]
```

## 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. At present, a maximum of 100 sites can be added to the allowed list.

## 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 -SitesList @("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: File

Required: True
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: SiteList

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: 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)
75 changes: 75 additions & 0 deletions src/Commands/Admin/AddTenantRestrictedSearchAllowedList.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
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.Admin
{
[Cmdlet(VerbsCommon.Add, "PnPTenantRestrictedSearchAllowedList", DefaultParameterSetName = ParameterSet_SiteList)]
public class AddTenantRestrictedSearchAllowedList : PnPAdminCmdlet
{
private const string ParameterSet_SiteList = "SiteList";
private const string ParameterSet_File = "File";

[Parameter(Mandatory = true, ParameterSetName = ParameterSet_SiteList)]
public string[] SitesList;

[Parameter(Mandatory = true, ParameterSetName = ParameterSet_File)]
public string SitesListFileUrl;

[Parameter(Mandatory = false, ParameterSetName = ParameterSet_File)]
public SwitchParameter ContainsHeader;

protected override void ExecuteCmdlet()
{
IList<string> _sitelist = null;
if (ParameterSetName == ParameterSet_File)
{
_sitelist = ReadFileContents();
}
else if (ParameterSetName == ParameterSet_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");
}

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();
}

private IList<string> ReadFileContents()
{
var lines = System.IO.File.ReadAllLines(SitesListFileUrl);
if (ContainsHeader)
{
lines = lines.Skip(1).ToArray();
}

foreach (var line in lines)
{
if (line.Contains(','))
{
throw new InvalidOperationException("File should only contain one column and no commas");
}
}

return lines.ToList();
}
}
}

0 comments on commit 23ce48d

Please sign in to comment.