-3
function listAllCategories() {
var ctx = new SP.ClientContext(appWebUrl);
var appCtxSite = new SP.AppContextSite(ctx, hostWebUrl);

var web = appCtxSite.get_web(); //Get the Web 

var list = web.get_lists().getByTitle("NewsList"); //Get the List

var query = new SP.CamlQuery(); //The Query object. This is used to query for data in the List

query.set_viewXml('<View><Query><OrderBy><FieldRef Name=\'Article Date\' Ascending=\'False\'/></OrderBy></Query>' + '<RowLimit>2</RowLimit></View>');


var items = list.getItems(query);

ctx.load(list); //Retrieves the properties of a client object from the server.
ctx.load(items);

var table = $("#tblcategories");
var innerHtml = "<table><p><h3></h3></p><tr><td></td><td><p></p><p></p></td></tr></table>";

//Execute the Query Asynchronously
ctx.executeQueryAsync(
    Function.createDelegate(this, function () {
        var itemInfo = '';
        var enumerator = items.getEnumerator();
        while (enumerator.moveNext()) {
            var currentListItem = enumerator.get_current();
            innerHtml += "<table><p><h3>" + currentListItem.get_item('Article Title') + "</h3></p><tr><td><img src='" + currentListItem.get_item('Article Thumbnail').$2_1 + "'/></td><td><p> " + currentListItem.get_item('Article Body') + "</p><p> Posted In: <a href = '" + currentListItem.get_item('Article Categories').$2_1 + "'>" + currentListItem.get_item('Article Categories').$3_1 + "</a></p></td></tr></table>";
        }
        table.html(innerHtml);
    }),
    Function.createDelegate(this, fail)
    );
}
  • 2
    There is no explanation for the issue, just some code pasted in it. – Amadeo Gallardo Jan 31 '15 at 12:46
  • By using above i got below error 0x800a139e - JavaScript runtime error: 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. – user39221 Jan 31 '15 at 17:00
  • I tried by adding internal field name but i am not getting item from list. still having issue. – user39221 Feb 1 '15 at 3:11
1

The reason for field initialization error is that in the view you are specifying Article Date field whereas in the success method you are trying to retrieve Article Title which obviously hasn't been requested at the time of query formation. It also looks like you are using Display names of the fields rather than Internal names.

0

Try below code

var items;
function listAllCategories() {
    var ctx = new SP.ClientContext(appWebUrl);
    var appCtxSite = new SP.AppContextSite(ctx, hostWebUrl);
    var web = appCtxSite.get_web(); //Get the Web 
    var list = web.get_lists().getByTitle("NewsList"); //Get the List
    var query = new SP.CamlQuery(); //The Query object. This is used to query for data in the List
    query.set_viewXml('<View><Query><OrderBy><FieldRef Name=\'Article Date\' Ascending=\'False\'/></OrderBy></Query><RowLimit>2</RowLimit></View>');
    items = list.getItems(query);   
    ctx.load(items);
    //Execute the Query Asynchronously
    ctx.executeQueryAsync(Function.createDelegate(this, onQuerySucceeded), Function.createDelegate(this, onQueryFailed));
}
function onQuerySucceeded(sender, args) {
    var table = $("#tblcategories");
    var innerHtml = "<table><p><h3></h3></p><tr><td></td><td><p></p><p></p></td></tr></table>";     
    var listItemEnumerator = items.getEnumerator();         
    while (listItemEnumerator.moveNext()) {
        var currentListItem = listItemEnumerator.get_current();
        innerHtml += "<table><p><h3>" + currentListItem.get_item('Article Title') + "</h3></p><tr><td><img src='" + currentListItem.get_item('Article Thumbnail').$2_1 + "'/></td><td><p> " + currentListItem.get_item('Article Body') + "</p><p> Posted In: <a href = '" + currentListItem.get_item('Article Categories').$2_1 + "'>" + currentListItem.get_item('Article Categories').$3_1 + "</a></p></td></tr></table>";
    }
    table.html(innerHtml);
}
function onQueryFailed(sender, args) {
    alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}
0

Check the internal name of any field. Sometimes this issue caused due to internal name miss match. You can follow this url http://www.enjoysharepoint.com/Articles/Details/the-property-or-field-has-not-been-initialized-error-in-20910.aspx

Your Answer

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

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