0

When calling more than one field the query fails. No descriptive reason why.

[Shorthand as typing on phone]

Working code:

 Function getData(){
    ctx = new SP.ClientContext();
    u = ctx.get_web().get_currentUser();
    //ctx.load(u,"Email");
    ctx.load(u,"LoginName");
    ctx.loadQueryAsync(Disp,onFail)
 }
 Function Disp(){
     //console.log(u.get_email());
     console.log(u.get_loginName());
 }
 ...fail function, etc..

I can choose to have email or loginName output successfully however ctx.loadQueryAsync fails if I try both. Each of the following has been tested in place of previously used load:

 ctx.load(u,"Include(Email,LoginName)");
 ctx.load(u,"Include(Email)");
 ctx.loadQuery(u,"Include(Email)");

Using any of the above result in a failed query. Message is Invalid Request a Microsoft.SharePoint.Client.InvalidQueryException. Other error values are undefined

2 Answers 2

1

Try the below code:

function getData(){
ctx = new SP.ClientContext();
this.u = ctx.get_web().get_currentUser();
ctx.load(u);
ctx.executeQueryAsync(Disp,onFail);

}

 function Disp(){
 console.log(u.get_loginName());
 console.log(u.get_email());
 console.log(u.get_title());

} ...fail function, etc..

4
  • I receive error that the field has not been loaded. For now ive changed my code to run for one field at a time one after another
    – user13186
    Aug 4, 2015 at 17:24
  • try using this.u = ctx.get_web().get_currentUser();
    – Pushpendra
    Aug 4, 2015 at 17:29
  • I have tested and updated with working code.
    – Pushpendra
    Aug 4, 2015 at 18:24
  • This worked. Thank you! I won't forget this again
    – user13186
    Aug 4, 2015 at 18:59
0

Recently had to play with SharePoint JS and here is what I found (tested in SharePoint 2010), see code and comments, code is based on answer from here:

function getListInfo() {
    var context = SP.ClientContext.get_current();
    var web = context.get_web();
    var lists = web.get_lists();
    var list = lists.getByTitle('Test');

    context.load(list); // load all fields, seems slow
    //context.load(list, 'Id'); // load only 'Id'
    //context.load(list, 'Title'); // load only 'Title'
    //context.load(list, 'Id', 'Title'); // load only 'Id' and 'Title'
    //context.load(list, 'Include(Id, Title)'); // fails with 'Invalid request' message
    //context.load(list, 'Id', 'Title', 'Description'); // define each field to load as additional parameter
    //context.load(list, ['Id', 'Title', 'Description']); // dynamically define array with fields to load
    //var fields = ['Id', 'Title', 'Description'];
    //context.load(list, fields); // load fields defined in array variable

    context.executeQueryAsync(  //submit query to the server
        function () { //process result in success callback
            console.log('ID:' + list.get_id());
            console.log('Title:' + list.get_title()); 
            console.log('Description:' + list.get_description());
        }, 
        function (sender, args) { //handle error in error callback
            console.log(args.get_message());
        }
    );
}

Alternatively you can try to use CAML query (see example here), in this case Include() works.

Your Answer

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

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