0

I have the below code which is finding a List on a Page and alerting the "Title" of each item in the list. Within the list I have a number of columns such as "Description" "Priority" etc, how do I return these column values?

<script type="text/javascript">  

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'));
                alert(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("Test List", "");
 }

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

</script>

1 Answer 1

0

You need to load the columns as below:

context.load(items,'Include(Title,Description,Priority)'); //specify internal names of columns in comma separated format

To check the value, logs value to console:

console.log(listEnumerator.get_current().get_item('Description'));

console.log(listEnumerator.get_current().get_item('Priority'));

So, you overall code would look like:

<script type="text/javascript">  

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,'Include(Title,Description,Priority)');
        context.executeQueryAsync(
        function () {
            var listEnumerator = items.getEnumerator();
            var i = 0;
            while (listEnumerator.moveNext()) {
                console.log(listEnumerator.get_current().get_item('Title'));

                alert(listEnumerator.get_current().get_item('Title'));

                console.log(listEnumerator.get_current().get_item('Description'));

                console.log(listEnumerator.get_current().get_item('Priority'));

                i++;
            }
            //alert("items retrieved: " + i);          
        },
        function (sender, args) { alert("error in inner request: " + args.get_message()); }
        );
    }

function getItems() {
    getItemsFromView("Test List", "");
 }

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

</script>

Reference - Include custom fields in context.load

10
  • Thanks, I am now getting the error: "error in inner request: Column 'Description' does not exist. It may have been deleted by another user" Commented Feb 27, 2017 at 11:02
  • check if Description column exists in the list and then use the internal column name. In case of tasks list, it is Body, so you can replace Description with Body Commented Feb 27, 2017 at 11:05
  • Ah yes it needed the internal column name - helpfully labelled "cp5g" Commented Feb 27, 2017 at 11:22
  • The "cp5g" Internal names are created if you add Fields using the Modern Experiences (where you only enter the Field DisplayName) still best practice is to add Fields in the List settings, where your first entered name will become the Internal Name (so you enter them without special characters and spaces, then after saving open the edit Field once more and set the correct DisplayName)... It was a pain... it still is a pain with Modern Experiences Commented Feb 27, 2017 at 11:33
  • I have another Question - This code works for returning lists on the current SharePoint page, I want to retrieve list items from a list in a sub-site, whats the best way to do this? Commented Feb 27, 2017 at 11:56

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.