0

I have a Visual Studio 2013 solution and I have been successfully able to get list items from the host web using the following JavaScript code:

    var context = SP.ClientContext.get_current();
    var factory;
    var appContextSite;
    var mylist;
    context = new SP.ClientContext(appweburl);
    factory = new SP.ProxyWebRequestExecutorFactory(appweburl);
    context.set_webRequestExecutorFactory(factory);
    appContextSite = new SP.AppContextSite(context, hostweburl);
    this.web = appContextSite.get_web();
    mylist = this.web.get_lists().getByTitle('My List');
    var camlQuery = new SP.CamlQuery();
    camlQuery.set_viewXml('<Query><OrderBy><FieldRef Name=\'ID\'/></OrderBy></Query>');
    var collListItem = mylist.getItems(camlQuery);
    context.load(mylist);
    context.load(collListItem);
    context.executeQueryAsync(
     Function.createDelegate(this, success),
     Function.createDelegate(this, error)
    );

However I have now created an External Content Type inside of the app and need to get information from the list that has been created:

External Content Types

I have tried modifying my code and having a look through MSDN but so far there appears to be very limited information on this.

I would appreciated any help and guidance that can be provided for this.

1
  • It would help if you provided the error you are encountering when attempting to access the information, in general, access to external content in lists should work the same as for normal list items. It would also be helpful if you provided the code in the success and error callbacks since in your comment to another answer you say you're encountering a not initialized error
    – John-M
    Sep 7, 2015 at 17:38

2 Answers 2

3

You can use below code to start with:

   var context = SP.ClientContext.get_current();//Get current app web context
    var mylist;
    this.web = context.get_web();
    mylist = this.web.get_lists().getByTitle('My List');
    var camlQuery = new SP.CamlQuery();
    camlQuery.set_viewXml('<Query><OrderBy><FieldRef Name=\'ID\'/></OrderBy></Query>');
    var collListItem = mylist.getItems(camlQuery);
    context.load(mylist);
    context.load(collListItem);
    context.executeQueryAsync(
     Function.createDelegate(this, success),
     Function.createDelegate(this, error)
    );
3
  • I currently get an error in the console: "The property or field has not been initialized. It has not been requested or the request has not been executed. It may need to be explicitly requested" I have updated my code to be the same as yours now. Any more help please?
    – Adam13Hylo
    Sep 7, 2015 at 15:39
  • You are trying to use some field of the list which is not loaded. Some fields needs to be explicitly loaded before using it.
    – Unnie
    Sep 7, 2015 at 21:16
  • Thank you very much, I was missing the "<Viewfields>" part of the query and this now works succesfully.
    – Adam13Hylo
    Sep 8, 2015 at 8:10
0
function queryListItems() {  
         var clientContext = new SP.ClientContext.get_current();
         var oList = clientContext.get_web().get_lists().getByTitle('My List');
         var camlQuery = new SP.CamlQuery();
         camlQuery.set_viewXml('<View><Query><Where><Leq><FieldRef Name=\'ID\'/><Value Type=\'Number\'>100</Value></Leq></Where></Query></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 x=document.getElementById("country");
    var listItemEnumerator = collListItem.getEnumerator();
    while (listItemEnumerator.moveNext()) {
        var oListItem = listItemEnumerator.get_current();
        //listItemInfo += '\nID: ' + oListItem.get_id() + '\nTitle: ' + oListItem.get_item('Title');

            var row=x.insertRow(1);
            var cell1=row.insertCell(0);
            var cell2=row.insertCell(1);
            var cell3=row.insertCell(2); 
            cell1.innerHTML=oListItem.get_id();
            cell2.innerHTML=oListItem.get_item('listvalue1');
            cell3.innerHTML=oListItem.get_item('listvalue2');
    }
    alert(cell2.value);
    alert(x.rows.length); 
    //alert(listItemInfo.toString());

}

function onQueryFailed(sender, args) {

    alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}
1
  • Can you explain your answer? Why/how does it answer the question? Solutions without explanations do not provide a learning experience.
    – wjervis
    May 27, 2016 at 12:36

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.