0

i am trying to call a function with parameters i got from CAML Query. But i almost always gets result of the last function call twice. please see the code below.

Here i use two parameters "01 ETHICAL AND PROFESSIONAL STANDARDS" and "04 CODE OF CONDUCT" but i only gets the output for the "04 CODE OF CONDUCT".

its gives me an error on the second call "The collection has not been initialized. It has not been requested or the request has not been executed. It may need to be explicitly requested"

Please see the below code.

<div id="divListItems"></div>


<script>
    var ul = document.createElement("ul");
    var linktemp;
    _spBodyOnLoadFunctionNames.push("MyFunction");

    function MyFunction() {
        retrieveListItems();
    }


    function retrieveListItems() {

        SubHeading('HUMAN RESOURCES');

    }






    function SubHeading(SubHeading) {


        var clientContext = new SP.ClientContext();

        var oList = clientContext.get_web().get_lists().getByTitle('Mega Menu');

        var camlQuery = new SP.CamlQuery();
        var ViewXml = "<View><Query><ViewFields><FieldRef Name='Main_x0020_Heading' /><FieldRef Name='Sub_x0020_Heading' /><FieldRef Name='Sub_x0020_Heading_x0020_Order' />" +
           "<FieldRef Name='Links' /><FieldRef Name='Link_x0020_URL' /><FieldRef Name='link_x0020_order' /></ViewFields><Where><Eq><FieldRef Name='Main_x0020_Heading' />" +
           "<Value Type='Text'>"+SubHeading+"</Value></Eq></Where></Query></View>";

        camlQuery.set_viewXml(ViewXml);
        this.collListItem = oList.getItems(camlQuery);

        clientContext.load(collListItem);
        clientContext.executeQueryAsync(
        Function.createDelegate(this, this.onQuerySucceeded1),
        Function.createDelegate(this, this.onQueryFailed1)
        );
    }


    function onQuerySucceeded1(sender, args) {

        var allValues = [];     
        var listItemInfo = '';


        var listItemEnumerator = collListItem.getEnumerator();
        var Description = "";
        var total = 0;
        var ul = document.createElement("ul");
                $("#divListItems").append(ul);
        while (listItemEnumerator.moveNext()) {

            var oListItem = listItemEnumerator.get_current();

            allValues.push(oListItem.get_item('Sub_x0020_Heading'));
        }
        var uniqueNames = [];
        $.each(allValues, function (i, el) {
            if ($.inArray(el, uniqueNames) === -1) uniqueNames.push(el);
        });
        links("01 ETHICAL AND PROFESSIONAL STANDARDS");
        links("04 CODE OF CONDUCT");
        for (var i = 0; i < uniqueNames.length; i++) {
           // alert(uniqueNames[i]);
            //links(uniqueNames[i]);
          //  links("04 CODE OF CONDUCT");

        }
    }

    function onQueryFailed1(sender, args) {
        alert('Request failed. ' + args.get_message() +
        '\n' + args.get_stackTrace());
    }



    function links(Subheading) {
        //alert(Subheading);
        var clientContext = new SP.ClientContext();

        var oList = clientContext.get_web().get_lists().getByTitle('Mega Menu');

        var camlQuery = new SP.CamlQuery();
        var ViewXml = "<View><Query><ViewFields><FieldRef Name='Main_x0020_Heading' /><FieldRef Name='Sub_x0020_Heading' /><FieldRef Name='Sub_x0020_Heading_x0020_Order' />" +
           "<FieldRef Name='Links' /><FieldRef Name='Link_x0020_URL' /><FieldRef Name='link_x0020_order' /></ViewFields><Where><Eq><FieldRef Name='Sub_x0020_Heading' />" +
           "<Value Type='Text'>" + Subheading + "</Value></Eq></Where></Query></View>";
        camlQuery.set_viewXml(ViewXml);
        this.collListItem = oList.getItems(camlQuery);
        //alert(link);
        clientContext.load(collListItem);
        clientContext.executeQueryAsync(
        Function.createDelegate(this, this.onQuerySucceeded2),
        Function.createDelegate(this, this.onQueryFailed2)
        );
    }


    function onQuerySucceeded2(sender, args) {

        var listItemInfo = '';

        var listItemEnumerator = this.collListItem.getEnumerator();
        var Description = "";
        var total = 0;
        var ul = document.createElement("ul");
        $("#divListItems").append(ul);
        while (listItemEnumerator.moveNext()) {

            var oListItem = listItemEnumerator.get_current();

            var li = document.createElement("li");

            var a = document.createElement("a");
            a.innerHTML = oListItem.get_item('Links');
            $(li).val(oListItem.get_item('Links'));
            li.appendChild(a);
            ul.appendChild(li);
        }

    }

    function onQueryFailed2(sender, args) {
        alert('Request failed. ' + args.get_message() +
        '\n' + args.get_stackTrace());
    }
  • Are all the internal names of the columns correct? – Abdul Hameed Mar 19 '18 at 12:18
  • yes it is all good. It work on the first call. i am only getting the result from the second call. i i comment either of links("04 CODE OF CONDUCT"); or links("01 ETHICAL AND PROFESSIONAL STANDARDS"); it works fine. I think it has something to with collListItem. i am calling multiple times to the Link() function will this have any effect to the collListItem ? – Rinu Mar 19 '18 at 12:26

Your Answer

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

Browse other questions tagged or ask your own question.