0

I am using the following code to get the available items from a list view.

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript">
    SP.SOD.executeFunc('sp.js', 'SP.ClientContext', function () {
    function getItemsFromView(listTitle, viewTitle) {
        var context = new SP.ClientContext.get_current();
        var list = context.get_web().get_lists().getByTitle(listTitle);
        var view = list.get_views().getByTitle(viewTitle);
        context.load(view);
        context.executeQueryAsync(
        function (sender, args) { getItemsFromList(listTitle, "<View><Query>" + view.get_viewQuery() + "</Query></View>") },
        function (sender, args) { alert("error: " + args.get_message()); }
        );
    }
    });
    SP.SOD.executeFunc('sp.js', 'SP.ClientContext', function () {
    function getItemsFromList(listTitle, queryText) {
        var context = new SP.ClientContext.get_current();
        var list = context.get_web().get_lists().getByTitle(listTitle);
        var query = new SP.CamlQuery();
        query.set_viewXml(queryText);
        var items = list.getItems(query);
        context.load(items);
        context.executeQueryAsync(
        function () {
            var listEnumerator = items.getEnumerator();
            var i = 0;
            while (listEnumerator.moveNext()) {
                i++;
            }
            alert("items retrieved: " + i);
        },
        function (sender, args) { alert("error in inner request: " + args.get_message()); }
        );
    }
    });

    getItemsFromView("Tasks", "All Items",
 function (items) {
     for (var i = 0; i < items.get_count() ; i++) {
         var item = items.get_item(i);
         console.log(item.get_item('Title'));
     }
 },
 function (sender, args) {
     console.log(args.get_message())
 });

</script>

The code is not working: nothing happens and I get an error in the debug console stating

getItemsFromView("Tasks", "All Items") undefined.

I have wrapped the two functions by SP.SOD.executeFunc('sp.js', 'SP.ClientContext', function (), because I was getting a SP.ClientContext.get_current() is undefined error. Can anyone help me?

2 Answers 2

2

I modified your code. Here is the working example. Note that the name of the view in my Tasks list is "All Tasks"

function getItemsFromView(listTitle, viewTitle) {
        var context = new SP.ClientContext.get_current();
        var list = context.get_web().get_lists().getByTitle(listTitle);
        var view = list.get_views().getByTitle(viewTitle);
        context.load(view);
        context.executeQueryAsync(
        function (sender, args) { getItemsFromList(listTitle, "<View><Query>" + view.get_viewQuery() + "</Query></View>") },
        function (sender, args) { alert("error: " + args.get_message()); }
        );
    }
function getItemsFromList(listTitle, queryText) {
        var context = new SP.ClientContext.get_current();
        var list = context.get_web().get_lists().getByTitle(listTitle);
        var query = new SP.CamlQuery();
        query.set_viewXml(queryText);
        var items = list.getItems(query);
        context.load(items);
        context.executeQueryAsync(
        function () {
            var listEnumerator = items.getEnumerator();
            var i = 0;
            while (listEnumerator.moveNext()) {
                console.log(listEnumerator.get_current().get_item('Title'));
                i++;
            }
            alert("items retrieved: " + i);
        },
        function (sender, args) { alert("error in inner request: " + args.get_message()); }
        );
    }

function getItems() {
    getItemsFromView("Tasks", "All Tasks");
 }

 SP.SOD.executeFunc('sp.js', 'SP.ClientContext', getItems);
0
1

The problem is that your functions don't exist when the line at the bottom calling getItemsFromView() executes as the code is waiting for sp.js to load.

Instead remove the sp.sod.executeFunc from around your functions getItemsFromView and getItemsFromList and wrap it around your code at the bottom that way you only have to write that code once and your functions will be declared once Sharepoint JSOM libraries have been loaded.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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