public/Add-SkylineScriptEditorWebPart.ps1

Function Add-SkylineScriptEditorWebPart {
    <#
    .SYNOPSIS
    Uploads a script editor webpart to the Web Part Gallery
     
    .DESCRIPTION
    Uploads a script editor webpart to the Web Part Gallery
     
    .EXAMPLE
    Add new script editor web part with the group 'Intranet' and javascript hello world alert
     
    Add-SkylineScriptEditorWebPart -Title "Widget1" -Description "The best widget" -Group "Intranet" -Content "<script>window.alert('hello world');</script>"
     
    .PARAMETER Title
    Name of the webpart
 
    .PARAMETER Content
    Script to include within the script editor web part
 
    .PARAMETER Description
    Description of the webpart
 
    .PARAMETER Group
    Group which the webpart should belong to. Default is 'Custom'
     
    .PARAMETER FileName
    File name of the webpart file that gets uploaded to the web part gallery.
    File extension will be added if not included.
    Default is Title parameter with .webpart extension (e.g. -Title "MyWebPart" becomes -FileName "MyWebPart.webpart")
 
    .PARAMETER Web
    The web to apply the command to. Omit this parameter to use the current web.
 
    #>


    [cmdletbinding()]   
    param(
        [parameter(Mandatory = $True)]
        [string]$Title = '',
        [parameter(Mandatory = $True)]
        [string]$Content = '',
        [parameter(Mandatory = $False)]
        [string]$Description = '',
        [parameter(Mandatory = $False)]
        [string]$Group = 'Custom',
        [parameter(Mandatory = $False)]
        [string]$FileName = '',
        [Microsoft.SharePoint.Client.Web]$Web
    )
    
    Process
    {
        Write-Debug ( "Running $($MyInvocation.MyCommand).`n" + "PSBoundParameters:`n$($PSBoundParameters | Format-List | Out-String)")

        #Stream varibles
        $DWPStream = $null
        $DWPStreamWriter = $null

        Try
        {   
            $PSBoundParameters.Remove("Title") | Out-Null
            $PSBoundParameters.Remove("Content") | Out-Null
            $PSBoundParameters.Remove("Description") | Out-Null
            $PSBoundParameters.Remove("FileName") | Out-Null
            
            #Sanitize content script
            $SanitizedContent = $Content
            $SanitizedContent = $SanitizedContent.Replace("&", "&amp;")
            $SanitizedContent = $SanitizedContent.Replace("<", "&lt;")
            $SanitizedContent = $SanitizedContent.Replace(">", "&gt;")

            #Get Web Part XML and replace tokens
            $WebPartTemplate = Get-Content ".\assets\ScriptEditor.webpart" -Raw
            $WebPartTemplate = $WebPartTemplate.Replace("{{WebPartTitle}}", $Title)
            $WebPartTemplate = $WebPartTemplate.Replace("{{WebPartDescription}}", $Description)
            $WebPartTemplate = $WebPartTemplate.Replace("{{Content}}", $SanitizedContent)
            
            #Ensure FileName is set
            if ([String]::IsNullOrEmpty($FileName))
            {
                $FileName = $Title
            }
            
            #Ensure FileName ends with .webpart extension
            if (-Not $FileName.ToLower().EndsWith('.webpart'))
            {
                $FileName = "$($FileName).webpart"
            }

            #Open memory stream of DWP XML
            $DWPStream = New-Object System.IO.MemoryStream
            $DWPStreamWriter = New-Object System.IO.StreamWriter $DWPStream
            $DWPStreamWriter.WriteLine($WebPartTemplate)
            $DWPStreamWriter.Flush()
            $DWPStream.Seek(0,"Begin")
            
            #Upload DWP XML to webpart gallery
            Add-SkylineWebPart -FileName $FileName -Stream $DWPStream @PSBoundParameters
        }
        Catch
        {
            Throw $_
        }
        Finally
        {
            #Dispose DWP stream objects
            if ($DWPStream -ne $null)
            {
                $DWPStream.Dispose()
            }
            if ($DWPStreamWriter -ne $null)
            {
                $DWPStreamWriter.Dispose()
            }
        }
    }
}