Skip to content

Commit

Permalink
Add Remove-PnPTenantRestrictedSearchAllowedList cmdlet (#4399)
Browse files Browse the repository at this point in the history
* Add example to get sharing link from list item

* new cmdlet to remove rss

---------

Co-authored-by: Gautam Sheth <gautamdsheth@outlook.com>
  • Loading branch information
reshmee011 and gautamdsheth authored Oct 10, 2024
1 parent 7268de8 commit 975f959
Show file tree
Hide file tree
Showing 2 changed files with 184 additions and 0 deletions.
109 changes: 109 additions & 0 deletions documentation/Remove-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/Remove-PnPTenantRestrictedSearchAllowedList.html
external help file: PnP.PowerShell.dll-Help.xml
title: Remove-PnPTenantRestrictedSearchAllowedList
---

# Remove-PnPTenantRestrictedSearchAllowedList

## SYNOPSIS
Removes site URLs from 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
Remove-PnPTenantRestrictedSearchAllowedList [-SitesListFileUrl <String>] [-SitesList <String[]>] [-ContainsHeaders <SwitchParameter>] [-Connection <PnPConnection>]
```

## DESCRIPTION

Removes site URLs from 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
Remove-PnPTenantRestrictedSearchAllowedList -SitesListFileUrl "C:\temp\sitelist.csv" -ContainsHeader
```

Removes site URLs from the allowed list from a CSV file. The first line, which is assumed to be a header, is skipped.

### EXAMPLE 2
```powershell
Remove-PnPTenantRestrictedSearchAllowedList -SitesListFileUrl "C:\temp\sitelist.csv"
```

Removes site URLs from the allowed list from a CSV file.

### EXAMPLE 3
```powershell
Remove-PnPTenantRestrictedSearchAllowedList -SitesList @("https://contoso.sharepoint.com/sites/Company311","https://contoso.sharepoint.com/sites/contosoportal")
```
Removes the specified sites from 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 removed from 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 remove from 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/RemoveTenantRestrictedSearchAllowedList.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.Remove, "PnPTenantRestrictedSearchAllowedList", DefaultParameterSetName = ParameterSet_SiteList)]
public class RemoveTenantRestrictedSearchAllowedList : 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.RemoveSPORestrictedSearchAllowedList(_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 975f959

Please sign in to comment.