locked
Customize Calendar Events using Javascript RRS feed

  • Question

  • Hi,

    I try to implement the solution from this POST:

    http://social.technet.microsoft.com/wiki/contents/articles/19755.sharepoint-customize-calendar-events-using-javascript.aspx

    But always it gives me error "itemToHide = GetItemsToHide($p0);". I tried everything but I do not have any result.

    Error Message:
    Reference uncaught Error: itemToHide is not defined

    What I want to achieve:
    In the end what I want to achieve is filtered through a column that I have on the list traveling by querystring. Will build a caml to bring me the result and hide the items that are not in that result.

    My Code:

    <script type="text/javascript">
        ExecuteOrDelayUntilScriptLoaded(CustomizeCalendarEvents, "SP.UI.ApplicationPages.Calendar.js");
        // This method customize calendar events. 
        function CustomizeCalendarEvents() {
            try {
                var _onItemsSucceed = SP.UI.ApplicationPages.CalendarStateHandler.prototype.onItemsSucceed;
                SP.UI.ApplicationPages.CalendarStateHandler.prototype.onItemsSucceed = function ($p0, $p1) {
    
                    // Get querystring parameter value
                    var fieldValue = GetQueryStringByParameter('Field1');
                   
    
                    // Get all items from calendar list which is having Field1 = fieldValue and store item id in itemCollection Array using ECMA/web service
                    itemIdCollection = GetItemsfromList(fieldValue);
    
                    // This function identifies item to hide on the page.
    
                    itemToHide = GetItemsToHide($p0);
    
                    // Remove selected items from array ($p0).
                    for (var count = 0; count < itemToHide.length; count++) {
                        for (var flag = 0 ; flag < $p0.length; flag++) {
                            if ($p0[flag].$12 == itemToHide[count]) {
                                removeItemByIndex($p0, flag);
                                break;
                            }
                        }
                    }
    
                    // Call this method to render events on the page. 
                    _onItemsSucceed.call(this, $p0, $p1);
                };
            }
            catch (err) {
                txt = "There was an error on this page.\n\n";
                txt += "Error description: " + err.message + "\n\n";
                txt += "Click OK to continue.\n\n";
                alert(txt);
            }
        }
        // This function adds items to hide in array.
        function GetItemsToHide($p0) {
            for (var i = 0; i < $p0.length; i++) {
                var itemId = $p0[i].$12;
                var result = IsItemExist(itemId, itemIdCollection);
                if (result == -1) {
                    itemToHide.push(itemId);
                }
            }
        }
    
        // This function removes item from array.
        function removeItemByIndex(arr, index) {
            arr.splice(index, 1);
        }
        // This function checks element exist in array or not.
        function IsItemExist(elem, array) {
            var len = array.length;
    
            for (var i = 0; i < len; i++) {
                if (array[i] == elem) {
                    return i;
                }
            }
            return -1;
        }
    
        function GetQueryStringByParameter(name) {
            name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
            var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
            results = regex.exec(location.search);
            return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
        }
    
        function GetItemsfromList(FieldValue) {
    
            var clientContext = new SP.ClientContext.get_current();
            var oList = clientContext.get_web().get_lists().getByTitle('AppointmentRequests');
    
            var camlQuery = new SP.CamlQuery();
            camlQuery.set_viewXml('<View><Query><Where><Eq><FieldRef Name=\'Title\'/><Value Type=\'Text\'>' + FieldValue + '</Value></Eq></Where></Query><RowLimit>100</RowLimit></View>');
            this.collListItem = oList.getItems(camlQuery);
    
            clientContext.load(collListItem);
    
            clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
    
            return collListItem;
    
        }
    
        function onQuerySucceeded(sender, args) {
    
            var listItemInfo = '';
    
            var listItemEnumerator = collListItem.getEnumerator();
    
            while (listItemEnumerator.moveNext()) {
                var oListItem = listItemEnumerator.get_current();
                listItemInfo += '\nID: ' + oListItem.get_id() +
                    '\nTitle: ' + oListItem.get_item('Title')
            }
    
            alert(listItemInfo.toString());
        }
    
        function onQueryFailed(sender, args) {
    
            alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
        }
    
    
    </script>

    Tuesday, August 11, 2015 2:37 PM