Using a default language in Sitecore 6.2

Published by on
Tags:

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:


      
$(1)
$(1)

Then alter the provider used by the database from:


  $(id)
  Network/16x16/earth.png
  true
  
    

An update the the provider to:

  $(id)
  Network/16x16/earth.png
  true
  
    
 

All blogs tagged with ' database'

Projects

Have you read?

Safari 4 Cookie Problem



Blogs by date