Sharepoint 2013 Javascript - Get List Fields
<p>I am on a double learning curve with SharePoint 2013 development and JavaScript.</p>

<p>Please refer to below.</p>

<pre><code>function GetListFields(listname, viewname, ResultCallBackFunction)
{
  var clientContext = new SP.ClientContext.get_current();
  var web = clientContext.get_web();
  var list = web.get_lists().getByTitle(listname);
  if (viewname == "")
  {
    var view = list.get_views().getByTitle(viewname);
    var listFields = view.get_viewFields(); 
  }
  else
  {
    this.listFields = list.get_fields().getByInternalNameOrTitle("Title");  
  } 
  clientContext.load(this.listFields);
  clientContext.executeQueryAsync(onListFieldsQuerySucceeded, onListFieldsQueryFailed);


  function onListFieldsQuerySucceeded()
  {
    console.log(listFields.get_fields().getByInternalNameOrTitle("Title").get_internalName());

    var fldArray = new Array();
    var fieldEnumerator = listFields.getEnumerator();
    while (fieldEnumerator.moveNext())
    {
      var oField = fieldEnumerator.get_current();
      fldArray.push(oField);
    }

    ResultCallBackFunction(fldArray);
  }

  function onListFieldsQueryFailed()
  {
    alert("Something went wrong. The End is Nigh.");
  }
}
</code></pre>

<p>The intent is to call <em>GetListFields</em> to return an array with the list's fieldnames. This returns an error "Unable to get property 'get_fields' of undefined or null reference" at the line "console.log....". Please note that this is for my debugging.</p>

<p>If I change the code for function <em>GetListFields</em> as follows:</p>

<pre><code>var clientContext = new SP.ClientContext.get_current();
var web = clientContext.get_web();
var list = web.get_lists().getByTitle(listname);
if (viewname == "")
    viewname = "All Items";
var view = list.get_views().getByTitle(viewname);
this.listFields = view.get_viewFields(); 
</code></pre>

<p>I DO get a result but this contains a field named 'LinkTitle' which I do not have in my list because I have renamed this. What am I missing?</p>

<p>Further to the above, I got an error when using </p>

<pre><code>var listFields = ...
</code></pre>

<p>which fixed by doing</p>

<pre><code>this.listFields = ...
</code></pre>

<p>but do not understand the difference.</p>

<p>Thanks for any help or pointers.</p>