Getting item from database returns only en version

149 Views Asked by At

I'm trying to get an item from database then publish it via code. The item only has pt-pt language (portugal)

I'm using Database.GetDatabase, when I try to get the item from the database. It gives me an item that has en language.

Item item = Database.GetDatabase("master").GetItem(guid);

Then when I try to edit the item using Editing.BeginEdit(), it edits the en version then proceeds to publish it.

Is there a way to get the pt-PT version then edit that version instead of en?

1

There are 1 best solutions below

0
Surendra Singh Aswal On

Each item can contain multiple languages. You can use the Sitecore.Globalization.Language class to specify a language when accessing an item using the Sitecore.Data.Database.GetItem() method. For example, to access the current version of the /Sitecore/Content/Home item in the default en language:

Sitecore.Data.Database master = Sitecore.Configuration.Factory.GetDatabase("master");

Sitecore.Globalization.Language language = Sitecore.Globalization.Language.Parse("en");

Sitecore.Data.Items.Item home = master.GetItem("/sitecore/content/home",language);
Note

If you do not specify a language, Sitecore uses the context language by default. Sitecore user interface components specify the content language.

You can use the Sitecore.Data.Items.Item.Versions.Count property to determine if any versions exist for a language. For example, to access the current version in each language of the /Sitecore/Content/Home item in the Master database:

Sitecore.Data.Database master = Sitecore.Configuration.Factory.GetDatabase("master");

Sitecore.Data.Items.Item home = master.GetItem("/sitecore/content/home");

foreach (Sitecore.Globalization.Language language in home.Languages)
{
    Sitecore.Data.Items.Item langItem = home.Database.GetItem(home.ID, language);
    
    if (langItem.Versions.Count > 0)
    {
        // Process language item
    }
}