Skip to main content

Get all fields from a library Javascript

 // Open current SPWeb: _spPageContextInfo.webServerRelativeUrl

var ctx = new SP.ClientContext(_spPageContextInfo.webServerRelativeUrl);

// Get Documents library
var list = ctx.get_web().get_lists().getByTitle('TEST');

//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());
    }
);



Comments

Popular posts from this blog

Get Sum of Columns in a SharePoint list (Threshold edition)

It is a known fact that once the treshold limit is reached in SP, everything seems frozen.  At this point you have some options. Increase the limit using powershell or from the Central Admin OR Index the columns that you will like to operate on and create views based on those. However programmatically if you want to sum a list that has reached its threshold.  You can do so in batches as hown below:          protected   void  GetSummary( out   long  ideas,  out   long  votes,  out   long  comments,  out   long  transform)         {              long  ideasum = 0;              long columntoSum = 0;              long  commentS...

Add Web Visual Studio templates to SharePoint Project

Would you like to add the web user control to SharePoint 2007 project development.  Then open the project file with notepad and add {349c5851-65df-11da-9384-00065b846f21} to the ProjectTypeGUIDs node.   The necessary change is shown below: < ProjectTypeGuids > {349c5851-65df-11da-9384-00065b846f21};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} </ ProjectTypeGuids >