Extending the List of Sites You can Embed From in SharePoint Using PowerShell

The Embed web part for modern pages lets you display content from secure websites right on your page. Want to show a YouTube video? Grab the embed code from youtube.com and slap it in the Embed web part. Wowee!

By default, modern pages support 30+ sites including the most common like YouTube, Vimeo, TED, and internal domains like Stream and OneDrive. But what about when you’ve got content from a site not on this list? You’ll end up with an error similar to this:

Don’t cry! Wipe those tears off that wet face! If you just need to allow the domain for a single site, the instructions are right there (here’s a quick summary):

  • Go to Site Settings
  • Click on HTML Field Security under Site Collection Administration
  • Type the domain from the error message (no https://) into the box and click Add
  • Click OK
  • Give it another try

But wait… Corporate just rolled out a video hosting platform for the enterprise and they want all sites to be able to embed content from this new site. Does the thought of repeating the above steps hundreds or even thousands of times make you weep in despair? Smack those tears off your moistened face!

Here’s a quick snippet of PowerShell which will show you how to add it to multiple sites:

$SiteUrls = @("HR","Accounting","IT")
foreach($SiteUrl in $SiteUrls) {
Write-Host ForegroundColor Cyan "Applying to $SiteUrl"
$FullSiteUrl = "https://superspecial.sharepoint.com/sites/$SiteUrl"
Connect-PnPOnline $FullSiteUrl ErrorAction Stop
$site = Get-PnPSite Includes CustomScriptSafeDomains
$ctx = Get-PnPContext
$ssDomain = [Microsoft.SharePoint.Client.ScriptSafeDomainEntityData]::new()
$ssDomain.DomainName = "special.hosted.panopto.com"

In the PowerShell above, I’m using PnP PowerShell. You can technically do this without PnP PowerShell since it’s just CSOM, but… why would you make your life harder?

Here’s what’s happening:

  • The list of sites in line 1 is just an array of the URL portion of the site after /sites/. You could easily alter this to grab all associated sites for a hub or to get all sites within a classification, etc. But I find a simple list of URLs works pretty well.
  • We connect to the site in line 9 and grab the site object in line 11
  • We get the Client Context in line 12
  • We create a new ScriptSafeDomainEntityData object and set the only part we care about, DomainName, to the URL from the error message before
  • Then in line 17 we use the Create method to add it to the list of domains (there’s no problem if the site already has that domain, it won’t be added twice)
  • We execute the query for the client context to save our changes in line 19
  • Finally we disconnect from the site in line 21 and move on to the next site

You can easily adapt the script above as part of your provisioning process to ensure that new site have the correct domains whitelisted as well. So fun!

Now you can take content from all over the web and mash it together to bring all the relevant stuff directly to your users. WOWEE!

Dog, Pug, Bitch, Pet, Animal, Obedient, Funny, Cute

4 thoughts on “Extending the List of Sites You can Embed From in SharePoint Using PowerShell

  1. Hi. I tried to use this code but I’m getting following:
    Cannot convert argument “parameters”, with value: “Microsoft.SharePoint.Client.ScriptSafeDomainEntityData”, for “Create” to type “Microsoft.SharePoint.Client.ScriptSafeDomainEntityData”: “Cannot convert the “Microsoft.SharePoint.Client.ScriptSafeDomainEntityData” value of type “Microsoft.SharePoint.Client.ScriptSafeDomainEntityData” to type “Microsoft.SharePoint.Client.ScriptSafeDomainEntityData”.”
    At line:
    + $site.CustomScriptSafeDomains.Create($ssDomain)
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : NotSpecified: (:) [], MethodException
    + FullyQualifiedErrorId : MethodArgumentConversionInvalidCastArgument”

    Do you have any idea? I tried to change SharePointPnPPowerShellOnline versions (from 3.17.2001.2 to 3.22.2006.2) but for every version I got the same error.

    • Sorry to hear you’re having trouble! Unfortunately, I can’t recreate this error in my environment. I’ve seen similar issues, however, when you have multiple versions of the SharePoint Client Components installed. Perhaps you have both SP2013 and SPOnline versions on your machine (as an example)? I’d suggest uninstalling all versions of PnP PowerShell (both modules and in add/remove programs) and verifying that you don’t have any additional versions of the SharePoint Client Components installed. Then install PnP PowerShell again as a module. Hope that helps! Let me know if anything is unclear, thanks!

      • Uninstalling everything (related to PNP ;)) and installing again helped! Thank you a lot for the answer and article!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s