Is this really the version you are looking for? Kentico 8.2 documentation is no longer updated. Use the switch in the header to choose your Kentico version.

Using Kentico API and Controls externally

This page describes how to configure external ASP.NET applications so that they support the Kentico API and Controls. Using Kentico functionality externally allows you to create scenarios such as:

  • Separate applications integrated with your main Kentico website
  • Custom websites or applications that use Kentico as a content repository

Start Visual Studio and open your external application's project. You need to perform the following steps:

  1. Connect to the Kentico database
  2. Add references to the Kentico libraries
  3. Initialize the Kentico application

Connecting to the Kentico database

To be able to access the Kentico database, you need to specify the appropriate connection string in your application's web.config or app.config file.

Add the connection string into the configuration/connectionStrings section using an <add> element named CMSConnectionString.

 <configuration>
  <connectionStrings>

    ...

    <add name="CMSConnectionString" connectionString="Persist Security Info=False;database=Kentico;server=myserver;user id=username;password=mypassword;Current Language=English;Connection Timeout=120;" />

  </connectionStrings>

Tip: We recommend copying the exact connection string from the web.config file of your Kentico web project.

 

 

Adding references to Kentico API libraries

Before you can use Kentico functionality in your application, you need to add references to the libraries containing the required code:

  1. Right-click your application's project root in the Solution Explorer and select Add -> Reference.
  2. On the Browse tab, click Browse and navigate to the Lib folder of the Kentico project.
  3. Select all dll files in the Lib folder and click Add.
  4. Click OK.

You also need to make the CMSDependencies folder from the CMS directory of the Kentico project available for your application:

  • For Web applications, copy the CMSDependencies folder into your project root.
  • For Console or Windows applications, copy the CMSDependencies folder into the output folder containing the executable file.

Initializing the Kentico application

You need to initialize Kentico before making calls to its API from an external project.

Web applications

  1. Edit your application's Global.asax file (create the file if necessary).

  2. Execute the CMSApplication.Init method in the Application_BeginRequest method:

    void Application_BeginRequest(object sender, EventArgs e) 
    {
    	CMS.DataEngine.CMSApplication.Init();
    }

Non-web applications (for example Console applications)

  1. Edit your application's Main method.
  2. Execute the CMSApplication.Init method at any point before the first call of the Kentico API.

You can now use the Kentico API and Controls in your external application. The Kentico API allows you to perform any action available in the standard administration interface. A typical example is Loading and displaying content from the Kentico database.

Additional configuration and tips

Sharing project files with the Kentico application

If you need to call API that works with a specific folder in the Kentico project, you can map the physical path used by the Kentico API in your external application to a specific Kentico project. For example, you can use this approach if you wish to externally update or rebuild the files of smart search indexes.

Set the SystemContext.WebApplicationPhysicalPath property to the path of your Kentico project's CMS folder. Map the path before you initialize the Kentico application (i.e. call CMSApplication.Init), for example in the Application_Start method of Global.asax, or in your application's Main method.

Example
// Maps the physical path used by the Kentico API in the external application to the folder of the connected Kentico project
CMS.Base.SystemContext.WebApplicationPhysicalPath = "C:\\inetpub\\wwwroot\\Kentico\\CMS";

Working with the user context

When working with the Kentico API in an external application, the default Kentico user context is not available. The standard API for getting the current user (CMS.Membership.MembershipContext.AuthenticatedUser) always returns the "public" user when called externally.

To use API that relies on the context of a specific user, you need to enclose the code into a using statement with a declared CMSActionContext instance and the appropriate UserInfo object as a parameter.

Note: If your external application has its own authentication/user functionality, calling certain parts of the Kentico API may cause the external application to log out the current user. To avoid this issue, use the approach described here to explicitly set the user context for your code.

Example
using CMS.Base;
using CMS.Membership;
using CMS.EventLog;

...

// Gets an object representing a specific Kentico user
UserInfo user = UserInfoProvider.GetUserInfo("Andy");

// Sets the context of the user
using (new CMSActionContext(user))
{
	// Logs an information event into the Kentico event log, with "Andy" as the user who caused the event
	EventLogProvider.LogEvent(EventType.INFORMATION, "External Application", "Event_Code", "Details");
}

Disabling the Kentico Virtual path provider

Kentico uses a virtual path provider to load virtual objects, such as ASCX transformations or page layouts, from the database. When using the Kentico API externally, you may wish to disable the virtual path provider for the following reasons:

  • To avoid conflicts if your application has its own virtual path provider implementation
  • If your external application does not use Kentico virtual objects at all (disabling the virtual path provider reduces overhead)

To disable the Kentico virtual path provider:

  1. Edit your external application's web.config or app.config file.
  2. Add the <appSettings> element under the <configuration> section.
  3. Add the CMSUseVirtualPathProvider key and set the value to false.
<configuration>

...

	<appSettings>
		<add key="CMSUseVirtualPathProvider" value="false" />
	</appSettings>

...

</configuration>

Note: Without the virtual path provider, you cannot use ASCX transformations (or other Kentico virtual objects) to display data in your external application. See Displaying Kentico content in external applications for more information and workarounds.