1

Hi guys I'm new to SharePoint dev and working on a project to retrieve list items. I have followed the instructions from http://msdn.microsoft.com/en-us/library/office/ee539350(v=office.14).aspx and made sure to add my javascript file in the html source of my content editor but still don't get the expected results. I've looked in my developer console for any errors in my JavaScript but nothing seems wrong. Any advice on this will be great. Thanks.

var siteUrl = '/Lists/My_x0020_Rooms';

SP.SOD.ExecuteOrDelayUntilScriptLoaded(retrieveListItems, "sp.js");

function retrieveListItems() {
    var clientContext = new SP.ClientContext(siteUrl);
    var oList = clientContext.get_web().get_lists().getByTitle('My_x0020_Rooms');    

    var camlQuery = new SP.CamlQuery();
    camlQuery.set_viewXml('<View><Query><Where><eq>' + 
        '<FieldRef Name=\'Building\'/><Value Type=\'Choice\'>Headquarters_x0020_Building</Value>' + 
        '</eq></Where></Query><RowLimit>50</RowLimit></View>');

    this.collListItem = oList.getItems(camlQuery);

    clientContext.load(collListItem);

    clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));        
}

function onQuerySucceeded(sender, args) {
    var listItemInfo = '';
    var listItemEnumerator = collListItem.getEnumerator();

    while (listItemEnumerator.moveNext()) {
        var oListItem = listItemEnumerator.get_current();
        listItemInfo += '\nBuilding: ' + oList.get_item('Building');
    }

    alert(listItemInfo.toString());

    document.getElementById('queryResults').innerHTML=listItemInfo.toString();
}

function onQueryFailed(sender, args) {
    alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}
5
  • I don't think the text for the choice value needs to have the space replaced with _x0020_ in your query. And <eq>/</eq> should be <Eq>/</Eq>.
    – wjervis
    Apr 3, 2014 at 19:25
  • Thanks @wjervis I made that change but still not working. I had the "x0020" in there after reading some posts about how the spaces could cause errors. Apr 3, 2014 at 19:43
  • Spaces in internal names for fields/lists can cause issues. I don't think it matters for this though. What exactly is the issue? Are no items getting returned, or are you not getting the value for "Building"?
    – wjervis
    Apr 3, 2014 at 19:51
  • @wjervis The issue is the former - no items are getting returned. Apr 3, 2014 at 19:58
  • As Aveenav said, you need to point the url to the correct one, since you're not on the same site as the list. And I'm certain the value in the query should use a space, not _x0020_. The url might use %20 instead of the unicode character code.
    – wjervis
    Apr 4, 2014 at 0:03

4 Answers 4

2

I doubt your list title is My_x0020_Rooms

SP.ListCollection.getByTitle(strListName) uses the list title. My guess is that you have replaced the space with _x0020_ like you do with InternalNames of fields. So try changing it to:

var oList = clientContext.get_web().get_lists().getByTitle('My Rooms'); 

Edit:

You have done the same with siteUrl, that should not be the URL to the list, but the site. If you are running the script from the same site as your list is in, you could use:

var clientContext = SP.ClientContext.get_current(); 

If not, use it as described on MSDN:

var object = new SP.ClientContext(serverRelativeUrlOrFullUrl)

serverRelativeUrlOrFullUrl The server-relative URL that starts with / or a full URL that starts with http:// or https://.

5
  • Thanks Anders. I made the change to var clientContext = SP.ClientContext.get_current(); as you suggested but still not getting results. Just to be sure, is the Name of the List the same as the Title in SharePoint? Thanks. Apr 3, 2014 at 19:47
  • getByTitle uses display name of the list, not internal name.
    – wjervis
    Apr 3, 2014 at 19:52
  • Hi guys just to make an update to this question, the site where I'm trying to get my list is a different one from where I'm trying to display my list. Does this affect how my function should be written? Apr 3, 2014 at 20:25
  • Provided that the user has at least Read permission, you can read the list from any site collection or sub site. You'd need to change your site Url. For ex. var siteUrl = '/sitecollectionname/nameOfTheSite';
    – Aveenav
    Apr 3, 2014 at 21:51
  • @Aveenav I actually have Admin access to the site. Apr 4, 2014 at 15:26
0

This script seems to be what I need too, but get errors aswell. Modified it to incorporate the suggested errors as well as to suit my need

     $(document).ready(function retrieveListItems() {
    JSRequest.EnsureSetup();
    var school_id = JSRequest.QueryString["School_id"]; 
    var camlQuery = new SP.CamlQuery();   
    var clientContext = SP.ClientContext.get_current();
    var oList = clientContext.get_web().get_lists().getByTitle('Contacts');

    camlQuery.set_viewXml("<View><Query><Where><eq> <FieldRef Name='school_id'/><Value Type='Text'>" + school_id + "</Value></eq>" +
    "</Where></Query><RowLimit>100</RowLimit></View>");

    var collListItem = oList.getItems(camlQuery);
    this.collListItem = oList.getItems(camlQuery);

    clientContext.load(collListItem);
    clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));        
});


function onQuerySucceeded(sender, args) {
    var listItemInfo = '';
    var listItemEnumerator = collListItem.getEnumerator();
    while (listItemEnumerator.moveNext()) {
        var oListItem = listItemEnumerator.get_current();
        listItemInfo += '\nContact: ' + oList.get_item('Contact');
    }
    alert(listItemInfo.toString());
    document.getElementById('queryResults').innerHTML=listItemInfo.toString();
}

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

However when running it, I get following error

Uncaught TypeError: Cannot read property 'apply' of undefined

clicking the link to the source brings me to

// MicrosoftAjax.js

(wtf) What might cause an error in an external library like Ajax?

3
  • have you tried using the chrome developer tools to see where or which line this error is occurring at. You could also throw in a few console logs or alerts to see where the code actually stops running and throws the error. May 21, 2015 at 13:49
  • Yes, when I step out of the first function ending in this: clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed)); }); it then steps into Jquery.js and then MicrosoftAjax.js where after a number of function calls, it returns the error Uncaught TypeError: Cannot read property 'apply' of undefined I can't read it though as the script is minimalized to 3 huge lines.
    – Essah
    May 21, 2015 at 14:24
  • This does not give an answer. To critique please attain 50 reputations and get eligible for commenting everywhere. Or else ask your own question.
    – Asad Refai
    Sep 30, 2015 at 5:15
0

Error is in below part of the code inside success :

while (listItemEnumerator.moveNext()) {
    var oListItem = listItemEnumerator.get_current();
    listItemInfo += '\nBuilding: ' + **oList.get_item('Building');**
}

Replace listItemInfo as below :

listItemInfo += '\nBuilding: ' + **oListItem.get_item('Building');**
-3

I have found this Client framework which helps a lot when you create a CAML query on client side.

Even if you don't want to use this Script for CAML, It has also a Plugin for Chrome where you can test the final CAML Query over the list page in your browser.

https://camljs.codeplex.com/

1
  • 1
    I removed the link to your site. It does not contribute to the answer.
    – wjervis
    May 1, 2015 at 13:31

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.