3

Right now I am using this method:

function getWebProperties(id) {
    var ctx = new SP.ClientContext.get_current();
    this.website = ctx.get_web();
    this.listCollection = website.get_lists();
    this.oList = listCollection.getByTitle('NavStructure');
    this.oItem = oList.getItemById(id);
    ctx.load(oItem, 'Include(Id, DisplayName)');
    ctx.executeQueryAsync(Function.createDelegate(this, this.onSuccess), Function.createDelegate(this, this.onFail));
}

function onSuccess(sender, args) {
        alert("Title: " + oItem.get_displayName() + " ID: " + oItem.get_id());
}

function onFail(sender, args) {
        alert('failed to get list. Error:' + args.get_message());
}

But it keeps erroring out (through the onFail method) with:

failed to get list. Error:Invalid request

I also changed this line:

ctx.load(oItem, 'Include(Id, DisplayName)');

to

ctx.load(oItem);

And it errored out (in the browser. never fired the onFail method) with:

uncaught exception: [Exception... "'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.' when calling method: [nsIDOMEventListener::handleEvent]" nsresult: "0x8057001c (NS_ERROR_XPC_JS_THREW_JS_OBJECT)" location: "JS frame :: resource://firebug_rjs/net/spy.js :: callPageHandler :: line 796" data: no]

Can anyone see what I am doing wrong here?

5

Use ctx.load(oItem);

Then get the columns data using oItem.get_item('Title') OR oItem.get_item('ID')

I will try using ctx.load(oItem, 'Include(Id, DisplayName)'); and udpate the results in comments.

Hope this helps.

0

With lookup column and no 'Include' declaration.

var oItem ='';
function retrieveWebSite() {
    SP.SOD.executeFunc('sp.js', 'SP.ClientContext', function(){
        var clientContext = new SP.ClientContext.get_current();
        this.oWebsite = clientContext.get_web();
        clientContext.load(this.oWebsite);

        var lstObject = oWebsite.get_lists().getByTitle('Listname');
        oItem = lstObject.getItemById(5);
        clientContext.load(oItem);

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

function onQuerySucceeded(sender, args) {
    var look  = oItem.get_item('LookupColumnName').get_lookupValue();
    var title = oItem.get_item('Title');
    var id = oItem.get_id();
    alert("Loook up column value: "+look);
    alert("Title column: "+title);
    alert("Id column value: "+id);
}

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

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.