SP2013 and PlanetWilson.SharePoint.ColourCalendarV2.4
So, we finally upgraded to SharePoint 2013.
PlanetWilson.SharePoint.ColourCalendarV2.4 was being used on our old SP2007 farm.
I delayed writing this script until my company was off of SP2007, thinking someone else would write it by the time we migrated.
However, I noticed no one easily Googleable has done it.
So after a few hours of coding, here is the script that you can add to your calendars that are using Planet Wilson color coding to get it working again, as originally designed (pulling colors from the Colour Calendar Mapping list).
I wrote it using SharePoint 2013 JavaScript Client API. I didn't use JQuery to reduce the number of library dependencies it had. Just plain old JavaScript. It uses document.getElementsByClassName, though, which only started getting support from IE after IE11. If you are on SP2013, you are more than likely using a fairly recent browser, anyways.
1) Throw the below code into a file called PlanetWilsonSP2013.js.
2) Upload it to a document library on your SharePoint site and add a Script Editor Web Part to your calendar page.
3) Reference the uploaded JavaScript file in the Script Editor Snippet:
<script language="javascript" src="/Scripts/PlanetWilsonSP2013.js"></script>
4) Planet Wilson will once again color code your calendars, just like in the days of yore.
The code will use the Colour Calendar Mapping list in the root web of the site collection to look up the colors. In my environment, I had to modify the Category field to be a text field instead of an empty choice field.
PlanetWilson.SharePoint.ColourCalendarV2.4 was being used on our old SP2007 farm.
I delayed writing this script until my company was off of SP2007, thinking someone else would write it by the time we migrated.
However, I noticed no one easily Googleable has done it.
So after a few hours of coding, here is the script that you can add to your calendars that are using Planet Wilson color coding to get it working again, as originally designed (pulling colors from the Colour Calendar Mapping list).
I wrote it using SharePoint 2013 JavaScript Client API. I didn't use JQuery to reduce the number of library dependencies it had. Just plain old JavaScript. It uses document.getElementsByClassName, though, which only started getting support from IE after IE11. If you are on SP2013, you are more than likely using a fairly recent browser, anyways.
1) Throw the below code into a file called PlanetWilsonSP2013.js.
2) Upload it to a document library on your SharePoint site and add a Script Editor Web Part to your calendar page.
3) Reference the uploaded JavaScript file in the Script Editor Snippet:
<script language="javascript" src="/Scripts/PlanetWilsonSP2013.js"></script>
4) Planet Wilson will once again color code your calendars, just like in the days of yore.
The code will use the Colour Calendar Mapping list in the root web of the site collection to look up the colors. In my environment, I had to modify the Category field to be a text field instead of an empty choice field.
//Author: William W.C.Chung
//Source: http://adamantinewolverine.blogspot.kr/2017/04/sp2013-and-planetwilsonsharepointcolour.html
var objPWColor = new UpdatePlanetWilsonCalendarColors();
_spBodyOnLoadFunctionNames.push("LaunchPWColorCode");
function LaunchPWColorCode()
{
SP.SOD.executeOrDelayUntilScriptLoaded(MyCalendarHook1, 'SP.js');
}
function MyCalendarHook1()
{
SP.SOD.executeOrDelayUntilScriptLoaded(MyCalendarHook, 'SP.UI.ApplicationPages.Calendar.js');
}
function MyCalendarHook() {
objPWColor.initialize();
var _patchCalendar = function () {
//Do something to the items in the calendar here
//ColorCodeCalendar();
objPWColor.ColorCodeCalendar();
};
}
function UpdatePlanetWilsonCalendarColors()
{
var self = this;
function EventCategoryColor(EventCategory, Color, BGColor) {
this.EventCategory = EventCategory || '';
this.Color = Color || '';
this.BGColor = BGColor || '';
}
self.GetEventColor=function(cat)
{
var EventCategoryObj = new EventCategoryColor("","","");
for (var i=0;i<self.EventCategoryArray.length;i++)
{
var ec = self.EventCategoryArray[i];
if (ec.EventCategory.toLowerCase() == cat.toLowerCase())
{
return ec;
}
}
return EventCategoryObj;
}
self.onQuerySucceeded = function (sender, args) {
var listItemInfo = '';
var listItemEnumerator = self.collListItem.getEnumerator();
while (listItemEnumerator.moveNext()) {
var oListItem = listItemEnumerator.get_current();
var PWEventCategory = oListItem.get_item('PWEventCategory');
var BGColor = oListItem.get_item('PWBGColour');
var Color = oListItem.get_item('PWFGColour');
var eventColor = new EventCategoryColor(PWEventCategory, Color, BGColor);
self.EventCategoryArray.push(eventColor);
listItemInfo += "Added event cat: " + eventColor.EventCategory + "\r\n";
}
// alert(listItemInfo.toString());
var _onItemsSucceed = SP.UI.ApplicationPages.CalendarStateHandler.prototype.onItemsSucceed;
SP.UI.ApplicationPages.CalendarStateHandler.prototype.onItemsSucceed = function ($p0, $p1) {
_onItemsSucceed.call(this, $p0, $p1);
self.ColorCodeCalendar();
};
self.ColorCodeCalendar();
}
self.ColorCodeCalendar=function()
{
var eventelements = document.getElementsByClassName('ms-acal-mdiv');
var eventelements2 = document.getElementsByClassName('ms-acal-item');
var eventelements3 = document.getElementsByClassName('ms-acal-ddiv');
//All Day Event
for (var i = 0; i < eventelements.length; i++) {
var divi = eventelements[i];
if (divi.hasChildNodes) {
var alink = divi.firstChild;
if (alink.nodeName.toUpperCase() == 'A') {
var strText = alink.innerHTML;
if (strText.indexOf("|||") >= 0) {
var splitText = strText.split('|||');
var category = splitText[0];
var title = splitText[1];
alink.innerHTML = title;
var eco = self.GetEventColor(category);
var strStyle = "";
if (eco.BGColor) {
strStyle = "background-color: " + eco.BGColor + ";";
}
if (eco.Color) {
strStyle += "color: " + eco.Color + " !important";
}
alink.setAttribute('style', strStyle);
var prevStyle = divi.getAttribute('style');
divi.setAttribute('style', prevStyle + ";" + strStyle);
}
}
}
}
//Recurrent Event Day View
//All Day Event
for (var i = 0; i < eventelements3.length; i++) {
var ddiv = eventelements3[i];
if (ddiv.hasChildNodes) {
var titlediv = ddiv.firstChild;
var alink = null;
if (titlediv.hasChildNodes)
{
for (var j=0;j<titlediv.childNodes.length;j++)
{
if (titlediv.childNodes[j].nodeName && titlediv.childNodes[j].nodeName.toUpperCase()=="A")
{
alink = titlediv.childNodes[j];
continue;
}
}
}
if (alink && alink.nodeName.toUpperCase() == 'A') {
var strText = alink.innerHTML;
if (strText.indexOf("|||") >= 0) {
var splitText = strText.split('|||');
var category = splitText[0];
var title = splitText[1];
alink.innerHTML = title;
var eco = self.GetEventColor(category);
var strStyle = "";
if (eco.BGColor) {
strStyle = "background-color: " + eco.BGColor + ";";
}
if (eco.Color) {
strStyle += "color: " + eco.Color + " !important";
}
alink.setAttribute('style', strStyle);
var prevStyle = ddiv.getAttribute('style');
ddiv.setAttribute('style', prevStyle + ";" + strStyle);
if (ddiv.parentElement.nodeName.toUpperCase()=="DIV")
{
var prevStyle = ddiv.parentElement.getAttribute('style');
ddiv.parentElement.setAttribute('style', prevStyle + ";" + strStyle);
}
for (var j=0;j<ddiv.childNodes.length;j++)
{
var childDiv = ddiv.childNodes[j];
if (childDiv.nodeName.toUpperCase() == 'DIV') {
var prevStyle = childDiv.getAttribute('style');
childDiv.setAttribute('style', prevStyle + ";" + strStyle);
}
}
}
}
}
}
//Timed Event
for (var i = 0; i < eventelements2.length; i++) {
var divi = eventelements2[i];
if (divi.hasChildNodes) {
var sdiv = divi.firstChild;
if (sdiv.hasChildNodes)
{
var childrenDivs = sdiv.childNodes;
if (childrenDivs)
{
var titlediv = null, alink = null;
for (var j = 0; j < childrenDivs.length; j++)
{
var tempDiv = childrenDivs[j];
if (tempDiv.attributes && tempDiv.attributes["class"] && tempDiv.attributes["class"].value=='ms-acal-title')
{
titlediv = tempDiv;
continue;
}
if (tempDiv.nodeName.toUpperCase()=="A")
{
alink = tempDiv;
continue;
}
}
if (titlediv == null && alink==null)
{
continue;
}
if (titlediv && titlediv.hasChildNodes) {
alink = titlediv.firstChild;
}
if (alink.nodeName.toUpperCase()=="A")
{
var strText = alink.innerHTML;
if (strText.indexOf("|||") >= 0) {
var splitText = strText.split('|||');
var category = splitText[0];
var title = splitText[1];
alink.innerHTML = title;
var eco = self.GetEventColor(category);
var strStyle = "";
if (eco.BGColor) {
strStyle = "background-color: " + eco.BGColor + ";";
}
if (eco.Color) {
strStyle += "color: " + eco.Color + " !important";
}
alink.setAttribute('style', strStyle);
var prevStyle=divi.getAttribute('style');
divi.setAttribute('style', prevStyle + ";" + strStyle);
prevStyle = sdiv.getAttribute('style');
sdiv.setAttribute('style', prevStyle + ";" + strStyle);
for (var j = 0; j < childrenDivs.length; j++) {
var tempDiv = childrenDivs[j];
if (tempDiv.attributes && tempDiv.nodeName.toUpperCase()=="DIV") {
var prevStyle = tempDiv.getAttribute('style');
tempDiv.setAttribute('style', prevStyle + ";" + strStyle);
}
}
}
}
}
}
}
}
}
self.onQueryFailed = function (sender, args) {
alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}
self.retrieveEventCategoryItems = function () {
var clientContext = self.ctx;
var oList = self.rootWeb.get_lists().getByTitle('Colour Calendar Mapping');
var camlQuery = new SP.CamlQuery();
camlQuery.set_viewXml(
"<View><Query><Where><Neq><FieldRef Name='PWEventCategory'/><Value Type='Text'></Value></Neq></Where></Query>" +
'<RowLimit>1000</RowLimit></View>'
);
self.collListItem = oList.getItems(camlQuery);
clientContext.load(self.collListItem);
clientContext.executeQueryAsync(
Function.createDelegate(self, self.onQuerySucceeded),
Function.createDelegate(self, self.onQueryFailed)
);
}
// retrieveEventCategoryItems();
self.initialize = function () {
self.EventCategoryArray = [];
self.ctx = new SP.ClientContext();
self.site = self.ctx.get_site();
self.rootWeb = self.site.get_rootWeb();
self.ctx.load(self.rootWeb);
//self.ctx.load(site);
self.retrieveEventCategoryItems();
}
}
//Source: http://adamantinewolverine.blogspot.kr/2017/04/sp2013-and-planetwilsonsharepointcolour.html
var objPWColor = new UpdatePlanetWilsonCalendarColors();
_spBodyOnLoadFunctionNames.push("LaunchPWColorCode");
function LaunchPWColorCode()
{
SP.SOD.executeOrDelayUntilScriptLoaded(MyCalendarHook1, 'SP.js');
}
function MyCalendarHook1()
{
SP.SOD.executeOrDelayUntilScriptLoaded(MyCalendarHook, 'SP.UI.ApplicationPages.Calendar.js');
}
function MyCalendarHook() {
objPWColor.initialize();
var _patchCalendar = function () {
//Do something to the items in the calendar here
//ColorCodeCalendar();
objPWColor.ColorCodeCalendar();
};
}
function UpdatePlanetWilsonCalendarColors()
{
var self = this;
function EventCategoryColor(EventCategory, Color, BGColor) {
this.EventCategory = EventCategory || '';
this.Color = Color || '';
this.BGColor = BGColor || '';
}
self.GetEventColor=function(cat)
{
var EventCategoryObj = new EventCategoryColor("","","");
for (var i=0;i<self.EventCategoryArray.length;i++)
{
var ec = self.EventCategoryArray[i];
if (ec.EventCategory.toLowerCase() == cat.toLowerCase())
{
return ec;
}
}
return EventCategoryObj;
}
self.onQuerySucceeded = function (sender, args) {
var listItemInfo = '';
var listItemEnumerator = self.collListItem.getEnumerator();
while (listItemEnumerator.moveNext()) {
var oListItem = listItemEnumerator.get_current();
var PWEventCategory = oListItem.get_item('PWEventCategory');
var BGColor = oListItem.get_item('PWBGColour');
var Color = oListItem.get_item('PWFGColour');
var eventColor = new EventCategoryColor(PWEventCategory, Color, BGColor);
self.EventCategoryArray.push(eventColor);
listItemInfo += "Added event cat: " + eventColor.EventCategory + "\r\n";
}
// alert(listItemInfo.toString());
var _onItemsSucceed = SP.UI.ApplicationPages.CalendarStateHandler.prototype.onItemsSucceed;
SP.UI.ApplicationPages.CalendarStateHandler.prototype.onItemsSucceed = function ($p0, $p1) {
_onItemsSucceed.call(this, $p0, $p1);
self.ColorCodeCalendar();
};
self.ColorCodeCalendar();
}
self.ColorCodeCalendar=function()
{
var eventelements = document.getElementsByClassName('ms-acal-mdiv');
var eventelements2 = document.getElementsByClassName('ms-acal-item');
var eventelements3 = document.getElementsByClassName('ms-acal-ddiv');
//All Day Event
for (var i = 0; i < eventelements.length; i++) {
var divi = eventelements[i];
if (divi.hasChildNodes) {
var alink = divi.firstChild;
if (alink.nodeName.toUpperCase() == 'A') {
var strText = alink.innerHTML;
if (strText.indexOf("|||") >= 0) {
var splitText = strText.split('|||');
var category = splitText[0];
var title = splitText[1];
alink.innerHTML = title;
var eco = self.GetEventColor(category);
var strStyle = "";
if (eco.BGColor) {
strStyle = "background-color: " + eco.BGColor + ";";
}
if (eco.Color) {
strStyle += "color: " + eco.Color + " !important";
}
alink.setAttribute('style', strStyle);
var prevStyle = divi.getAttribute('style');
divi.setAttribute('style', prevStyle + ";" + strStyle);
}
}
}
}
//Recurrent Event Day View
//All Day Event
for (var i = 0; i < eventelements3.length; i++) {
var ddiv = eventelements3[i];
if (ddiv.hasChildNodes) {
var titlediv = ddiv.firstChild;
var alink = null;
if (titlediv.hasChildNodes)
{
for (var j=0;j<titlediv.childNodes.length;j++)
{
if (titlediv.childNodes[j].nodeName && titlediv.childNodes[j].nodeName.toUpperCase()=="A")
{
alink = titlediv.childNodes[j];
continue;
}
}
}
if (alink && alink.nodeName.toUpperCase() == 'A') {
var strText = alink.innerHTML;
if (strText.indexOf("|||") >= 0) {
var splitText = strText.split('|||');
var category = splitText[0];
var title = splitText[1];
alink.innerHTML = title;
var eco = self.GetEventColor(category);
var strStyle = "";
if (eco.BGColor) {
strStyle = "background-color: " + eco.BGColor + ";";
}
if (eco.Color) {
strStyle += "color: " + eco.Color + " !important";
}
alink.setAttribute('style', strStyle);
var prevStyle = ddiv.getAttribute('style');
ddiv.setAttribute('style', prevStyle + ";" + strStyle);
if (ddiv.parentElement.nodeName.toUpperCase()=="DIV")
{
var prevStyle = ddiv.parentElement.getAttribute('style');
ddiv.parentElement.setAttribute('style', prevStyle + ";" + strStyle);
}
for (var j=0;j<ddiv.childNodes.length;j++)
{
var childDiv = ddiv.childNodes[j];
if (childDiv.nodeName.toUpperCase() == 'DIV') {
var prevStyle = childDiv.getAttribute('style');
childDiv.setAttribute('style', prevStyle + ";" + strStyle);
}
}
}
}
}
}
//Timed Event
for (var i = 0; i < eventelements2.length; i++) {
var divi = eventelements2[i];
if (divi.hasChildNodes) {
var sdiv = divi.firstChild;
if (sdiv.hasChildNodes)
{
var childrenDivs = sdiv.childNodes;
if (childrenDivs)
{
var titlediv = null, alink = null;
for (var j = 0; j < childrenDivs.length; j++)
{
var tempDiv = childrenDivs[j];
if (tempDiv.attributes && tempDiv.attributes["class"] && tempDiv.attributes["class"].value=='ms-acal-title')
{
titlediv = tempDiv;
continue;
}
if (tempDiv.nodeName.toUpperCase()=="A")
{
alink = tempDiv;
continue;
}
}
if (titlediv == null && alink==null)
{
continue;
}
if (titlediv && titlediv.hasChildNodes) {
alink = titlediv.firstChild;
}
if (alink.nodeName.toUpperCase()=="A")
{
var strText = alink.innerHTML;
if (strText.indexOf("|||") >= 0) {
var splitText = strText.split('|||');
var category = splitText[0];
var title = splitText[1];
alink.innerHTML = title;
var eco = self.GetEventColor(category);
var strStyle = "";
if (eco.BGColor) {
strStyle = "background-color: " + eco.BGColor + ";";
}
if (eco.Color) {
strStyle += "color: " + eco.Color + " !important";
}
alink.setAttribute('style', strStyle);
var prevStyle=divi.getAttribute('style');
divi.setAttribute('style', prevStyle + ";" + strStyle);
prevStyle = sdiv.getAttribute('style');
sdiv.setAttribute('style', prevStyle + ";" + strStyle);
for (var j = 0; j < childrenDivs.length; j++) {
var tempDiv = childrenDivs[j];
if (tempDiv.attributes && tempDiv.nodeName.toUpperCase()=="DIV") {
var prevStyle = tempDiv.getAttribute('style');
tempDiv.setAttribute('style', prevStyle + ";" + strStyle);
}
}
}
}
}
}
}
}
}
self.onQueryFailed = function (sender, args) {
alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}
self.retrieveEventCategoryItems = function () {
var clientContext = self.ctx;
var oList = self.rootWeb.get_lists().getByTitle('Colour Calendar Mapping');
var camlQuery = new SP.CamlQuery();
camlQuery.set_viewXml(
"<View><Query><Where><Neq><FieldRef Name='PWEventCategory'/><Value Type='Text'></Value></Neq></Where></Query>" +
'<RowLimit>1000</RowLimit></View>'
);
self.collListItem = oList.getItems(camlQuery);
clientContext.load(self.collListItem);
clientContext.executeQueryAsync(
Function.createDelegate(self, self.onQuerySucceeded),
Function.createDelegate(self, self.onQueryFailed)
);
}
// retrieveEventCategoryItems();
self.initialize = function () {
self.EventCategoryArray = [];
self.ctx = new SP.ClientContext();
self.site = self.ctx.get_site();
self.rootWeb = self.site.get_rootWeb();
self.ctx.load(self.rootWeb);
//self.ctx.load(site);
self.retrieveEventCategoryItems();
}
}
I am unable to find the original instructions to setup the calendar to work with the code. Is there any way you can post them? thanks.
ReplyDeleteIf you aren't using the Planet Wilson Color Calendar, then you won't need this code.
DeleteJust google for color code SharePoint Calendar:
Here's a couple links:
http://www.sharepointjunkies.com/color-coded-calendar-sharepoint-online-using-javascript/
or
https://www.eimagine.com/sharepoint-color-coding-your-calendar-by-category-tutorial/