3

I have a multilingual site with single node approach. Requirement is simple, need to create a task to fetch all items and generate a XML file.

I have created a Sitecore scheduled job and command and implemented the logic for generating XML in C#. It is generating the XML file as expected. But the only issue is it is not generating for different languages in the site, rather than its generating only for single language and that's English.

For example I have a node with two languages English and French. I created a scheduled job and written a logic to fetch all item names in the node and generates an XML file. But I need two separate XML file. One must holds the information in English version and another must be in French version.

Things I have tried:

  • Created same task in both English and French version and tried setting it in different interval time.

  • Created same command in both English and French version.

..

public void Hook(Item[] items, Sitecore.Tasks.CommandItem command, Sitecore.Tasks.ScheduleItem schedule) 
{ 
    var appSettings = new FetchAppSetting(); 
    using (new DatabaseSwitcher(Sitecore.Configuration.Factory.GetDatabase("web"))) 
    { 
        if (command.ID.ToString() == appSettings.Config.CommandId) 
        { 
            Settings SettingsItem = null; SettingsItem = new Settings(commandId); 
            //..and my logic for generating XML goes here.. 
        } 
    } 
}

Please do let me know if I'm making any mistakes on the above steps or give your suggestions to make it proper if I'm wrong.

Thanks in advance.

  • Do you have some sample code? You probably need to explicitly set which language you want when you get the item? Depending on how you are getting each item for your xml file... English is usually the default in Sitecore unless you specify the language – Ethan Schofer Feb 17 '18 at 20:46
  • @EthanSchofer I'm not setting any language specific or passing language code to get the item in language specific. – Gowtham Eswaramoorthy Feb 18 '18 at 3:16
2

If I had to implement such functionality, I would have created a schedule job only with below implementation

public class XmlGenerator()
{
    public string Database { get; set; }

    public string SitePath { get; set; }

    public List<string> ItemLanguages { get; set; }

    public XmlGenerator(string sitePath, string database, string language)
    {
        this.SitePath = sitePath;
        this.Database = database;
        this.ItemLanguages = language.Split('|').ToList();  
    }

    public void Run()
    {
        var database = Factory.GetDatabase(this.Database);

        // Add logic to create 1 file for each loop in the ItemLanguage 
        foreach(var itemLanguage in this.ItemLanguages)
        {
            using (new Sitecore.Globalization.LanguageSwitcher(itemLanguage))
            {
                var itemList = database.GetItem(this.SitePath, Language.Parse(itemLanguage)).Axes.GetDescendants().ToList();

                foreach(var item in itemList)
                {
                    // Your logic for generating the XML goes here
                }
            }
        }
    }
}

And the patch file for the agent as follows

<agent name="XML Generator Agent" type="YourNamespaceHere, YourAssembly" method="Run" interval="00:00:00">
    <param hint="1" desc="sitePath">sitecore/content/my site</param>
    <param hint="3" desc="database">master</param>
    <param hint="4" desc="language">en|fr-FR</param>
</agent>

If you don't want to pass in the language, then you will need to get the different versions of the item.

public class XmlGenerator()
{
    public string Database { get; set; }

    public string SitePath { get; set; }

    public XmlGenerator(string sitePath, string database)
    {
        this.SitePath = sitePath;
        this.Database = database;
    }

    public void Run()
    {
        var itemList = Factory.GetDatabase(this.Database).GetItem(this.SitePath).Axes.GetDescendants().ToList();

        foreach(var item in itemList)
        {
            foreach (var itemLanguage in item.Languages)
            {
                // Your Logic for generating the XML goes here
            }
        }
    }
}

And the agent config file will be as shown below

<agent name="XML Generator Agent" type="YourNamespaceHere, YourAssembly" method="Run" interval="00:00:00">
    <param hint="1" desc="sitePath">sitecore/content/my site</param>
    <param hint="3" desc="database">master</param>
</agent>
| improve this answer | |
  • 1
    Your answer is in the right direction, so I won't add another but you might want to check the code examples.. The first one is still ignoring the language when fetching the items and the second one is fetching all items twice ;) – Gatogordo Feb 18 '18 at 10:48
  • @Gatogordo I have updated the answer for the first one by using LanguageSwticher. The second one I am fetching all the items which then I read what language versions it has for each of them and then build up the xml – Hishaam Namooya Feb 18 '18 at 10:55
  • In the first one, also add the language in the GetItem method. In the second one, you can skip the GetItem for the language you already have.. will save you some time when processing lots of items.. I would also get the GetDatabase out of the loop btw.. ;) – Gatogordo Feb 18 '18 at 11:10
  • @Gatogordo just edited it :) – Hishaam Namooya Feb 18 '18 at 11:16
  • @HishaamNamooya Thanks for your valuable comment. I have used the first option with some specific changes according to my requirement and it worked out. Thanks once again. – Gowtham Eswaramoorthy Feb 19 '18 at 3:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.