Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
languagejs
 /* 
	Script by: Tim Wheeler http://tjwheeler.blogspot.com/
	This script retrieves the classification properties from the Root Web of the current site collection's 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\').hideslideUp()"> &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);
});

...