Retrieve Static and Internal Field Names of a SharePoint List using JavaScript in Office 365

Introduction

There are scenarios where we need to show and compare Internal and Static field names of a SharePoint list.

For instance, if we want to do CAML query over some custom fields, but we don’t remember exactly the Internal Name of the field, we can use this code directly copied and pasted in the browser console to get all the names.

JavaScript Object Model reference

Using JSOM or JavaScript Object Model, It is important the difference between get all fields directly from a list and get all view fields from a view.

A little more information about the Object Model:

Code

This is the code to get all the field information:

// Open current SPWeb: _spPageContextInfo.webServerRelativeUrl
var ctx = new SP.ClientContext(_spPageContextInfo.webServerRelativeUrl);
// Get Documents library
var list = ctx.get_web().get_lists().getByTitle('Documents');
//get_fields() returns SP.FieldCollection object --- contains all SP.Field object properties > https://msdn.microsoft.com/en-us/library/office/jj246815.aspx
var fieldCollection = list.get_fields();
ctx.load(fieldCollection,'Include(InternalName,StaticName)');
var view = list.get_views().getByTitle('All Documents');
//get_viewFields() returns SP.ViewFieldCollection object --- only field names (Internal Names), but not a SP.Field object > https://msdn.microsoft.com/en-us/library/office/jj244841.aspx
var viewFieldCollection = view.get_viewFields();
ctx.load(viewFieldCollection);
ctx.executeQueryAsync(function (){
var cont=0;
var fields = 'SP.FieldCollection from list.get_fields()'
fields += 'Internal Name - Static Name \n';
fields += '--------------------------- \n';
var listEnumerator = fieldCollection.getEnumerator();
while (listEnumerator.moveNext()) {
fields += listEnumerator.get_current().get_internalName() + ' - ' + listEnumerator.get_current().get_staticName() + ";\n "; //
cont++;
}
console.log(fields + '-------------------------- \n Number of Fields: ' + cont);
var cont=0;
var viewfields = '\nSP.ViewFieldCollection from view.get_viewFields() \n';
viewfields += 'Internal Name \n';
viewfields += '--------------------------- \n';
var listEnumerator = viewFieldCollection.getEnumerator();
while (listEnumerator.moveNext()) {
viewfields += listEnumerator.get_current() + "\n";
cont++;
// If we need more data about view fields we can call again to server using
// var field = fieldCollection.getByInternalNameOrTitle(listEnumerator.get_current());
// ctx.load(field);
// ctx.executeQueryAsync(.....)
// Or, If we would like to save a server query, we could iterate again
// the fieldCollection.getEnumerator(); checking the Internal Name of the field
}
console.log(viewfields + '-------------------------- \n Number of Fields: ' + cont);
},
function(sender, args){
console.log('Request collListItem failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}
);

 

Author: José Quinto
Link: https://blog.josequinto.com/2015/12/08/retrieve-static-and-internal-field-names-of-a-sharepoint-list-using-javascript-in-office-365/
Copyright Notice: All articles in this blog are licensed under CC BY-SA 4.0 unless stating additionally.