Work with items as resources
Sitecore provides methods to check for overridden items and to check if an item is stored in the database or the resource file.
Check if an item exists in both the database and the resource file
To check if a resource item or list of items is overridden, you can use the Database.DataManager.DataSource.GetItemLocations(itemId).IsOverridden()
method.
This method returns true
if the item exists both in the database and in the resource file, otherwise false
.
For example, to check if an item with itemId
is overridden in the Master database:
var db = Database.GetDatabase(“master”);
bool isOverridden = db.DataManager.DataSource.GetItemLocations(itemId).IsOverridden();
For example, to check if a list of items is overridden in the Core database:
var db = Database.GetDatabase(“core”);
ICollection<AggregatedItemLocations>
locations = db.DataManager.DataSource.GetItemLocations(itemIds);
var overriddenItemIds = locations.Where(l=>l.IsOverridden());
Check if an item exists in the database
To check if an item is stored in the database, you can use the Database.DataManager.DataSource.GetItemLocations(itemId).IsSql()
method.
For example, to check if an item exists in the Master database:
var db = Database.GetDatabase(“master”);
bool isInDatabase = db.DataManager.DataSource.GetItemLocations(itemId).IsSql();
Check if an item exists in the resource file
To check if an item is stored in the resource file, you can use the Database.DataManager.DataSource.GetItemLocations(itemId).IsResource()
method.
For example, to check if an item exists in the Master resource file:
var db = Database.GetDatabase(“master”);
bool isResource = db.DataManager.DataSource.GetItemLocations(itemId).IsResource();
Get a list of all items that exist in both the resource file and database
To get a list of all overridden items in the database, use the Database.DataManager.DataSource.GetOverriddenItems()
method.
For example, to get a list of all the overridden items in the Master database:
var db = Database.GetDatabase(“master”);
var overriddenItemIds = db.DataManager.DataSource.GetOverriddenItems();