For developers: 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:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
|
using System; |
|
using System.Collections.Generic; |
|
using System.Linq; |
|
using System.Text; |
|
using System.Threading.Tasks; |
|
using Telerik.Sitefinity.GenericContent.Model; |
|
using Telerik.Sitefinity.Modules.GenericContent; |
|
|
|
namespace Telerik.Sitefinity.Documentation.CodeSnippets.DevGuide.SitefinityEssentials.Modules.ContentBlocks |
|
{ |
|
public partial class ContentBlocksSnippets |
|
{ |
|
public ContentItem GetContentItemByIdNative(Guid contentItemId) |
|
{ |
|
ContentManager manager = ContentManager.GetManager(); |
|
ContentItem contentItem = manager.GetContent().Where(cI => cI.Id == contentItemId).FirstOrDefault(); |
|
return contentItem; |
|
} |
|
} |
|
} |
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:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
|
using System; |
|
using System.Collections.Generic; |
|
using System.Linq; |
|
using System.Text; |
|
using System.Threading.Tasks; |
|
using Telerik.Sitefinity.GenericContent.Model; |
|
|
|
namespace Telerik.Sitefinity.Documentation.CodeSnippets.DevGuide.SitefinityEssentials.Modules.ContentBlocks |
|
{ |
|
public partial class ContentBlocksSnippets |
|
{ |
|
public ContentItem GetContentItemByIdFluent(Guid contentItemId) |
|
{ |
|
ContentItem contentItem = App.WorkWith().ContentItems().Where(cI => cI.Id == contentItemId).Get().FirstOrDefault(); |
|
return contentItem; |
|
} |
|
} |
|
} |
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.