0

I have a client context not initialized error and I have no clue how to fix it I feel like I have tried everything. Would appreciate some help Here is my code :

Code that gets the list:

var SFW = window.SFW || {};
var NBSFeedbackEditInbox = SFW.NBSFeedbackEditInbox || {};

NBSFeedbackEditInbox.FeedbackDetails = (function () {

    editdeleted = function (getid, edititem, deleterecordtext, deleterecordid) {
        NBSFeedbackEditInbox.siteURL = _spPageContextInfo.siteAbsoluteUrl + '/Community/News-and-feedback/Feedback';
        var cContext = new SP.ClientContext(NBSFeedbackEditInbox.siteURL);
        var oList = cContext.get_web().get_lists().getByTitle('Feedback Items');

        getid = getid.substring(getid.lastIndexOf('#') + 1);
        getid = Math.round(getid);

        oListItem = oList.getItemById(getid);
        if (edititem == 'feedback') {

            oListItem.set_item('RequireFeedback', false);
        }
        if (edititem == 'delete') {


            if (deleterecordtext == 'Sent' || deleterecordtext == 'Inbox') {

                oListItem.set_item('IsDeleted', 'Both');
            }

            if (deleterecordid == 'sentfordelete' && deleterecordtext == 'No') {

                oListItem.set_item('IsDeleted', 'Sent');
            }
            if (deleterecordid == 'inboxfordelete' && deleterecordtext == 'No') {

                oListItem.set_item('IsDeleted', 'Inbox');
            }
        }
        oListItem.update();

        cContext.executeQueryAsync(onQuerySucceeded, onQueryFailed);

    }
    onQuerySucceeded = function () {


    }

    onQueryFailed = function (sender, args) {


    }

    return {
        editdeleted: editdeleted
    }

})()

NBSFeedbackEditInbox.FeedbackRemove = (function () {
    var listItemEnumeratorlistFeedback,
    collListItems;
    var cContext;
    removemessage = function (getid) {

        NBSFeedbackEditInbox.siteURL = _spPageContextInfo.siteAbsoluteUrl + '/Community/News-and-feedback/Feedback';
        cContext = new SP.ClientContext(NBSFeedbackEditInbox.siteURL);
        var oList = cContext.get_web().get_lists().getByTitle('Feedback Items');

        var querystring = jQuery('#createdby').text();

        for (var i = 0; i < getid.length; i++) {


            getid[i] = getid[i].substring(getid[i].lastIndexOf('#') + 1);
            getid[i] = Math.round(getid[i]);

        }

        if (getid.length != 0) {
            var query = SP.CamlQuery.createAllItemsQuery();
            query.set_viewXml("<View Scope='RecursiveAll'><Query><Where><Contains><FieldRef Name='Author'/><Value Type='Text'>" + querystring + "</Value></Contains></Where></Query></View>");
            collListItems = oList.getItems(query);


            cContext.load(collListItems, 'Include(Title, ID,IsDeleted,RequireFeedback)');

            cContext.executeQueryAsync(onQuerySucceeded, onQueryFailed);
        }

    }
    onQuerySucceeded = function () {
        var itemTitle = [];
        var sentnumber = 0;
        var inboxnumber = 0;
        listItemEnumeratorlistFeedback = collListItems.getEnumerator();
        while (listItemEnumeratorlistFeedback.moveNext()) {

            feedbackListItem = listItemEnumeratorlistFeedback.get_current();

            if (feedbackListItem.get_item('IsDeleted') == 'Sent' || feedbackListItem.get_item('RequireFeedback') == false || feedbackListItem.get_item('IsDeleted') == 'Both') {
                jQuery("div[title='" + (feedbackListItem.get_item('Title')) + "sent']").parent().parent().parent().parent().css('display', 'none');

                var sentlength = jQuery('.nbsallmessagecountsent').filter(function () { return $(this).css("display") != "none" }).length;


                jQuery("#nbsmessagesentcount").text('Showing ' + sentlength + ' of ' + sentlength + ' conversations');
                if (sentlength <= 0) {
                    jQuery('#sentcounters').addClass('nbsmessageremoval');
                } else {
                    jQuery('#sentcounters').removeClass('nbsmessageremoval');
                }



            }

            if (feedbackListItem.get_item('IsDeleted') == 'Inbox' || feedbackListItem.get_item('IsDeleted') == 'Both') {

                jQuery("div[title='" + (feedbackListItem.get_item('Title')) + "inbox']").parent().parent().parent().parent().css('display', 'none');
                var inboxlength = jQuery('.allmessagecount').filter(function () { return $(this).css("display") != "none" }).length;
                jQuery("#nbsmessageinboxcount").text('Showing ' + inboxlength + ' of ' + inboxlength + ' conversations');

                if (inboxlength == 0) {
                    jQuery('#nbsnewmessages').text('');
                } else {
                    jQuery('#nbsnewmessages').text('(' + inboxlength + ')');
                }
                if (inboxlength <= 0) {
                    jQuery('#nbsdeletemessage').addClass('nbsmessageremoval');
                } else {
                    jQuery('#nbsdeletemessage').removeClass('nbsmessageremoval');
                }

            }
        }

    }

    onQueryFailed = function (sender, args) {

    }

    return {
        removemessage: removemessage
    }

})()

Code that calls the method:

jQuery(document).ready(function () {



        if (!Array.prototype.indexOf) {
            Array.prototype.indexOf = function (elt /*, from*/) {
                var len = this.length >>> 0;

                var from = Number(arguments[1]) || 0;
                from = (from < 0)
                     ? Math.ceil(from)
                     : Math.floor(from);
                if (from < 0)
                    from += len;

                for (; from < len; from++) {
                    if (from in this &&
                        this[from] === elt)
                        return from;
                }
                return -1;
            };
        }
        var itemid = [];
        jQuery("div#NBSinboxsent").find(".itemid").each(function () {
            itemid.push(jQuery(this).text());
        });

        var inboxid = [];
        jQuery("div#NBSinboxsent").find(".inboxid").each(function () {
            inboxid.push(jQuery(this).text());
        });
        SP.SOD.executeOrDelayUntilScriptLoaded(someSimpleWrapper, "sp.js");

function someSimpleWrapper(){

  NBSFeedbackEditInbox.FeedbackRemove.removemessage(itemid);



  NBSFeedbackEditInbox.FeedbackRemove.removemessage(inboxid);

}

        var countinbox = 0;
        var count = 0;
        var checkedidfeedback = [];
        var checkedidsentdelete = [];
        var checkedidinboxdelete = [];

        var sentlength = jQuery('.nbsforthechopsent').filter(function () { return $(this).css("display") != "none" }).length;
        var inboxlength = jQuery('.nbsforthechop').filter(function () { return $(this).css("display") != "none" }).length;
        var nbsconversationbetween = jQuery('#conversationbetweenreply').text();
        nbsconversationbetween = nbsconversationbetween.replace(';', ' and ');

        jQuery('#nbsnameofuser').html('<h2 style="padding-bottom:15px;">' + jQuery('.CreatedByreply').attr('title') + '</h2>');
        jQuery("#nbsmessageinboxcount").text('Showing ' + inboxlength + ' of ' + inboxlength + ' conversations');
        jQuery("#nbsmessagesentcount").text('Showing ' + sentlength + ' of ' + sentlength + ' conversations');
        if (jQuery('.nbs-newmessage').length > 0) {
            jQuery('#nbsnewmessages').text(' (' + jQuery('.nbs-newmessage').length + ')');
        }
        if (inboxlength <= 0) {
            jQuery('#nbsdeletemessage').addClass('nbsmessageremoval');
        } else {
            jQuery('#nbsdeletemessage').removeClass('nbsmessageremoval');
        }
        if (sentlength <= 0) {
            jQuery('#sentcounters').addClass('nbsmessageremoval');
        } else {
            jQuery('#sentcounters').removeClass('nbsmessageremoval');
        }



        //withdraw single feedback
        jQuery('.removefeedback').click(function () {
            jQuery(this).parent().parent().css('display', 'none');
            jQuery(this).parent().parent().parent().css('display', 'none');
            var deletecheck = jQuery(this).parent().parent().attr('id');
            deletecheck = deletecheck + 'checkbox';
            jQuery('#' + deletecheck).css('display', 'none');

            var getid = jQuery(this).parent().last().text();

            NBSFeedbackEditInbox.FeedbackDetails.editdeleted(getid, 'feedback', null, null);

            if (count > 0) {
                count = count - 1;
            } else {
                count = 0;
            }
            if (sentlength <= 0) {
                jQuery('#sentcounters').addClass('nbsmessageremoval');
            }
            if (count <= 0) {
                jQuery('.nbsdeletecountsent').text('');

            } else {
                jQuery('.nbsdeletecountsent').text('(' + count + ')');
            }
        });

        //count inbox checkbox
        jQuery('.nbsforthechop').change(function () {

            var deleteid = jQuery(this).attr('id');

            deleteid = deleteid.replace('checkbox', '');

            if (jQuery(this).is(':checked') == true) {
                checkedidinboxdelete.push(deleteid);
                jQuery('#' + deleteid).addClass('coloronchecked');
                countinbox++;
            } else {
                var i = checkedidinboxdelete.indexOf(deleteid);
                if (i != -1) {
                    checkedidinboxdelete.splice(i, 1);
                }

                jQuery('#' + deleteid).removeClass('coloronchecked');
                countinbox = countinbox - 1;

            }
            if (countinbox <= 0) {
                jQuery('#nbsdeletecount').text('');

            } else {
                jQuery('#nbsdeletecount').text('(' + countinbox + ')');
            }

        });

        //count sent checkbox
        jQuery('.nbsforthechopsent').change(function () {
            var deleteid = jQuery(this).attr('id');
            deleteid = deleteid.replace('checkbox', '');

            if (jQuery(this).is(':checked') == true) {
                jQuery('#' + deleteid).addClass('coloronchecked');
                checkedidfeedback.push(deleteid);
                checkedidsentdelete.push(deleteid);
                count++;
            } else {
                var i = checkedidsentdelete.indexOf(deleteid);
                if (i != -1) {
                    checkedidsentdelete.splice(i, 1);
                }
                var i = checkedidfeedback.indexOf(deleteid);
                if (i != -1) {
                    checkedidfeedback.splice(i, 1);
                }

                jQuery('#' + deleteid).removeClass('coloronchecked');
                count = count - 1;

            }
            if (count <= 0) {
                jQuery('.nbsdeletecountsent').text('');

            } else {
                jQuery('.nbsdeletecountsent').text('(' + count + ')');
            }
        });

        //checkbox withdraw feedback
        jQuery('.withdrawfeedback').click(function () {
            if (checkedidfeedback.length != 0) {
                for (var i = 0; i < checkedidfeedback.length; i++) {
                    jQuery('#' + checkedidfeedback[i]).css('display', 'none');
                    jQuery('#' + checkedidfeedback[i]).parent().css('display', 'none');
                    deleteid = checkedidfeedback[i] + 'checkbox';
                    jQuery('#' + deleteid).css('display', 'none');
                    var getid = (jQuery('#' + checkedidfeedback[i] + ' div').last().text());
                    NBSFeedbackEditInbox.FeedbackDetails.editdeleted(getid, 'feedback', null, null);

                }
                count = 0;
                jQuery('.nbsdeletecountsent').text('');
                jQuery("#nbsmessagesentcount").text('Showing ' + sentlength + ' of ' + sentlength + ' conversations');
            }
            checkedidfeedback = [];
            checkedidsentdelete = [];
        });

        //checkbox delete inbox message
        jQuery('.deleteinbox').click(function () {
            if (checkedidinboxdelete.length != 0) {

                for (var i = 0; i < checkedidinboxdelete.length; i++) {
                    jQuery('#' + checkedidinboxdelete[i]).css('display', 'none');
                    jQuery('#' + checkedidinboxdelete[i]).parent().css('display', 'none');

                    deleteid = checkedidinboxdelete[i] + 'checkbox';
                    jQuery('#' + deleteid).css('display', 'none');
                    var getid = (jQuery('#' + checkedidinboxdelete[i] + ' div').last().text());

                    var deleterecordtext = (jQuery('#' + checkedidinboxdelete[i] + ' small').last().text());
                    var deleterecordid = (jQuery('#' + checkedidinboxdelete[i] + ' small').last().attr('id'));

                    NBSFeedbackEditInbox.FeedbackDetails.editdeleted(getid, 'delete', deleterecordtext, deleterecordid);
                    NBSFeedbackEditInbox.FeedbackRemove.removemessage;
                }
                countinbox = 0;

                jQuery('#nbsdeletecount').text('');

            }
            checkedidinboxdelete = [];
        });

        //checkbox delete sent message
        jQuery('.deletesent').click(function () {

            if (checkedidsentdelete.length != 0) {
                for (var i = 0; i < checkedidsentdelete.length; i++) {
                    jQuery('#' + checkedidsentdelete[i]).css('display', 'none');
                    jQuery('#' + checkedidfeedback[i]).parent().css('display', 'none');

                    deleteid = checkedidsentdelete[i] + 'checkbox';
                    jQuery('#' + deleteid).css('display', 'none');
                    var getid = (jQuery('#' + checkedidfeedback[i] + ' div').last().text());

                    var deleterecordtext = (jQuery('#' + checkedidfeedback[i] + ' small').last().text());
                    var deleterecordid = (jQuery('#' + checkedidfeedback[i] + ' small').last().attr('id'));


                    NBSFeedbackEditInbox.FeedbackDetails.editdeleted(getid, 'delete', deleterecordtext, deleterecordid);

                }
                count = 0;
                jQuery('.nbsdeletecountsent').text('');
            }
            checkedidsentdelete = [];
            checkedidfeedback = [];
        });




});

I don't know what else to try, it's getting really frustrating now

  • 1
    Try SP.SOD.executeOrDelayUntilScriptLoaded("sp.js",someSimpleWrapper ); – Amal Hashim Nov 12 '14 at 13:39
  • Did you include <script type="text/javascript" src="/_layouts/15/sp.js"></script> ? – Nk SP Nov 12 '14 at 13:39
  • I get in init.js Object doesn't support property tolowercase for SP.SOD.executeOrDelayUntilScriptLoaded("sp.js",someSimpleWrapper ); – Asad Nov 12 '14 at 13:47
  • I have not touched init.js either – Asad Nov 12 '14 at 13:49
  • had to add SP.ClientContext to it too Amal was correct, can put as an answer so I can mark it? – Asad Nov 12 '14 at 13:52
1

You need to invoke the function as follows

SP.SOD.executeOrDelayUntilScriptLoaded("sp.js","sp.ClientContext.js",someSimpleWrapper );
  • I now get Unable to get property 'getEnumerator' of undefined or null reference – Asad Nov 12 '14 at 15:19
0

you can refer the below code:

Function Definition something as below:

function functionName()
{
      try {
          var ListName = "Demo";
          var context = new SP.ClientContext.get_current(); // the current context is taken by default here
            //you can also create a particular site context as follows
            //var context = new SP.ClientContext(siteURL);
            var lstObject = context.get_web().get_lists().getByTitle(ListName);
            this.lstObjectItem = lstObject.getItemById(ID);
            lstObjectItem.deleteObject();
            }

            catch(exception){var message = exception.message;alert('Exception:'+message);}
            context.executeQueryAsync(Function.createDelegate(this, this.onSuccess),
            Function.createDelegate(this, this.onFailure));
        }
}

Try calling your function using below code:

SP.SOD.executeFunc('sp.js', null, function(){
      functionName()
    })
  • I now get Unable to get property 'getEnumerator' of undefined or null reference – Asad Nov 12 '14 at 14:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.