Display warning message in SharePoint site using PowerShell

In this SharePoint tutorial, we will discuss how to display a warning message on SharePoint site using PowerShell. Also, we will see how to make SharePoint site read-only using PowerShell.

A lot of time you might need to display a warning message or banner message using PowerShell in the SharePoint site. This will work in SharePoint 2013, SharePoint 2016, and SharePoint 2019.

Display warning message in SharePoint site using PowerShell

Now, let us see how to display a banner or warning message in a SharePoint 2013 site using PowerShell.

Creating new custom action on the SharePoint site to display banner /warning message on the site header. The PowerShell script will add the custom action and add a banner on all SharePoint sites on the site collection automatically.

Display warning message in SharePoint site using PowerShell
Display warning message in SharePoint site using PowerShell

You can write, debug and test PowerShell script using PowerShell ISE.

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://sitecollection"
Add-CustomAction -Site $site -Message "This site will be moved to cloud . The on premise version will be read only.For any information please write to email id [email protected]" -ActionName "SiteMovedBanner" -BackGroundColor Yellow

Make SharePoint site readonly using PowerShell

We can also easily make a SharePoint site readonly using PowerShell. You can run the below PowerShell script to make a SharePoint site readonly. It will work in SharePoint 2013, SharePoint 2016 and SharePoint 2019.

Set-SPSite -Identity "https://Sitecollection" -LockState "Unlock"

You may like the following SharePoint tutorials:

In this SharePoint tutorial, we learned:

  • Display warning message in SharePoint site using PowerShell
  • Make SharePoint site readonly using PowerShell
  • Nice solution!
    There is a bug in code. when you clicking around on site, banner will shown multiple time:

    code for java should looks like:

    function Execute_$($ActionName)_MessageBar_$($PSCmdlet.ParameterSetName)()
    {
    var st = document.getElementById(‘$($ActionName)_MessageBar_$($PSCmdlet.ParameterSetName)’);

    if(st != null)
    {
    st.style.display = ”;

    }
    else {
    st = document.createElement(“div”);
    st.id = “$($ActionName)_MessageBar_$($PSCmdlet.ParameterSetName)”;
    st.innerHTML = “$Message”;

    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)();
    }

    • I have the same issue where the alert duplicates but when i replace the code with this, the alert doesn’t show at all on SP2013.

  • >