If you have the pageItem
which is an instance of Sitecore.Data.Items.Item
class, you can wrap your code into a DatabaseSwitcher
and DeviceSwitcher
using (new DatabaseSwitcher(pageItem.Database))
{
using (new DeviceSwitcher(pageItem.Database.Resources.Devices.GetAll().WhereFirstOrDefault(t=>t.Name.Equals("default",System.StringComparison.OrdinalIgnoreCase)))))
{
// Add your code block here
}
}