Querying content items

Sitefinity allows you to query for a specific content item by its ID. To search for specific content items based on any other property or criteria, see Finding content items.

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

To query for a specific content item you can use the Native API or the Fluent API.

To query for a specific content item using the Native API, you use the ContentManager class. For more information, see Querying for a content item using Native API.

To query for a specific content item using the Fluent API, you use the content item facade. For more information, see Querying for a content item using Fluent API.

Querying for a content item using Native API

The following code queries for the content item with the specified ID:

public ContentItem GetContentItemById(Guid contentItemId)
{
    ContentManager manager = ContentManager.GetManager();
    ContentItem contentItem = manager.GetContent().Where(cI => cI.Id == contentItemId).FirstOrDefault();
    return contentItem;
}

To query for the content item, you use the GetContent method and filter based on the Id property. If the content item does not exist, the method returns null.

To find the content item, you can also use the GetContent method passing contentItemId:

ContentItem contentItem = manager.GetContent(contentItemId);

Calling GetContent(contentItemId) throws an exception of type ItemNotFoundException, if there is no content item with the specified Id.

Querying for a content item using Fluent API

The following code queries for the content item with the specified ID:

public ContentItem GetContentItemById(Guid contentItemId)
{
    ContentItem contentItem = App.WorkWith().ContentItems().Where(cI => cI.Id == contentItemId).Get().FirstOrDefault();
    return contentItem;
}

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

To find the content item, you can also use the singular content item facade:

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

Calling ContentItem(contentItemId) throws an exception of type ItemNotFoundException, if there is no content item with the specified Id.

See Also

Related topics:

Feedback

How useful is this article?

Tell us more

Submit
Your message was successfully sent.

We appreciate your feedback.

Your message could not be sent.

OK