Query content blocks

You can query a content block by its ID. To search for a content blocks based on other property or criteria, see For developers: Find content blocks.

NOTE: The code examples below work with the ID of the master version of the content block. The examples return the master version. For more information about querying by the ID of the live version and returning the live version, see For developers: Query master and live versions.

To query a content block you can use the Native API or the Fluent API:

Query a content block with the Native API

To query a specific content block using the Native API, you use the ContentManager class. 
The following code queries the content block by its ID:

You use the GetContent method and filter based on the Id property. If the content block does not exist, the method returns null.

To find the content block, you can also use the GetContent method passing contentItemId, in the following way:

ContentItem contentItem = manager.GetContent(contentItemId);

If there is no content block with the specified Id, the call to GetContent(contentItemId) throws an exception of type ItemNotFoundException.

Query a content block with the Fluent API

To query a content block using the Fluent API, you use the content block facade. 
The following code queries the content block by its ID:

You first initialize the plural content block facade using App.WorkWith().ContentItems(). Then, you filter the content blocks based on the Id property. Finally, you use the Get method to get the content block. If the content block does not exist, the method returns null.

To find the content block, you can also use the singular content block facade, in the following way:

ContentItem contentItem = App.WorkWith().ContentItem(contentItemId).Get();

If there is no content block with the specified Id, the call to ContentItem(contentItemId) throws an exception of type ItemNotFoundException.


Was this article helpful?

Next article

Find content blocks