0

Trying to look up a content by name in order to load the CT's fields. Is there a way to avoid getting all the fields and then iterating and comparing the name to the one I'm looking for (shown here)?

  • The body of your question and your question title are asking two different things. Are you trying to get a field by name or trying to get a content type by name? – Thriggle May 3 '17 at 16:37
1

To get the content type fields from a list you have to get Id from a title and make a second call to get the fields using ID coming from the first call.

  1. Get the content type id from a list using content type name

    site url/_api/web/Lists/getByTitle('list title')/ContentTypes?$filter=Name eq 'content type name'
    
  2. Get Content type is from the StringId variable

  3. Make a second call to get conent type fields

    site url/_api/web/Lists/getByTitle('list title')/ContentTypes('content type id')/Fields
    
0

If you already know the ID of the content type you want to access and the name of the field you want to access, then you do not need to iterate over either the content types in the list or the fields associated with the content type.

For example:

 var context = new SP.ClientContext();
 var listTitle = "Example List", 
     contentTypeId = "0x0100ABC8ABF94519804BBFB5AFAB12683F5F", 
     fieldName = "Title";
 var field = context.get_web().get_lists().getByTitle(listTitle)
     .get_contentTypes().getById(contentTypeId)
     .get_fields().getByInternalNameOrTitle(fieldName);
 context.load(field);
 context.executeQueryAsync(
     function(){
         alert("Field Name: "+field.get_title()+
             "\nInternal Name:"+field.get_internalName()+
             "\nID: "+field.get_id());
     },
     function(sender,args){ 
         alert(args.get_message());
     }
 );

If you don't know the Content Type ID, and only know it's name, you'll need to retrieve all of the content types from the list and check their names to find the one you want, as demonstrated in Amal Hashim's answer.

Once you have the content type object, you can invoke .get_fields().getByInternalNameOrTitle(fieldname) as in my example code above to select a particular field.

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.