Showing external content with your Sitecore PowerShell scripts

Sitecore PowerShell Extensions have proven time and again as a great tool for manipulating and exposing Sitecore content, but that about if you need to show your authors something that is external to your Sitecore instance?

This blog shows a number of techniques to show pages and embedded content in various ways as part of your script execution.

Showing external content in a new tab

The most obvious way is to show your author an external page as a new tab in the browser. This can be achieved by executing a JavaScript expression that will become available in SPE 4.4+

Invoke-JavaScript -Script `
    "window.open('http://blog.najmanowicz.com/', '_blank')"

But what if you want to present them with something that is more integrated with a Sitecore experience?

Opening external content within Sitecore Desktop window

How about opening the same URL but within the Sitecore desktop interface rather than in a tab? The following makes it possible:

$URL = "http://blog.najmanowicz.com/"
$Width = 800
$Height = 600
$Title = "Adam's blog..."

Invoke-ShellCommand `
    -Name "ise:iframe(spe_url=$url,spe_w=$width,spe_h=$height,spe_t=$title)" `
    -Path master:\ ` # not ideal but the cmdlet needs it

And will result in the following window appearing on your desktop:
pageiniframe

But that is not the end, there are further ways to integrate external and internally crafted content in your SPE dialogs!

Show an IFrame in an interactive dialog

You can embed iframes within dialogues created with the Read-Variable cmdlet. The cmdlet is generally used to prompt your users to provide values for variables needed by your scripts, for this purpose it comes with a number of “editors”. one of such is “info” editor that does nothing more than rendering content of a variable or any data otherwise provided. Should the content be HTML formatted you can do some curious things with it, such as following:

$embed = '<iframe width="560" height="315" ' +
    'src="https://www.youtube.com/embed/VPpIjhtgGj0" ' +
    'frameborder="0" allowfullscreen></iframe>' 

Read-Variable `
    -Parameters @{Name="embed"; Title=""; Editor="info" } `
    -Width 595 -Height 410 `
    -Title "Cave Johnson on safe science" `
    -Description "Embedded Youtube video"

Resulting in the following dialog:

cavejohnsonembedded

Iframe however is not the only thing you can embed, why not get a bit more creative and write the code in the dialog directly?

Consider this sample code:

$html = @'
<style>;
 .banner { position: relative; overflow: auto; }
 .banner li { list-style: none; }
 .banner ul li { float: left; }
</style>
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
<script src="http://unslider.com/unslider.min.js"></script>
<script>
  $(function() {
    $('.banner').unslider();
  });
</script>
<div class="banner">
<ul>
	<li><img src="https://sitecorepowershellcom.files.wordpress.com/2016/11/powershellbanner2.png"></li>
	<li><img src="https://sitecorepowershellcom.files.wordpress.com/2017/01/sitecore_red_logo.png"></li>
	<li><img src="https://sitecorepowershellcom.files.wordpress.com/2017/01/join_the_sitecore.jpg"></li>
</ul>
</div>
'@
Read-Variable `
    -Parameters @{Name="html"; Title=""; Editor="info" } `
    -Width 675 -Height 500 `
    -Title "Embedded carousel" `
    -Description "You can do just about anything!"

And the awesome result!

embeddedcarousel

Hope this blog post will help you create even more awesome SPE interfaces.

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s