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

Event Date Function

  Date.toISOFormat = function (date, ignoreTime) {      /// <summary>Date object static method to format a date to date ISO string - YYYY-MM-DDThh:mm:ssZ</summary>      /// <param name="date" type="Date" mayBeNull="false" optional="false"></param>      /// <param name="ignoreTime" type="Boolean" mayBeNull="false" optional="true"></param>      /// <returns type="String">A string representing ISO format for specied date</returns>        // If not specified, time is ignored      var ignoreTime = ignoreTime || {};        function pad(number) {          // Add leading 0 if number is less then 10 (enclosed method)          var r = String(number);          if (r.length ==...

The _spPageContextInfo

I f you are creating a SharePoint app using JavaScript and the Client side object model you need this friendly object. In the development of an app, you would require some basic properties- SharePoint as a framework provides these with the  _spPageContextInfo  object. _  spPageContextInfo  will provide these below properties:  webServerRelativeUrl  webAbsoluteUrl siteAbsoluteUrl serverRequestPath layoutsUrl webTitle webTemplate tenantAppVersion isAppWeb webLogoUrl webLanguage currentLanguage currentUICultureName currentCultureName env nid fid clientServerTimeDelta updateFormDigestPageLoaded siteClientTag crossDomainPhotosEnabled webUIVersion webPermMasks pagePersonalizationScope userId userLoginName systemUserKey alertsEnabled siteServerRelativeUrl allowSilverlightPrompt themedCssFolderUrl themedImageFileNames

PublishingAssociatedContentType

The Content Type to be associated with a page layout is indicated by the 'PublishingAssociatedContentType'. In the actual sense it means a binding setting between the Page Layout and the content type. If you fail to provide one, the SharePoint framework will make use of the Page Content type. The format of the binding is: ';# e.g. ';#ContentPage;#0x010100C568DB52D9D0A14D9B2FDCC96666E9F2007948130EC3DB064584E219954237AF3900470b13dd348649d08f9e5151501df9a4000dbd46dad8d045f98c83ad983b66d3f2;#'. where Content Type name: ContentPage Content Type ID: 0x010100C568DB52D9D0A14D9B2FDCC96666E9F2007948130EC3DB064584E219954237AF3900470b13dd348649d08f9e5151501df9a4000dbd46dad8d045f98c83ad983b66d3f2 respectively. QED