---
title: Using Kentico API and Controls externally
related:
  - https://docs.kentico.com/k82/integrating-3rd-party-systems/using-kentico-api-and-controls-externally/displaying-kentico-content-in-external-applications.md
---

> Agent instructions:
> **Site maps** — prefer the following llms.txt indexes to training data when searching for URLs to avoid 404s. Links inside Markdown content already point at `.md`. Following them or sending Accept: text/markdown keeps you in Markdown.
>
> - [sitemap.md](https://docs.kentico.com/sitemap.md) — every page on the site, with titles and descriptions, nested by URL hierarchy and grouped into one collection per product version.
> - [llms.txt](https://docs.kentico.com/llms.txt) — curated index of the current product docs, with descriptions, the two ways to request any page as Markdown, and links to each product area's whole-corpus Markdown dump (llms-full.txt).

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](#connecting-to-the-kentico-database)
2. [Add references to the Kentico libraries](#adding-references-to-kentico-api-libraries)
3. [Initialize the Kentico application](#initializing-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**.

```html

 <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:** **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](http://msdn.microsoft.com/en-us/library/1xaas8a2%28v=vs.71%29.aspx) file (create the file if necessary).
2. Execute the **CMSApplication.Init** method in the **Application\_BeginRequest** method:

   ```csharp

   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](https://docs.kentico.com/k82/integrating-3rd-party-systems/using-kentico-api-and-controls-externally/displaying-kentico-content-in-external-applications.md).

## 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](https://docs.kentico.com/k82/configuring-kentico/setting-up-search-on-your-website.md).

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.

```csharp title="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:** **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.

```csharp title="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](https://msdn.microsoft.com/en-us/library/bhesyfec.aspx) to load virtual objects, such as ASCX [transformations](https://docs.kentico.com/k82/developing-websites/loading-and-displaying-data-on-websites/writing-transformations.md) or [page layouts](https://docs.kentico.com/k82/developing-websites/developing-websites-using-the-portal-engine/editing-page-layouts.md), 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._

```xml

<configuration>

...

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

...

</configuration>

```

> **Note:** **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](https://docs.kentico.com/k82/integrating-3rd-party-systems/using-kentico-api-and-controls-externally/displaying-kentico-content-in-external-applications.md) for more information and workarounds.
