0

I am using CSOM/JSON to get field data for a list. If I enumerate the list fields I get the field data/object. But if I look for one using getByInternalNameOrTitle I get the 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.".

The code below prints all the fields in that first console.log but that second one results in an error. What gives?

ExecuteOrDelayUntilScriptLoaded(function()
{
    var clientContext = SP.ClientContext.get_current();
    var list = clientContext.get_web().get_lists().getByTitle("Meeting Agenda Items");
    this.listFields = list.get_fields();
    clientContext.load(this.listFields);


    clientContext.executeQueryAsync(Function.createDelegate(this, listFieldsLoaded), Function.createDelegate(this, function(){alert("error 1");}));
}, "sp.js");

function listFieldsLoaded()
{
    // if i enumerate the list fields this works fine
    var fieldEnumerator = listFields.getEnumerator();
    while(fieldEnumerator.moveNext())
    {
        var thisField = fieldEnumerator.get_current();
        console.log(thisField.get_internalName());
    }

    // but if i try to get it by an internal name
    // then it does not
    var thisField = listFields.getByInternalNameOrTitle("Title");
    console.log(thisField.get_internalName());
}

2 Answers 2

1

You need to load the thisField variable into the context.

var thisField;

ExecuteOrDelayUntilScriptLoaded(function()
{
    var clientContext = SP.ClientContext.get_current();
    var list = clientContext.get_web().get_lists().getByTitle("Meeting Agenda Items");
    this.listFields = list.get_fields();
    clientContext.load(this.listFields);
    thisField = listFields.getByInternalNameOrTitle("Title");

    clientContext.load(thisField);

    clientContext.executeQueryAsync(Function.createDelegate(this, listFieldsLoaded), Function.createDelegate(this, function(){alert("error 1");}));
}, "sp.js");

function listFieldsLoaded()
{
    // if i enumerate the list fields this works fine
    var fieldEnumerator = listFields.getEnumerator();
    while(fieldEnumerator.moveNext())
    {
        var thisField = fieldEnumerator.get_current();
        console.log(thisField.get_internalName());
    }


    console.log(thisField.get_internalName());
}

This will return the field name. (you can move the variable somewhere and pass it as a parameter) I was just doing this quickly.

2
  • I don't understand why I have to load it into context when it works fine through getEnumerator without explicitly loading each field into the context? Commented Oct 14, 2016 at 20:58
  • I wish I had an answer to that. I can only say that the Client context seems to only load pieces at a time, any time that the error thrown says something about being explicitly called it usually means it has to be loaded first. Commented Oct 14, 2016 at 21:59
0

why not set it before you add it to the context:

single field into the context:

ExecuteOrDelayUntilScriptLoaded(function()
{
    var clientContext = SP.ClientContext.get_current();
    var list = clientContext.get_web().get_lists().getByTitle("Meeting Agenda Items");
    this.listFields = list.get_fields().getByInternalNameOrTitle("Title");
    clientContext.load(this.listFields);


    clientContext.executeQueryAsync(Function.createDelegate(this, listFieldsLoaded), Function.createDelegate(this, function(){alert("error 1");}));
}, "sp.js");

function listFieldsLoaded()
{
    console.log(listFields.get_internalName());
}

or

all fields into the context:

ExecuteOrDelayUntilScriptLoaded(function()
{
    var clientContext = SP.ClientContext.get_current();
    var list = clientContext.get_web().get_lists().getByTitle("Meeting Agenda Items");
    this.listFields = list.get_fields();
    clientContext.load(this.listFields);


    clientContext.executeQueryAsync(Function.createDelegate(this, this.listFieldsLoaded), Function.createDelegate(this, function(){alert("error 1");}));
}, "sp.js");

function listFieldsLoaded()
{
    console.log(listFields.get_fields().getByInternalNameOrTitle("Title").get_internalName());
}
6
  • I don't understand why I have to load it into context when it works fine through getEnumerator without explicitly loading each field into the context? Commented Oct 14, 2016 at 20:59
  • updated answer. loads all fields into the context and get internal name within success method
    – Ali Jafer
    Commented Oct 14, 2016 at 21:21
  • That won't work. That is the exact same thing I tried in my example question and it gives an error. Commented Oct 14, 2016 at 21:24
  • sorry updated my answer
    – Ali Jafer
    Commented Oct 14, 2016 at 21:41
  • I think the issue is your returning a collection and cant use the getByInternalNameOrTitle directly. I understand what your trying to do
    – Ali Jafer
    Commented Oct 14, 2016 at 21:43

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.