Console app CMSApplication.Init() seemingly can't read CMSConnectionString

2024/06/11 9:13 AM

Hi, I'm trying to access to Kentico API from a console app but am getting the exception Cannot access the database specified by the 'CMSConnectionString' connection string. Please install the database externally and set a correct connection string.

Running the following I can see that connection string is read and the SqlConnection opens successfully, which rules out an invalid connection string or malformed json file.

Is there any other setup required so that the Kentico API can read the json?

using CMS.DataEngine;
using Microsoft.Data.SqlClient;
using Microsoft.Extensions.Configuration;

var configuration = new ConfigurationBuilder()
    .SetBasePath(AppContext.BaseDirectory)
    .AddJsonFile("appsettings.json").Build();

var connectionString = configuration.GetConnectionString("CMSConnectionString");
await using (var connection = new SqlConnection(connectionString))
{
    connection.Open();
    Console.WriteLine("Database connection successful."); // success
}

CMSApplication.Init(); // exception

The license file is places into the same directory. I can't see anything missing from https://docs.kentico.com/developers-and-admins/api/use-the-xperience-by-kentico-api-externally

Thanks

Environment

  • Xperience by Kentico version: [29.1.3]
  • .NET version: [8]
  • Deployment environment: local

Answers

2024/06/14 7:08 AM

If you use the API, you don't have to open the database manually. The API handles this.

Does this also not work if you use the Query API to get some data from XbyK?

To answer this question, you have to login first.