How to Query the API with .NET

prismic.io natively supports Custom types. Custom types allow you to define and configure fields for your content. Define custom types for pages, posts, events, authors, products, places, …

A productive way to build pages with prismic.io, is to follow an iterative cycle of defining fields for your custom type, querying its content, and then integrating it into templates.

Queries allow you to retrieve content to be displayed in your pages.

Query Predicates

Queries in prismic.io are based on predicates. Some predicates apply to any custom type, and some are field-specific. You can also combine multiple predicates to express your query.

Here is an example query for retrieving a basic page through its UID:

Copy
// String uid comes from the querystring
Document document = api.GetByUID("basic-page", uid);

The following example retrieves all blog posts:

Copy
Response response = api.query(Predicates.at("document.type", "blog-post"))
   .PageSize(10)
   .Orderings("my.blog-post.date desc")
   .Submit();

A full-text search on all blog posts is done combining two predicates:

Copy
Response response = api.Query(
    Predicates.at("document.type", "blog-post"),
    Predicates.fulltext("document", terms) // terms is a String
).Submit();
IList<Document> documents = response.Results;

Predicates Reference

at(path, value)

The at operator is the equality operator, checking that the fragment matches the described value exactly. 
It takes a value for a field or an array (only for tags).

path

document.type

document.tags

my.{type}.{field}

value

single value

examples

at("document.type", "product")
at("document.tags", ["Macaron", "Cupcake"])
at("my.articles.gender", "male")

not(path, value)

The not operator is the different operator, checking that the fragment doesn't match the described value exactly. 
It takes a value for a field.

path

document.type

my.{type}.{field}

value

single value

example

not("document.type", "product")

any(path, values)

The any operator takes an array of strings as a value. It works exactly the same way as the at operator, but checks whether the fragment matches either one of the values in the array. You can use it with all fragment types.

path

document.type

my.{type}.{field}

values

array of values

example

any("document.type", ["product", "blog-post"])

in(path, values)

The in operator is used to retrieve documents using an Array of IDs or UIDs. This operator is much more performant than the any operator. This operator is more performant than any to query document by UID or ID. This operator return the documents in the same order as the passed Array.

path

document.type

my.{type}.{field}

values

array of values

example

in("document.uid, ["myuid1", "myuid2"])

fulltext(path, value)

The fulltext operator provides two capabilities: either you want to check if a certain string is anywhere inside a document (this is what you should use to make your project's search engine feature), or if the string is contained inside a specific document's structured text fragment. In the example you want to search the term banana in the document

If you want to check in a given structured text fragment you will use something like the second example.

path

global namespace 'document' or your custom types 'my.type.field'

value

search terms

example

fulltext("document", "banana")
fulltext("my.product.title", "banana")

has(path)

The has operator check whether a fragment has a value.

path

your custom types 'my.type.field'

example

has('my.product.price')

missing(path)

The missing operator check if a fragment doesn't have a value. Note that the missing operator will restrict the results to the type implied in the fragment path.

path

your custom types 'my.type.field'

example

missing('my.product.price')

similar(id,value)

The similar operator is especially smart, since it takes an ID of a document, and returns a list of documents whose contents are similar. This allows to build an automated content discovery feature (for instance, a "Related posts" block) at almost no cost.

Also, remember that you can combine it with other predicates, and search for the "similar blog posts" for instance, of even the "similar blog posts that mention chocolate".

id

document ID

value

number

the maximum count of documents that a term may appear in to be still considered relevant

example

similar("VkRmhykAAFA6PoBj", 10)

Predicate options

pageSize(value)

description

The pageSize options define the maximum of document that the API will return for your query. Default is 20, max is 100.

value

page size

example

pageSize(3)

page(value)

description

The page options define the pagination for the result of your query. Defaults to "1", corresponding to the first page.

value

page index

example

page(2)

orderings(values)

description

order result by fields. You can specify as many fields as you want, in order to address all of the documents you are querying or if you have the same value on a given field. Use "desc" next to the field name, to order it from greatest to lowest.

values

fields

example

orderings("[my.product.price desc, my.product.price.title]")

after(value)

description

query documents after a specific document. By reversing the ordering, you can query for previous documents. Useful when creating navigation for a blog.

value

document id

example

after("VkRmhykAAFA6PoBj")

fetchLinks(values)

description

additional fields to retrieve in LinkDocument fragments. The LinkDocument can then be queried like a Document. Note that this only works with basic fields such as Text, Number or Date. It is not possible to retrieve StructuredText fragments from linked document using this field.

value

fields

example

fetchLinks([author.full_name, author.first_name])

fetch(values)

description

fetch only specific fields

value

fields to retrieve

example

fetch(["author.full_name", "author.first_name"])