From cf78c71e3c65700767920f42272a905136823934 Mon Sep 17 00:00:00 2001 From: Koen Zomers Date: Fri, 4 Oct 2024 09:59:39 +0200 Subject: [PATCH] Implemented new cmdlet --- CHANGELOG.md | 1 + documentation/Remove-PnPSearchExternalItem.md | 96 +++++++++++++++++++ .../Search/RemoveSearchExternalItem.cs | 37 +++++++ 3 files changed, 134 insertions(+) create mode 100644 documentation/Remove-PnPSearchExternalItem.md create mode 100644 src/Commands/Search/RemoveSearchExternalItem.cs diff --git a/CHANGELOG.md b/CHANGELOG.md index 3dda04957..1e12c2f99 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,6 +21,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/). - Added `-X509KeyStorageFlags` parameter to `Connect-PnPOnline` cmdlet which allows setting of how private keys are to be imported. [#4324](https://github.com/pnp/powershell/pull/4324) - Added `-RestrictContentOrgWideSearch` parameter to `Set-PnPSite` which allows for applying the Restricted Content Discoverability (RCD) setting to a site [#4335](https://github.com/pnp/powershell/pull/4335) - Added `-LaunchBrowser` parameter to `Register-PnPEntraIDAppForInteractiveLogin` and `Register-PnPEntraIDApp` cmdlets to open browser and continue app registration in the browser. [#4347](https://github.com/pnp/powershell/pull/4347) & [#4348](https://github.com/pnp/powershell/pull/4348) +- Added `Remove-PnPSearchExternalItem` which allows for removal of an external item from the Microsoft Search index ### Changed diff --git a/documentation/Remove-PnPSearchExternalItem.md b/documentation/Remove-PnPSearchExternalItem.md new file mode 100644 index 000000000..5caa68475 --- /dev/null +++ b/documentation/Remove-PnPSearchExternalItem.md @@ -0,0 +1,96 @@ +--- +Module Name: PnP.PowerShell +schema: 2.0.0 +applicable: SharePoint Online +online version: https://pnp.github.io/powershell/cmdlets/Remove-PnPSearchExternalItem.html +external help file: PnP.PowerShell.dll-Help.xml +title: Remove-PnPSearchExternalItem +--- + +# Remove-PnPSearchExternalItem + +## SYNOPSIS + +**Required Permissions** + + * Microsoft Graph API : One of ExternalItem.ReadWrite.OwnedBy, ExternalItem.ReadWrite.All as a delegate or application permission + +Removes an external item from an external connector in Microsoft Search + +## SYNTAX + +```powershell +Remove-PnPSearchExternalItem -ItemId -ConnectionId [-Verbose] [-Connection ] +``` + +## DESCRIPTION + +This cmdlet can be used to remove a specific external item from a Microsoft Search custom connector. The item will be removed from the index and will no longer be available for search results. + +## EXAMPLES + +### EXAMPLE 1 +```powershell +Remove-PnPSearchExternalItem -ConnectionId "pnppowershell" -ItemId "12345" +``` + +This will remove the external item with the identifier "12345" from the external Microsoft Search index connector named "pnppowershell". + +## 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 +``` + +### -ItemId +Unique identifier of the external item in Microsoft Search that you wish to remove. + +```yaml +Type: String +Parameter Sets: (All) +Required: True +Position: Named +Default value: None +Accept pipeline input: False +Accept wildcard characters: False +``` + +### -ConnectionId +The Connection ID or connection instance of the custom connector to use. This is the ID that was entered when registering the custom connector and will indicate for which custom connector this external item is being removed from the Microsoft Search index. + +```yaml +Type: SearchExternalConnectionPipeBind +Parameter Sets: (All) +Required: True +Default value: None +Accept pipeline input: True +Accept wildcard characters: False +``` + +### -Verbose +When provided, additional debug statements will be shown while executing the cmdlet. + +```yaml +Type: SwitchParameter +Parameter Sets: (All) + +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) +[Microsoft Graph documentation](https://learn.microsoft.com/graph/api/externalconnectors-externalitem-delete) \ No newline at end of file diff --git a/src/Commands/Search/RemoveSearchExternalItem.cs b/src/Commands/Search/RemoveSearchExternalItem.cs new file mode 100644 index 000000000..0ba337245 --- /dev/null +++ b/src/Commands/Search/RemoveSearchExternalItem.cs @@ -0,0 +1,37 @@ +using System.Management.Automation; +using PnP.PowerShell.Commands.Base; +using PnP.PowerShell.Commands.Base.PipeBinds; +using PnP.PowerShell.Commands.Attributes; +using PnP.PowerShell.Commands.Utilities.REST; +using System.Management.Automation.Remoting; + +namespace PnP.PowerShell.Commands.Search +{ + [Cmdlet(VerbsCommon.Remove, "PnPSearchExternalItem")] + [RequiredApiDelegatedOrApplicationPermissions("graph/ExternalItem.ReadWrite.OwnedBy")] + [RequiredApiDelegatedOrApplicationPermissions("graph/ExternalItem.ReadWrite.All")] + [OutputType(typeof(Model.Graph.MicrosoftSearch.ExternalItem))] + public class RemoveSearchExternalItem : PnPGraphCmdlet + { + [Parameter(Mandatory = true, ValueFromPipeline = true, Position = 0)] + public SearchExternalConnectionPipeBind ConnectionId; + + [Parameter(Mandatory = true)] + public string ItemId; + + protected override void ExecuteCmdlet() + { + var connection = ConnectionId.GetExternalConnection(this, Connection, AccessToken); + + try + { + var response = GraphHelper.Delete(this, Connection, $"beta/external/connections/{connection.Id}/items/{ItemId}", AccessToken); + WriteVerbose($"External item with ID '{ItemId}' successfully removed from external connection '{connection.Id}'"); + } + catch (PSInvalidOperationException ex) + { + throw new PSInvalidOperationException($"Removing external item with ID '{ItemId}' from external connection '{connection.Id}' failed with message '{ex.Message}'", ex); + } + } + } +} \ No newline at end of file