Have DocumentHelper query a different connection string

Douglas Fittipaldi asked on May 21, 2025 23:25

I'm using 13.0.138 and whenever I use DocumentHelper it's picking up the CMSConnectionString from web.config as normal, but I was wondering if it was possible to specify in the DocumentHelper a different connection to query from. This is to check between different versions of the same document between two Kentico databases. I did see some code in the answer here but since that code is really old I wondered if there was a better way to do this. I guess I'm hoping for something like this:

var doc1 = DocumentHelper.GetDocuments([classname]).WhereEquals("ID", id);
var doc2 = DocumentHelper.ConnectionString("otherDb").GetDocuments([classname]).WhereEquals("ID", id);

That won't work, but I hope this makes it clear what I'm trying to do. What can I do to accomplish something like this? Thanks for any advice.

Recent Answers


Juraj Ondrus answered on May 22, 2025 05:17 (last edited on May 22, 2025 05:17)

I would say the old code approach is still OK, maybe there were some API changes, but in newer Kentico versions this was used:

using (var c = new CMSConnectionContext("<connectionstring>"))
    {
        external = DocumentHelper.GetDocuments()......;
    }  
    original = DocumentHelper.GetDocuments()......
0 votesVote for this answer Mark as a Correct answer

Douglas Fittipaldi answered on May 22, 2025 14:59

Thanks Juraj, but unfortunately I'm running into problems with either example. For the using statement you suggested, I got this error:

The initialization of CMS.DataEngine.AbstractInfoBase1[[CMS.DataEngine.DataClassInfo, CMS.DataEngine, Version=13.0.13.0, Culture=neutral, PublicKeyToken=834b12a258f213f9]] failed. See the inner exception for details. at CMS.DataEngine.AbstractInfoBase1.EnsureData(Boolean loadDefault, Boolean applyTypeCondition) at CMS.DataEngine.AbstractInfoBase1.get_DataClass() at CMS.DataEngine.AbstractInfoBase1.ItemChanged(String columnName) at CMS.DataEngine.AbstractInfoBase1.TryGetValue(String columnName, Object& value) at CMS.DataEngine.AbstractInfoBase1.GetValue(String columnName) at CMS.DataEngine.DataClassInfoBase1.get_ClassShowAsSystemTable() at CMS.DataEngine.DataClassInfo.get_TypeInfo() at CMS.DataEngine.BaseInfo.CheckLicense(ObjectActionEnum action, String domainName) at CMS.DataEngine.BaseInfo.GeneralizedInfoWrapper.CheckLicense(ObjectActionEnum action, String domainName) at CMS.DataEngine.AbstractInfoProvider3.GetObjectQuery(Boolean checkLicense) at CMS.DataEngine.DataClassInfoProviderBase1.GetInfoByColumn[T](String columnName, T value) at CMS.DataEngine.AbstractInfoProvider3.GetInfoByCodeName(String codeName, Boolean useHashtable) at CMS.DataEngine.DataClassInfoProviderBase1.GetDataClassInfo(String name) at CMS.DocumentEngine.DocumentEngineModule.AbstractProvider_LoadProvider(Object sender, LoadProviderEventArgs e) at CMS.DataEngine.InfoProviderLoader.OnLoadProvider.Invoke(Object sender, LoadProviderEventArgs e) at CMS.DataEngine.InfoProviderLoader.GetInfoProvider(String objectType, Boolean exceptionIfNotFound) at CMS.DataEngine.InfoProviderLoader.GetInfoProvider[TProvider](String objectType, Boolean exceptionIfNotFound) at CMS.DocumentEngine.TreeNodeProvider.GetDocuments(String className) at CMS.DocumentEngine.DocumentHelper.GetDocuments(String className)

As for the old code, when I paste it into VS I get an error for CMSStatic.CurrentContext = connString: CS0305: Using the generic type 'CMSStatic<TValue>' requires 1 type arguments.

0 votesVote for this answer Mark as a Correct answer

Juraj Ondrus answered on May 26, 2025 07:17

I had to dig into this bit deeper. There were made some API adjustments in newer versions. Two things keep on mind:

  1. you need to set the connection context like this using (var c = new CMSConnectionContext("External")) - where the "External" value is just a prefix for the external connection string. This means, that the connection string should be named "ExternalCMSConnectionString" (the prefix could be also something custom, this is just an example)
  2. It depends where you run the code - it must be done within the admin app. It may not work when running in custom class assembly or elsewhere as the context might be missing.
0 votesVote for this answer Mark as a Correct answer

Douglas Fittipaldi answered on May 27, 2025 14:57

Hi Juraj:

Thanks for looking into this but unfortunately that didn't help either, probably because the code isn't being run in the admin app, but an external website. What are my options then? Should I just run a basic database query to get what I need?

0 votesVote for this answer Mark as a Correct answer

Juraj Ondrus answered on May 27, 2025 15:03

Well, in this case I guess that the context is missing. I would do it as you suggested then - do a basic DB queries to get the data from different DB

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.