I'm trying to return only custom fields created for a list but the fields api returns all available fields from what i can see. I've created a view with the fields I want but can't see how to get the fields from the views api.
Thanks.
Hey @simkessy,
You can use fields filtering to get only custom fields. View's fields can be returned as well. Check the code samples below:
import { sp } from '@pnp/sp';
const list = sp.web.lists.getByTitle('Custom');
// Fields which can be updated
const filter1 = `Hidden eq false and ReadOnlyField eq false`;
list.fields.select('InternalName').filter(filter1).get().then(fields => {
console.log(`Can be updated: ${fields.map(f => f.InternalName).join(', ')}`);
// Title, ...Custom, ContentType, Attachments
});
// Only custom field
const filter2 = `Hidden eq false and CanBeDeleted eq true`;
list.fields.select('InternalName').filter(filter2).get().then(fields => {
console.log(`Custom fields: ${fields.map(f => f.InternalName).join(', ')}`);
// ...Custom
});
// Application specific fields
const includeFields = [ 'Title', 'Author', 'Editor', 'Modified', 'Created' ];
const filter3 = `Hidden eq false and (ReadOnlyField eq false or (${
includeFields.map(field => `InternalName eq '${field}'`).join(' or ')
}))`;
list.fields.select('InternalName').filter(filter3).get().then(fields => {
console.log(`Application specific: ${fields.map(f => f.InternalName).join(', ')}`);
// Title, ...Custom, ContentType, Modified, Created, Author, Editor, Attachments
});
// Fields in a view
list.defaultView.fields.select('Items').get().then(f => {
const fields = (f as any).Items.results || (f as any).Items;
console.log(`Fields in a view: ${fields.join(', ')}`);
});
Great answer @koltyakov, I am going to steal this and add it to the fields docs page.
Going to close this as answered, I have also added the above examples to the docs. Thanks!
Most helpful comment
Hey @simkessy,
You can use fields filtering to get only custom fields. View's fields can be returned as well. Check the code samples below: