Thursday, December 09, 2010

Assembly Version Conflicts for Newtonsoft.Json, Telerik, ComponentArt, etc.


Hi there,

Most of you know that Sitecore CMS relies on a few 3rd party components that help providing compelling web experience. A few come to mind: Telerik’s RTE, ComponentArt’s grids, JSON for .NET utility from Newtonsoft, Lucene.NET, etc.

Since earlier Sitecore versions (pre 6.3) rely on older versions of these 3rd party assemblies and most implementers are logically striving for using latest and greatest, conflicts of assembly versions arise. And strangely enough, I am hearing about these issues quite frequently now.

So how can these conflicts can be resolved?

Well, since an option of registering those in GAC is almost always a big NO-NO, the following technique proved to be workable for a few customers. It is called assembly version redirection.

What you need to do is add the following into the web.config’s <assemblyBinding>. The following example covers the “Newtonsoft.Json” assembly, however this is applicable to any other 3rd party assembly:

<dependentAssembly>
   <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" />
   <codeBase version="3.5.0.0" href="C:\wwwroot\Sitecore\WebSite\new_bin\Newtonsoft.Json.dll"/>
</dependentAssembly>

Afterwards, feel free to reference the newer version in your Visual Studio project, just remember to set the “Copy Local” property to False.

Note that since the release of Sitecore 6.3, most of those 3rd party assemblies were updated, so this article may be completely useless to you. Just make sure to check the exact Sitecore version you are using on the login page!

As always, thanks goes to our tech support for finding such a plausible alternative.

Wednesday, December 08, 2010

Treelist Not Registering Links in Sitecore


Greetings,

If you work with the Treelist field extensively like I do, you may notice that it is not registering links under certain conditions. You may experience this when search is not coming back with results, when working with Link Database, etc. Also the issue is happening when you have a complex source parameter passed to your treelist field on the template:

DataSource=/sitecore/content/Home&IncludeTemplatesForDisplay=PrimaryPage

Luckly, there are two solutions from our brilliant tech support.

1st option:
Add “databasename=master” attribute to every treelist field source:
DataSource=/sitecore/content/Home&IncludeTemplatesForDisplay=PrimaryPage&databasename=master

2nd option:
Override the Sitecore.Data.Fields.MultilistField class:

    1) Compile the following class and put the built assembly to the “/bin” folder:

public class MultilistField : MultilistField
{
    public MultilistField(Field innerField) : base(innerField) { }

    private Database GetDatabase()
    {
        string source = base.InnerField.Source;
        if (!string.IsNullOrEmpty(source))
        {
            if (!LookupSources.IsComplex(source))
            {
                return base.InnerField.Database;
            }
            Database database = LookupSources.GetDatabase(source);
            if (database != null)
            {
                return database;
            }
        }
        return base.InnerField.Database;
    }

    public override void ValidateLinks(LinksValidationResult result)
    {
        Database database = this.GetDatabase();
        if (database != null)
        {
            foreach (string str in base.Items)
            {
                if (ID.IsID(str))
                {
                    ID id = ID.Parse(str);
                    if (!ItemUtil.IsNull(id) && !id.IsNull)
                    {
                        Item targetItem = database.GetItem(id);
                        if (targetItem != null)
                        {
                            result.AddValidLink(targetItem, base.Value);
                        }
                        else
                        {
                            result.AddBrokenLink(base.Value);
                        }
                    }
                }
            }
        }
    }
}

2) Modify the \App_Config\FieldTypes.config file, specifically the Treelist field type definition:

<fieldType name="Treelist" type="Custom.MultilistField,Custom" />

Enjoy!