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.
10,917 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,572 questions
0 comments No comments
{count} votes

Accepted answer
  1. Spencer Cooper 201 Reputation points
    2021-09-26T07:35:01.86+00:00

    Solved this with the following code

    $removed= $Site.CustomScriptSafeDomains.GetByDomainName("test.com")
    $removed.deleteobject()

    1 person found this answer helpful.
    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. Elsie Lu_MSFT 9,796 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,696 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 : )


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.