4

I have created a generic function which returns a list with only selected properties as shown:

var getList = function (listTitle, properties) {
        var deferred = $.Deferred();
        var clientContext = new SP.ClientContext.get_current();
        var list = clientContext.get_web().get_lists().getByTitle(listTitle);   
        // This works properly
        //clientContext.load(list, 'DefaultEditFormUrl', 'DefaultNewFormUrl', 'Id');        

        clientContext.load(list, properties);
        clientContext.executeQueryAsync(
            Function.createDelegate(this,
                function () { deferred.resolve(list); }),
            Function.createDelegate(this,
                function (sender, args) { deferred.reject(sender, args); }));

        return deferred.promise();
    };

The issue is how to call this function properly. The below call only loads 'DefaultEditFormUrl'.

var ListTitle = 'myListTitle';
getList(ListTitle, 'DefaultEditFormUrl', 'DefaultNewFormUrl', 'Id')

And the below call gives error:

The query expression is not supported

var ListTitle = 'myListTitle';
getList(ListTitle, 'DefaultEditFormUrl\', \'DefaultNewFormUrl\', \'Id')
5

Since SP.ClientRuntimeContext.prototype.load(clientObject) method accepts properties as dynamic parameters, you could consider the following approach:

var getList = function (listTitle) {
     var deferred = $.Deferred();
     var ctx = SP.ClientContext.get_current();
     var list = ctx.get_web().get_lists().getByTitle(listTitle);   

     //construct arguments for SP.ClientRuntimeContext.prototype.load method
     var loadArgs = Array.prototype.slice.call(arguments);
     loadArgs.splice(0, 1);  //remove first arg since it is a List Title
     loadArgs.unshift(list); //insert ClientObject as first arg 
     SP.ClientRuntimeContext.prototype.load.apply(ctx,loadArgs);
     ctx.executeQueryAsync(
            function () { 
               deferred.resolve(list);
            },
            function (sender, args) { 
               deferred.reject(sender, args); 
            });
     return deferred.promise();       
};

Usage

getList('Documents', 'DefaultNewFormUrl','DefaultEditFormUrl','Id')
.done(function(list)
{
   console.log(list.get_defaultEditFormUrl());
   console.log(list.get_defaultNewFormUrl());
   console.log(list.get_id().toString());
})
.fail(
function(sender,args){
    console.log(args.get_message());
});
2
  • 1
    Awesome. Thanks for the help. That works perfectly. – Nadeem Yousuf-AIS Dec 23 '14 at 13:17
  • Given the title of the question, I think it would be useful to add that if you're loading a collection of items (instead of a single object like a list) you need to use the "Include(Field1, Field2, Field3)" argument syntax instead of seperate string arguments. – John-M Dec 23 '14 at 13:20
1

Just want to add that you could pass the properties as an array, if you look at the definition for the load function:

load: function SP_ClientRuntimeContext$load(clientObject) {
        if (!clientObject) {
            throw Error.argumentNull('clientObject');
        }
        SP.ClientAction.$j(this, clientObject);
        var $v_0 = null;

        if (arguments.length === 2 && Array.isInstanceOfType(arguments[1])) {
            $v_0 = arguments[1];
        }
        else {
            var $v_1 = [];

            for (var $v_2 = 1; $v_2 < arguments.length; $v_2++) {
                Array.add($v_1, arguments[$v_2]);
            }
            $v_0 = $v_1;
        }
        SP.DataRetrievalWithExpressionString.load(clientObject, $v_0);

Then you use it like this:

getList(ListTitle, ['DefaultEditFormUrl', 'DefaultNewFormUrl', 'Id'])

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.