locked
Calendar view filter RRS feed

  • Question

  • Hi,

    I have a requirement to filter my calendar view based on a value which is either coming from a control or from query string.

    I need this solution for office 365 as well as SharePoint 2013.

    Please let me know the solution approach if you have faced such scenario earlier.

    Regards:

    Sanjay

    Monday, February 13, 2017 11:13 AM

Answers

  • Hi,

    Try to check the solution of below thread would help.

    http://sharepoint.stackexchange.com/questions/178027/filter-sharepoint-calender-by-category-using-javascript-jquery-ootb

    <style type="text/css">
        .ms-cal-nodataMid, .ms-cal-nodataBtm2 {
            background-color: #FFFFFF;
        }
    </style>
    <script type="text/javascript" src="/test/English/Javascript/jquery-1.3.2.min.js"></script>
    <script type="text/javascript" src="/test/English/Javascript/interaction.js"></script>
    <script type="text/javascript" src="/test/English/Javascript/stringBuffer.js"></script>
    <script type="text/javascript">
        // Set variables
        listGuid = 'FilterCalendarView'; // 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("ddlCategory"); // 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) {
            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) {
            $(".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'>&nbsp;</td>");
                        }
                        thisParentTd.replaceWith("<td class='ms-cal-workitem'>&nbsp;</td>");
                    } else {
                        thisParentTd.html('&nbsp;');
                    }
                }
            });
            // Show calendar view after it has been filtered
            $(".ms-cal-gempty").show();
        }
    
    </script>

    Best Regards,

    Lee


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    Tuesday, February 14, 2017 2:28 AM

All replies

  • Hi,

    Try to check the solution of below thread would help.

    http://sharepoint.stackexchange.com/questions/178027/filter-sharepoint-calender-by-category-using-javascript-jquery-ootb

    <style type="text/css">
        .ms-cal-nodataMid, .ms-cal-nodataBtm2 {
            background-color: #FFFFFF;
        }
    </style>
    <script type="text/javascript" src="/test/English/Javascript/jquery-1.3.2.min.js"></script>
    <script type="text/javascript" src="/test/English/Javascript/interaction.js"></script>
    <script type="text/javascript" src="/test/English/Javascript/stringBuffer.js"></script>
    <script type="text/javascript">
        // Set variables
        listGuid = 'FilterCalendarView'; // 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("ddlCategory"); // 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) {
            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) {
            $(".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'>&nbsp;</td>");
                        }
                        thisParentTd.replaceWith("<td class='ms-cal-workitem'>&nbsp;</td>");
                    } else {
                        thisParentTd.html('&nbsp;');
                    }
                }
            });
            // Show calendar view after it has been filtered
            $(".ms-cal-gempty").show();
        }
    
    </script>

    Best Regards,

    Lee


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    Tuesday, February 14, 2017 2:28 AM
  • Hi,

    Thanks you for reply.

    I am actually looking for a requirement which is mentioned in below link. Can you please suggest something on that.

    https://social.msdn.microsoft.com/Forums/office/en-US/ae1dc02d-06a2-46b8-bfcd-ccac470b0946/need-solution-approach-for-my-requirement-with-least-customization?forum=appsforsharepoint


    Monday, February 20, 2017 5:40 AM
  • Hi,

    As our engineer had provided feedback in the thread you mentioned, you could check the solution suggested by this engineer.

    Let’s focus on the issue in that thread as the requirement is more clear there.

    For providing better support, I would suggest you post same requirement/issue in same thread so we/others could follow asap.

    Thanks for your understanding.

    Best Regards,

    Lee


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    Tuesday, February 21, 2017 8:29 AM