Here i am working with SharePoint Calendar. I came up with the requirement where i need to filter the Calendar view by Category. Here i am entering the custom categories.
Here is my approach snap:
I was able to filter the Calender View by Month/Date/Week with the help of this post Changing view of Calendar
But for filtering Calendar based on custom category i am not able to get any proper approach or code.
If any one of you have gone through the same requirement then please suggest me the approach or solution.
My working environment is SharePoint Online and i want to avoid custom C# solutions.
Based on the answer given by @Hardik I kept an CEWP and kept the following code
<html>
<head>
<title></title>
<style type="text/css">
.ms-cal-nodataMid, .ms-cal-nodataBtm2 {
background-color: #FFFFFF;
}
</style>
<script type="text/javascript" src="DocumentLibraryUrl/interaction.js"></script>
<script type="text/javascript" src="DocumentLibraryUrl/jquery-1.9.1.min.js"></script>
<script type="text/javascript" src="DocumentLibraryUrl/stringBuffer.js"></script>
<script type="text/javascript">
// Set variables
function filterCode()
{
listGuid = 'Company Calender'; // Use list Name or List Guid
listBaseUrl = L_Menu_BaseUrl;
arrOfFieldInternalNames = ['Category']; //Put the internal name of the field "Category" here.
// Hide calendar view until it is filtered
$(".ms-cal-gempty").hide();
// Get the UserInfo
var e = document.getElementById("ddlFilter"); // Change the dropdown ID here.
var category = e.options[e.selectedIndex].value;
// Find all "relevant" items
var myItems = getMyItemsID(category, arrOfFieldInternalNames);
// Filter view
filterCalendarView(myItems);
}
function getMyItemsID(find, findInArr) {
debugger;
wsBaseUrl = listBaseUrl + '/_vti_bin/';
var query = "";
$.each(findInArr, function (i, fin) {
query += "<Eq><FieldRef Name='" + fin + "' /><Value Type='Text'>" + find + "</Value></Eq>";
if (i > 0) query = "<Or>" + query + "</Or>";
});
query = "<Where>" + query + "</Where>";
var arrToReturn = [];
res = queryItems(listGuid, query, ['ID']);
$.each(res.items, function (i, item) {
arrToReturn.push(item.ID);
});
return arrToReturn;
}
function filterCalendarView(arrOfIDs) {
debugger;
$(".ms-cal-gempty a[href*='DispForm.aspx?ID=']").each(function () {
var currID = $(this).attr('href').match(/ID=(d+)/);
if ($.inArray(currID[1], arrOfIDs) == -1) {
// remove
var thisParentTd = $(this).parents('table:first').parents('td:first');
var colspan = thisParentTd.attr('colspan');
if (colspan > 1) {
for (i = 1; i < colspan; i++) {
thisParentTd.after("<td class='ms-cal-workitem'> </td>");
}
thisParentTd.replaceWith("<td class='ms-cal-workitem'> </td>");
} else {
thisParentTd.html(' ');
}
}
});
// Show calendar view after it has been filtered
$(".ms-cal-gempty").show();
}
</script>
</head>
<body>
<select onchange="filterCode()" id="ddlFilter">
<option value="test">test</option>
<option value="test1">test1</option>
<option value="test2">test2</option>
</select>
</body>
</html>
Still code has no effect. Console does not shows any error log.