1

I am trying to get field of a list :

var ctx = SP.ClientContext.get_current()
var web = ctx.get_web()
var list = web.getList(_spPageContextInfo.webAbsoluteUrl + "/lists/DocumentReferences")
ctx.load(list)
ctx.executeQueryAsync(function(){alert("ok");}, function(){alert("ko");})

It's work but, after, if I try to do this :

var field = list.get_fields().getByInternalNameOrTitle("ColumnName");

I have this error :

list.get_fields is not a function

Someone know why?

Thanks !

3 Answers 3

1

Please share all of your code since the error is probably there.

I have no problems running this code:

var ctx = SP.ClientContext.get_current();
var web = ctx.get_web();
var list = web.getList(_spPageContextInfo.webAbsoluteUrl + "/lists/mylist");
var fields = list.get_fields();
ctx.load(fields); 
ctx.executeQueryAsync(
   function(){ console.log('success', fields); }, 
   function(){ console.log('error', arguments); } 
);
0

Before to use this line var field = list.get_fields().getByInternalNameOrTitle("ColumnName");

Try to load first your fields as the following

var listFields = list.get_fields();
clientContext.load(listFields);

And then in the function: onQuerySucceeded

var fieldEnumerator = listFields.getEnumerator();
    while (fieldEnumerator.moveNext()) {
        var field = fieldEnumerator.get_current();
        var name = field.get_staticName();
        if (name == "Colname") {
            docFeatureEnabled = true;
            break;
        }
    }

For more details check Try Get Field with client object model without error

0

executeQueryAsync has two parameters: the callback function to run when the query is successful, and the callback function to run if the query runs into any errors.

Any code that depends on the query running successfully should be placed inside the success callback function (the first parameter of executeQueryAsync).

ctx.executeQueryAsync(
    function(){
        alert("ok");
        var field = list.get_fields().getByInternalNameOrTitle("ColumnName");
        // rest of your code goes here...
    }, 
    function(){alert("ko");}
);

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.