Change connection string using document API

Mark Ellis asked on January 5, 2015 02:09

Hi all,

I have 2 Kentico sites running, and I'd like to retrieve some documents from one site to use in the other.

I've added the other database's connection string to web.config, but using the DocumentHelper class there isn't an overload to switch connection strings so it looks like I can't do it this way.

I've also tried wrapping the DocumentHelper.GetDocuments call in a using block and passing in a new CMSConnectionScope object initialised with the name of the other site's connection string, but that doesn't seem to affect the connection that DocumentHelper uses:

using (CMSConnectionScope cs = new CMSConnectionScope("OtherConnectionString", true))
{
    var result = DocumentHelper.GetDocuments(...
}

Appreciate any help.

Correct Answer

Martin Danko answered on January 6, 2015 15:35

Hi Mark,

As I've mentioned in our ticket... this scenario is not officially supported at the moment.

Currently, in Kentico 8.1, you may try using code similar to the below one, please note that it is not fully tested and our developers are working on other apporaches for the next version.

try
{
string connStringPrefix = "External"; // Prefix for connection string
string connString = connStringPrefix + ConnectionHelper.DEFAULT_CONNECTIONSTRING_NAME;
CMSStatic.CurrentContext = connString;
CacheHelper.CurrentCachePrefix = connString;
ConnectionHelper.ConnectionStringName = connString;
ConnectionHelper.ConnectionStringPrefix = connStringPrefix; // Use API against external DB
}
finally
{
CMSStatic.CurrentContext = null;
CacheHelper.CurrentCachePrefix = null;
}

And in the web.config add connection string to the other database as well:

<add name="ExternalCMSConnectionString" connectionString="..." />

Best regards, Martin

0 votesVote for this answer Unmark Correct answer

Recent Answers


Brenden Kehren answered on January 6, 2015 16:33

Mark,

On the lines of what Martin mentions I'd think you could gather your data in a dataset/table and create strongly typed objects by using object queries and passing in the datarow to create the new object reference. Then fill in some of your values (node, alias, etc.) and save it. As long as you have the same object in the local database you should be able to create the object dynamically based on the class name.

0 votesVote for this answer Mark as a Correct answer

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