Classifying Site Collection Content

Script

Name

Description

Last Modified Date

Download

Set-SiteClassification

Updates property bag values

16/05/2014

Set-SiteClassification.ps1

Get-SiteClassificationGets applied classification16/05/2014Get-SiteClassification.ps1
Classify.js

A script to inject classification at top of page

Note: This script depends on jQuery.

16/05/2014

classify.js

 

Summary

These set of scripts allow a classification to be applied to a SharePoint Site Collection.  It is an information only value and does not attempt to control content at any level.

It looks like:

The process to use this consists of:

  1. Run the Set-SiteClassification.ps1 to apply the classification values to your site
  2. Upload Classify.js to the site
  3. Add a reference to the JS in the master page

 

Thanks

Thanks to Jeremy Thake for his post on retrieving the property bag values: http://www.jeremythake.com/2013/10/using-the-sharepoint-csom-api-to-get-a-property-bag-value/

 

Classify.js

 /* 
	Script by: Tim Wheeler http://tjwheeler.blogspot.com/
	This script retrieves the classification properties from the Root Web of the current site collection's root web property bag.
	It then injects html positioned at the top center of the page identifying the content classification.
	If the properties are not present or invalid it will show an Unclassified name.
	This script should be referenced in the master page: Eg;
	<SharePoint:ScriptLink language="javascript" name="~SiteCollection/style library/classify/classify.js" OnDemand="false" runat="server" Localizable="false" /> 
	Thanks to Jeremy Thake for his post on retrieving the property bag values: http://www.jeremythake.com/2013/10/using-the-sharepoint-csom-api-to-get-a-property-bag-value/
*/
var spClassify= {};
//Function that makes the client side call to the server
spClassify.getClassification = function(onSuccess, onFailure) {
    spClassify.clientContext = SP.ClientContext.get_current();
    spClassify.site = spClassify.clientContext.get_site();
    spClassify.web = spClassify.site.get_rootWeb();
    spClassify.clientContext.load(spClassify.web);
    spClassify.properties = spClassify.web.get_allProperties();
	spClassify.clientContext.load(spClassify.properties);
    spClassify.clientContext.executeQueryAsync(Function.createDelegate(this, onSuccess), Function.createDelegate(this, onFailure));
};
spClassify.renderLink = function(color, backColor, name, url){
	$('body').append('<div id="classificationWrapper" style="background:' + backColor + ';border-left:1px solid black;border-bottom:1px solid black;border-right:1px solid black; position:absolute; left:50%;border-bottom-left-radius: 4px;border-bottom-right-radius: 4px; padding-top:1px;padding-left: 5px; padding-right:5px;  padding-bottom: 2px; top:0;"></div>');
	var link = '<a id="classificationLink" target=_blank; href="' + url  + '" style="color:' + color + ';">' + name + '</a>';
	$("#classificationWrapper").append(link);
	$("#classificationWrapper").append('<label style="color:' + color + '" onclick="$(\'#classificationWrapper\').slideUp()"> &nbsp;&#94;</label>'); 
};
//If successful, this method is called
spClassify.onSuccess = function(sender, args) {
	try
	{
	    var color = spClassify.properties.get_item('ClassificationColor'); // '#32B332'
  	    var backColor = spClassify.properties.get_item('ClassificationBackColor'); // '#32B332'
	    var name =  spClassify.properties.get_item('ClassificationName');
	    var url = spClassify.properties.get_item('ClassificationUrl');
	    spClassify.renderLink(color, backColor, name, url);
	}
	catch(e)
	{
	 	var color = 'black';
  	    var backColor = 'white';
	    var name =  'Unclassified';
	    var url = '';
   	    spClassify.renderLink(color, backColor, name, url);
	}
};
//If an error occurs this method is called
spClassify.onFailure = function(sender, args) {
    if (args instanceof SP.ClientRequestFailedEventArgs) {
        var message = args.get_message();
        var code = args.get_errorCode();
        var details = args.get_errorDetails();
        var value = args.get_errorValue();
        var typeName = args.get_errorTypeName();
        var stack = args.get_stackTrace();
        var correlationId = args.get_errorTraceCorrelationId();
        alert('Error: ' + message + ".");
    } else {
        alert('Unexpected Error accessing classification: ' + args);
    }
};
//This method initates the process
function ClassifyThis() {
	spClassify.getClassification(spClassify.onSuccess, spClassify.onFailure);
} 
$(document).ready(function () {
	SP.SOD.executeFunc('sp.js', 'SP.ClientContext', ClassifyThis);
});

 

Adding script reference to master page

In the head section of the master page you can add a reference as follows:

 <SharePoint:ScriptLink language="javascript" name="~SiteCollection/style library/classify/classify.js" OnDemand="false" runat="server" Localizable="false" /> 

 

Set-SiteClassification

This is a powershell script to create the properties on the site collection's root web.

#Name: Set-SiteClassification
#Description: Applies details of the site classification to the property bag on the root web.  
#           : Combinded with the classify.js script, the classification will display at the top center of the web page. 
#Note:This is informational only and requires a reference to the classify.js script.
#Usage: 
#    $green = "#18A329"
#    $white = "#ffffff"
#    .\ClassifySiteCollection "http://mysharepoint" "Low" $green $white "http://mysharepoint/pages/classificatons.aspx"
param
(
    $siteCollectionUrl = (Read-Host "Please enter a site collection URL"),
    $name = (Read-Host "Please enter a classificaton name"),
    $backColor = (Read-Host "Please enter the background color value"),
    $color = (Read-Host "Please enter the foreground color"),
    $url = (Read-Host "Please enter the url of the classificaton definition")
)
$site = get-spsite $siteCollectionUrl
$web = $site.RootWeb;
$web.AllProperties['ClassificationColor'] = $color
$web.AllProperties['ClassificationBackColor'] = $backColor
$web.AllProperties['ClassificationName'] = $name
$web.AllProperties["ClassificationUrl"] = $url
$web.Update()
"A classification of '$name' has been applied to site collection " + $site.Url 

 

Get-SiteClassification

 #Name: Get-SiteCollection
#Description: Retrieves the properties for the classification if it has been applied to the site collection
#Usage: 
#    .\Get-SiteClassification "http://mysharepoint"
param
(
    $siteCollectionUrl = (Read-Host "Please enter a site collection URL")
)
$site = get-spsite $siteCollectionUrl
$web = $site.RootWeb
"ClassificationName:" + $web.AllProperties['ClassificationName']
"ClassificationColor:" + $web.AllProperties['ClassificationColor']
"ClassificationBackColor:" + $web.AllProperties['ClassificationBackColor']
"ClassificationUrl:" + $web.AllProperties["ClassificationUrl"]

CodeMonkey Software is a division of JCHMedia www.jchmedia.com