1

I need to print only the column that has been created by me.

function retrieveFieldsOfListView(listTitle,viewName){

   var context = new SP.ClientContext.get_current();
   var web = context.get_web();
   var list = web.get_lists().getByTitle(listTitle);
   var listFields = list.get_fields();
   context.load(listFields);
   context.executeQueryAsync(printFieldNames,onError);


   function printFieldNames() {
      var e = listFields.getEnumerator();
      while (e.moveNext()) {
         var fieldName = e.get_title();
         console.log(fieldName);
      }
   }

   function onError(sender,args)
   {
      console.log(args.get_message());
   }

    }

But this code is printing all the pre-defined fields along with my field. I don't want the pre-defined fields like 'modified,created,etc'. I want only the coding changes. UI changes are not upto me.

1

If it were CSOM, you could check the SourceId property (built-in fields have the value http://schemas.microsoft.com/sharepoint/v3 in it), in order to tell whether a field is a built-in or a custom one, like in the following code snippet:

SPListItem item = //some code retrieval

var customFields = item.Fields.Cast<SPField>().Where(field => field.SourceId.IndexOf("http", StringComparison.InvariantCultureIgnoreCase) == -1));

In JSOM however the SourceId property is not directly available, so you have to resort to looking into the xml schema, like this:

// the rest of your code omitted for brevity
while (e.moveNext()) {
  var field = e.get_current();
  var schema = field.get_schemaXml();
  if (/SourceID="http/i.test(schema)){
      console.log("custom field found:" + field.get_title());
  }
}
  • Can you please correct the code. This is a new concept for me. It seems something wrong with If part. – Naveen Apr 18 '15 at 10:52
  • 1
    I don't see any problem with the if statement. It's a regular expression match test – MdMazzotti Apr 18 '15 at 12:35
  • This code again displays all the fields in my list:-( – Naveen Apr 19 '15 at 8:37
1

In fact the similar question has already been answered here

Below is provided the examples for the case of JSOM API.

At least two options are available to determine whether field is system or user-defined

Using SourceId property of a Field

For system fields it's value is set to http://schemas.microsoft.com/sharepoint/v3

Note: SP.Field object does not expose SourceId property, but it could be extracted from SP.Field.schemaXml property as demonstrated below:

function getListFields(listTitle,success,error){

   var context = SP.ClientContext.get_current();
   var web = context.get_web();
   var list = web.get_lists().getByTitle(listTitle);
   var fields = list.get_fields();
   context.load(fields);
   context.executeQueryAsync(
      function(){
          success(fields);
      },
      error);
}




//Usage
getListFields('Pages',
   function(fields) {
      //get only user defined fields
      var userDefinedFields = fields.get_data().filter(function(f){
          var schema = f.get_schemaXml();
          if (schema.indexOf('SourceID="http://schemas.microsoft.com/sharepoint/v3"') === -1){
              return f;
          }
      });

      //print user defined fields title
      userDefinedFields.forEach(function(f){
          console.log(f.get_title()); 
      });
   },
   function(sender,args)
   {
      console.log(args.get_message());
   });

Using Field.FromBaseType property

SP.Field.fromBaseType property indicates whether the field derives from a base field type, for system fields it returns true

 getListFields('Contacts',
   function(fields) {
      //get only user defined fields
      var userDefinedFields = fields.get_data().filter(function(f){
          if (!f.get_fromBaseType()){
              return f;
          }
      });

      //print user defined fields title
      userDefinedFields.forEach(function(f){
          console.log(f.get_title()); 
      });
   },
   function(sender,args)
   {
      console.log(args.get_message());
   });

Note: Personally i find option 1 as more reliable way to determine whether field is system or user-defined

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.