Today I was thinking about a simple solution to display a classification on a SharePoint site. There is a solution available in MSDN, but for me it was to complex. My solution uses some JavaScript injection that was taken from an PnP sample. This sample in combination with a property in the root folder of a library would be an easy solution.

The JavaScript file would look like this (in my case I called this file ShowSiteClassification.js.

var jQuery = "https://ajax.aspnetcdn.com/ajax/jQuery/jquery-2.0.2.min.js";

// Is MDS enabled?
if ("undefined" != typeof g_MinimalDownload && g_MinimalDownload && (window.location.pathname.toLowerCase()).endsWith("/_layouts/15/start.aspx") && "undefined" != typeof asyncDeltaManager) {
    // Register script for MDS if possible
    RegisterModuleInit("ShowSiteClassification.js", JavaScript_Embed); //MDS registration
    JavaScript_Embed(); //non MDS run
} else {
    JavaScript_Embed();
}

function JavaScript_Embed() {

    loadScript(jQuery, function () {
        $(document).ready(function () {
            // Execute status setter only after SP.JS has been loaded
            SP.SOD.executeOrDelayUntilScriptLoaded(function () { GetSiteClassification(); }, 'sp.js');
        });
    });
}

function GetSiteClassification() {
    var ctx = new SP.ClientContext.get_current();

    // var webProperties = ctx.get_web().get_allProperties();
    var webProperties = ctx.get_site().get_rootWeb().get_lists().getByTitle("Scripts").get_rootFolder().get_properties();
    ctx.load(webProperties);
    ctx.executeQueryAsync(Function.createDelegate(this, GetSiteClassificationSuccess), Function.createDelegate(this, GetSiteClassificationFailure));

    function GetSiteClassificationSuccess() {
        var allProps = webProperties.get_fieldValues();

        var customProp = "";

        //make sure the property is there before using it.
        if (webProperties.get_fieldValues().SiteClassification != undefined) {
            var customProp = webProperties.get_fieldValues().SiteClassification;
            var message = "The classification of this site is: " + customProp;

            switch (customProp) {
                case "Internal":
                    var strStatusID = SP.UI.Status.addStatus("Information : ", message, true);
                    SP.UI.Status.setStatusPriColor(strStatusID, "green");
                    break;

                case "Confidential":
                    var strStatusID = SP.UI.Status.addStatus("Information : ", message, true);
                    SP.UI.Status.setStatusPriColor(strStatusID, "yellow");
                    break;

                case "Secret":
                    var strStatusID = SP.UI.Status.addStatus("Information : ", message, true);
                    SP.UI.Status.setStatusPriColor(strStatusID, "red");
                    break;

                default:
                    break;
            }
        }
    }

    function GetSiteClassificationFailure() {
        var message = "There was a failure getting the web properties.";
        var strStatusID = SP.UI.Status.addStatus("Error : ", message, true);
        SP.UI.Status.setStatusPriColor(strStatusID, "red");
    }
}

function loadScript(url, callback) {
    var head = document.getElementsByTagName("head")[0];
    var script = document.createElement("script");
    script.src = url;

    // Attach handlers for all browsers
    var done = false;
    script.onload = script.onreadystatechange = function () {
        if (!done && (!this.readyState
					|| this.readyState == "loaded"
					|| this.readyState == "complete")) {
            done = true;

            // Continue your code
            callback();

            // Handle memory leak in IE
            script.onload = script.onreadystatechange = null;
            head.removeChild(script);
        }
    };

    head.appendChild(script);
}

To store this file, I created a new library in the root web of my site collection. In this library, I broke the permission inheritance and set read permissions for Everyone except External Users (could also be just Everyone). Then I added the script to the custom actions of the site:

Add-PnPJavaScriptLink -Name ShowSiteClassification -Url https://mytenant.sharepoint.com/sites/site-class/Scripts/ShowSiteClassification.js -Scope Site

Finally, I set the SiteClassification property in the root folder of the library I created.

Set-PnPPropertyBagValue -Key "SiteClassification" -Value "Confidential" -Folder /Scripts

With this solution, the classification is also visible in subwebs, even when the user does not have permissions to the root web.

Advertisements