1

I'm currently using Sitecore GraphQL to fetch fields from a specific item in Sitecore. However, my query is fetching all standard fields along with the fields added in the template. I only want to retrieve the fields that are explicitly added to the template.

Here's the query I'm using:

query GetFieldsFromContextItem($contextItem: String!) {
  item(path: $contextItem) {
    fields {
      id
      name
      value
    }
  }
}

Is there a way to modify this query or add parameters to achieve this? I want to exclude standard fields like __Created, __Updated, etc., and only retrieve the fields defined in the template.

Any help or guidance would be greatly appreciated. Thank you!

6 Answers 6

4

You can modify your GraphQL query by setting the ownFields parameter to true. This parameter ensures that only fields belonging to the current template are returned, excluding the standard fields.

Here's the corrected script:

query GetFieldsFromContextItem($contextItem: String!) {
  item(path: $contextItem) {
    fields(ownFields: true) {
      id
      name
      value
    }
  }
}

By setting ownFields: true, you ensure that only the fields defined in the template of the current item are returned.

Hope this helps!

3

you can use fields(ownFields:true) for only selecting custom fields from your template . This will exclude standard fields from result array .

query GetFieldsFromContextItem($contextItem: String!) {
  item(path: $contextItem) {
    fields(ownFields:true) {
      id
      name
      value
    }
  }
}

You can refer this also : https://www.getfishtank.com/blog/useful-sitecore-graphql-queries hope this helps !

2

There are two important options of excludeStandardFields:true and ownFields:false for the fields property. excludeStandardFields allows you to select if you want the system/standard values returned or just the primary editable fields. ownFields is a little different when used within fields as it is a true/false that seems to return an array of the first field type in the list.

fields(excludeStandardFields:true, ownFields:true) {
      id
      name
      value
}

Reference: Getting all Sitecore Fields with GraphQL

Hope it helps!

0

Using ownFields: true will allow you to grab your fields. Here is the query you can use.

query {
  item(path: $itemPath, language: "en") {
    ... on TemplateNameHere {
      fields(ownFields: true) {
        id
        name
        value
      }
    }
  }
}

Refer to this for more details.

https://medium.com/@ravikumarmishra334/how-to-write-graphql-query-for-sitecore-jss-part-1-ec5dee837333

Hope this helps.

0

You can add ownFields parameter as below in the GraphQL query, to fetch only template fields.

query GetFieldsFromContextItem($contextItem: String!) {
  item(path: $contextItem) {
   fields(ownFields: true){
    id
    name
    value
   }
 }
}

Hope this helps!!!

-1

Using ownFields:true will allow you to grab your fields. Here is the query you can use. you ensure that only the fields defined in the template of the current item are returned. ownFields is a little different when used within fields as it is a true/false that seems to return an array of the first field type in the list.

query GetFieldsFromContextItem($contextItem: String!) {
  item(path: $contextItem) {
    fields(ownFields: true) {
      id
      name
      value
    }
  }
}

Hope this helps!!!

1
  • Is there something wrong in my answer? Commented Jul 9 at 7:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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