This post has been migrated from www.experimentsincode.com, we apologise if some of the images or content is missing

This blog has been migrated, original date 22 Feb 2010 I had written a previous blog about setting up a default language for Sitecore, however this was for Sitecore 5.3.x . The Sitecore 6 API is slightly different. I have detailed the Sitecore 6 solution below. Firstly create the following class:
    public class SqlServerDataProvider : Sitecore.Data.SqlServer.SqlServerDataProvider
    {

        public SqlServerDataProvider(string connectionString)
            : base(connectionString)
        {
        }
        protected override void LoadItemFields(string itemCondition, string fieldCondition, object[] parameters, Sitecore.Collections.SafeDictionary prefetchData)
        {
            base.LoadItemFields(itemCondition, fieldCondition, parameters, prefetchData);

            //set the default language to use
            Language defaultLang;
            Language.TryParse("en", out defaultLang);

            //full list of languages the site uses
            LanguageCollection coll = this.GetLanguages();

            foreach (var data in prefetchData)
            {
                if (data.Value != null)
                {
                    var uris = data.Value.GetVersionUris();
                    //check the item contains the default language before doing anything
                    if (uris.Cast().Any(x => x.Language == defaultLang))
                    {
                        //get the default language fields
                        var defaultUri = uris.Cast().First(x => x.Language == defaultLang);
                        //loop through each language
                        foreach (Language lang in coll)
                        {
                            //check if language doesn't exist
                            if (!uris.Cast().Any(x => x.Language == lang))
                            {
                                //update language with default language values
                                FieldList fields = data.Value.GetFieldList("en", defaultUri.Version.Number);
                                foreach (var key in fields.GetFieldIDs())
                                {
                                    data.Value.AddField(lang.Name, 1, key, fields[key]);
                                }

                            }
                        }
                    }
                }
            }
        }
    }
The main difference is the change in the for loop. In Sitecore 5.3.x the GetFieldsList method returned a collection of field objects that had two properties, the Key which was the field name and a Value property. This has been replaced with an object that wraps a HashList of ID's and Values. This makes more sense since field names could be change and break code but ID's will be consistent.
FieldList fields = data.Value.GetFieldList("en", defaultUri.Version.Number);
foreach (var key in fields.GetFieldIDs())
{
  data.Value.AddField(lang.Name, 1, key, fields[key]);
}
The XML describing the data provider in the web.config has also changed, add the provider detailed below:
<dataproviders>
      <main type="Sitecore.Data.$(database).$(database)DataProvider, Sitecore.Kernel">
	<param connectionstringname="$(1)">
        <name>$(1)</name>
      </main>
      <web type="Demo.Sitecore.SqlServerDataProvider, Demo">
        <param connectionstringname="$(1)">
        <name>$(1)</name>
      </web>
</dataproviders>
Then alter the provider used by the database from:
<database id="web" singleinstance="true" type="Sitecore.Data.Database, Sitecore.Kernel">
  <param desc="name">$(id)
  <icon>Network/16x16/earth.png</icon>
  <securityenabled>true</securityenabled>
  <dataproviders hint="list:AddDataProvider">
    <dataprovider ref="dataProviders/main" param1="$(id)">
</dataprovider></dataproviders></database>
An update the the provider to:
<param desc="name">$(id)  
<icon>Network/16x16/earth.png</icon>  
<securityenabled>true</securityenabled>  
<dataproviders hint="list:AddDataProvider">
    <dataprovider ref="dataProviders/web" param1="$(id)">
</dataprovider>
</dataproviders>
comments powered by Disqus