How to remove domains within HTML Field Security settings via powershell

Spencer Cooper 201 Reputation points
2021-09-24T01:33:12.793+00:00

As the title states, I am working on a script and need to have the ability to remove links from the allowed domains section of HTML Field Security of Sharepoint Online.....

as an example the below code successfully adds links.

Set Variables

$SiteURL = "https://crescentintranet.sharepoint.com/sites/marketing"

Connect to PnP Online

Connect-PnPOnline -Url $SiteURL -UseWebLogin

Get the Site

$Site = Get-PnPSite -Includes CustomScriptSafeDomains

Add domain

$Domain = [Microsoft.SharePoint.Client.ScriptSafeDomainEntityData]::new()
$Domain.DomainName = "products.crescent.com"
$Site.CustomScriptSafeDomains.Create($Domain)
Invoke-PnPQuery

SharePoint
SharePoint
A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.
9,565 questions
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,355 questions
0 comments No comments
{count} votes

2 additional answers

Sort by: Most helpful
  1. Elsie Lu_MSFT 9,756 Reputation points
    2021-09-24T08:44:21.52+00:00

    Hi @Spencer Cooper ,

    You could try to remove links manully:

    134994-72.png


    If the answer is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


  2. Limitless Technology 39,336 Reputation points
    2021-09-24T11:19:27.973+00:00

    Hello RowdeyCooper,

    Your code seems completely fine to me.

    But before code execution Add Trusted Domain To HTML Field security List using the below step.

    Online Site Collection >> Site Settings >> Select "HTML Field Security" from Site Collection Administration.

    Choose Allow Contributors to insert iframe" Option

    Add your preference and Click Ok.

    and consider Switching On the Custom Script before the process.


    Hope this answers all your queries, if not please do repost back.
    If an Answer is helpful, please click "Accept Answer" and upvote it : )