0

I am trying to get a list id for a specific list but it just goes to the failed method

Here is my code:

var SFW = window.SFW || {};
var NBSInboxResponse = SFW.NBSInboxResponse || {};
NBSInboxResponse.getListID = function () {
    'use strict';

var siteURl=url;
        var clientContext = new SP.ClientContext(siteUrl);
    var oWebsite = clientContext.get_web();
    NBSInboxResponse.collList = oWebsite.get_lists().getByTitle('Feedback Items');

    clientContext.load(NBSInboxResponse.collList, 'Include(Title, Id)');


    clientContext.executeQueryAsync(NBSInboxResponse.getListSuccess, NBSInboxResponse.getListIDFailure);
};

NBSInboxResponse.getListIDSuccess = function () {
    'use strict';
    var listID = NBSInboxResponse.collList.get_id();
    alert(listID);

   };

NBSInboxResponse.getListIDFailure = function () {
    'use strict';

    alert('Request failed. ');

    };

jQuery(document).ready(function () {
    'use strict';

    SP.SOD.executeFunc('sp.js', 'SP.ClientContext', NBSInboxResponse.getListID);


});
3
  • How are you populating the var siteURl=url; URL value? Dec 1, 2014 at 12:59
  • its just a url mysharepointsite.com
    – Asad
    Dec 1, 2014 at 13:26
  • Try this var context = SP.ClientContext.get_current(); and see if that helps. Dec 1, 2014 at 13:36

1 Answer 1

1

The syntax you use when filtering fields to return inside of the load method depends on wether you're dealing with a collection or an individual object.

You're encountering an error because you've specified the syntax for dealing with a collection:

clientContext.load(collOfItems, 'Include(Field1,Field2)');

What you need is the alternate syntax for dealing with individual items; it uses a comma separated list of strings that represent field/property names, in your case it would look like this:

clientContext.load(NBSInboxResponse.collList, 'Title', 'Id');

That should retrieve the list with just those two properties that you can then use get_title() and get_id() on respectively -- you'll note other getters such as get_created() won't work since other data was actually excluded.

As an aside, I wouldn't really call the variable collList because the name is misleading, making you think it's a collection of lists -- when it is in fact just a list object :) I would probably go with something like oList or objList; but that doesn't really effect the output of your script!

You can find more information about the load method signatures in an MSDN blog article by Nikhil Sachdeva, here:

The context type provides a load method which is responsible for loading objects and collections from the server . The SP.Context type provides two variants of load which serve different purposes: Load: This is also called as an In-Place load. This load variant should be used when you want to load objects and collection and maintain their state through multiple requests. Essentially, whatever object that you pass into the load method is populated and can be used as-is in subsequent requests.

LoadQuery: The LoadQuery variant returns a new instance or collection every time it is called. This is beneficial when you want to return an enumerator to the object which you want to maintain yourself.

Both the variant can take a parameter that defines which columns to return. For loading a singular object, you simple pass the names of the columns separated by commas as followings:

context.load(<object>,'<Field1>','<Field2>','<Field3>');

In case the object is a collection, you can still specify the columns to include in the load operation by using the following format 'Include(, ,, …)' format.

context.load(<objectCollection>,'Include(<Field1>,<Field2>,<Field3>');

Note that if you want to access the child properties of an object, you must load the object (by calling the ExecuteQueryAsync method) before you can access it; otherwise, you will generate an exception such as the following:

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.

For those new to SharePoint, it should be noted that you could have just called clientContext.load(NBSInboxResponse.collList) with no parameters, and it would have retrieved the Id and Title, but it would have also retrieved many other properties which you would be able to see by accessing the <list>.get_objectData().get_properties() method (in your browser tools probably works the best here, so you can inspect the result object). Using the filtering syntax with the load method makes the data sent back by the server smaller and can provide a better user experience by only requesting precisely what is required.

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.