Add a Notification Message in Top Banner of the SharePoint Site using PowerShell

Requirement: Add a message to top banner area of a SharePoint site collection


PowerShell to Add Site Notification Message in SharePoint:
After the migration from SharePoint 2013 to SharePoint 2016, we wanted to add a message at the top banner of a migrated site.
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

Function Add-CustomAction
{
    param 
    (
        [parameter(Mandatory=$true, ParameterSetName='Site')]
        [Microsoft.SharePoint.SPSite]$Site,
        
        [parameter(Mandatory=$true, ParameterSetName='Web')]
        [Microsoft.SharePoint.SPWeb]$Web,
        
        [parameter(Mandatory=$true, ParameterSetName='Site')]
        [parameter(Mandatory=$true, ParameterSetName='Web')]
        [string]$Message,
        
        [parameter(Mandatory=$true, ParameterSetName='Site')]
        [parameter(Mandatory=$true, ParameterSetName='Web')]
        [string]$ActionName,
        
        [parameter(Mandatory=$false, ParameterSetName='Web')]
        [switch]$IncludeSubWebs,

        [parameter(Mandatory=$true, ParameterSetName='Site')]
        [parameter(Mandatory=$true, ParameterSetName='Web')]
        [string][ValidateSet("Red", "Yellow", "Green", "Blue")]$BackGroundColor
    )

    begin
    {
        # To avoid quote conflicts
        $Message = $Message.Replace( "`"", "'")

        $startingSequence = 1100
        $JavaScript = @"

            function Execute_$($ActionName)_MessageBar_$($PSCmdlet.ParameterSetName)()
            {
                var st = document.getElementById('$ActionName');
                if(st != null)
                {
                    st.style.display = '';
                    return;
                }

                st = document.createElement("div");
                st.id = "$($ActionName)_MessageBar_$($PSCmdlet.ParameterSetName)";
                st.innerHTML = "<div class='ms-status-$($BackGroundColor.ToLower())' style='padding: 12px 14px 6px; border: 1px solid; font-family: \"Segoe UI\", Tahoma, sans-serif; font-size: 13px; min-height: 24px;'>$Message</div>";

                document.body.insertBefore(st, document.body.childNodes[0]);
            }

            // don't show on modal dialog windows
            if(!window.location.search.match("[?&]IsDlg=1"))
            {
                Execute_$($ActionName)_MessageBar_$($PSCmdlet.ParameterSetName)();
            } 
"@
    }
    process
    {
        $customAction = $null
        
        if( $PSCmdlet.ParameterSetName -eq "Site" )
        {
            Remove-CustomAction -Site $site -ActionName $ActionName | Out-Null
            $customAction = $Site.UserCustomActions.Add()
        }
        else
        {
            Remove-CustomAction -Web $Web -ActionName $ActionName | Out-Null
            $customAction = $Web.UserCustomActions.Add()
        }

        $customAction.Location    = "ScriptLink"
        $customAction.Sequence    = $startingSequence
        $customAction.Title       = $ActionName
        $customAction.ScriptBlock = $JavaScript
        $customAction.Update()

        if( $IncludeSubWebs )
        {
            foreach($w in $Web.Webs)
            {
                Add-CustomAction -Web $w -Message $Message -ActionName $ActionName -IncludeSubWebs -BackGroundColor $BackGroundColor
            }
        }
    }
    end
    {
    }
}

Function Remove-CustomAction
{
    param 
    (
        [parameter(Mandatory=$true, ParameterSetName='Site')]
        [Microsoft.SharePoint.SPSite]$Site,
        
        [parameter(Mandatory=$true, ParameterSetName='Web')]
        [Microsoft.SharePoint.SPWeb]$Web,
        
        [parameter(Mandatory=$true, ParameterSetName='Site')]
        [parameter(Mandatory=$true, ParameterSetName='Web')]
        [string]$ActionName
    )

    begin
    {
        $existingActions = @()
    }
    process
    {
        if( $PSCmdlet.ParameterSetName -eq "Site" )
        {
            $Site.UserCustomActions | ? { $_.Title -eq $ActionName } | % { $existingActions += $_ }
            $existingActions | % { $Site.UserCustomActions.Item($_.Id).Delete() }
        }
        else
        {
            $Web.UserCustomActions | ? { $_.Title -eq $ActionName } | % { $existingActions += $_ }
            $existingActions
            $existingActions | % { $Web.UserCustomActions.Item($_.Id).Delete() }
        }
    }
    end
    {
    }
}

$Site = Get-SPSite -Identity "https://intranet.crescent.com/"
Add-CustomAction -Site $site -Message "This site has been migrated to SharePoint 2016. Please don't change anything here!" -ActionName "SiteMovedBanner" -BackGroundColor Red
To Set a notification at web level, use:
$web = Get-SPWeb -Identity "https://intranet.crescent.com/sites/marketing/us"
Add-CustomAction -Web $web -Message "This site has been migrated to SharePoint 2016. Please don't change anything here!" -ActionName "WebMovedBanner" -BackGroundColor Yellow -IncludeSubWebs
To Remove the Custom action, use:
Remove-CustomAction  -Site $site -ActionName "SiteMovedBanner"
Add a Notification Message in Top Banner of the SharePoint Site using PowerShell Add a Notification Message in Top Banner of the SharePoint Site using PowerShell Reviewed by Salaudeen Rajack on April 03, 2019 Rating: 5

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.